问题记录:

1、在使用protobuf反射机制动态加载解析proto文件时,发现当proto文件中含有import系统proto文件的语句时,无法解析文件,解决方法是添加路径映射。

 google::protobuf::compiler::DiskSourceTree sourceTree;
sourceTree.MapPath("data", "./data");
sourceTree.MapPath("", "D:\\Documents\\Program\\Tools\\protobuf-3.0.2\\install\\x86\\debug\\include");
google::protobuf::compiler::Importer importer(&sourceTree, NULL);
const google::protobuf::FileDescriptor *fileDescriptor = importer.Import("data/test.proto");

  代码如上,其中的第3行为解决方案,增加之后才能正确解析。分析其原因是,Importer对象用于导入并解析proto文件,当proto文件中import了其他proto文件时,Importer对象递归导入并解析该proto文件;第二行告诉了Importer去哪里找test.proto,但是却没有告诉Importer去哪里找系统自带的proto文件,因此需要加上第3行,并且别名应该留空!

2、jsoncpp的下载和使用

  jsoncpp源码可以从github上得到:jsoncpp-master.zip

  解压后使用python执行根目录下的 amalgamate.py ,这个脚本将jsoncpp的头文件和源代码进行了合并,最终合并成了三个文件:

  dist\json\json.h  dist\json\json-forwards.h  dist\jsoncpp.cpp

  使用时把 jsoncpp.cpp文件连同json文件夹一起拷贝到工程目录下,两者保持同级,代码中包含 json\json.h 即可。

3、遍历proto文件中的所有消息以及所有字段

 #include <iostream>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/util/json_util.h> int parseProtoFile()
{
// 准备配置好文件系统
google::protobuf::compiler::DiskSourceTree sourceTree;
// 将当前路径映射为项目根目录 , project_root 仅仅是个名字,你可以你想要的合法名字.
sourceTree.MapPath("data", "./data");
sourceTree.MapPath("", "D:\\Documents\\Tools\\protobuf-3.0.2\\install\\x86\\debug\\include");
// 配置动态编译器.
google::protobuf::compiler::Importer importer(&sourceTree, NULL);
// 动态编译proto源文件。 源文件在./source/proto/test.proto .
auto fileDescriptor = importer.Import("data/complex.proto"); std::cout << fileDescriptor->message_type_count() << std::endl;
for (auto i = ; i < fileDescriptor->message_type_count(); i++)
{
auto descriptor = fileDescriptor->message_type(i); std::cout << descriptor->name() << " " << descriptor->field_count() << " " << descriptor->nested_type_count() << std::endl; auto descriptor1 = descriptor->containing_type(); if (descriptor1)
{
std::cout << descriptor1->name() << std::endl;
}
} std::cout << fileDescriptor->name() << std::endl; auto descriptor = fileDescriptor->message_type();
for (auto i = ; i < descriptor->field_count(); i++)
{
auto fieldDes = descriptor->field(i);
google::protobuf::SourceLocation outLocation;
if (fieldDes->GetSourceLocation(&outLocation))
{
printf("%s: %d %d %d %d\nleading_comments:%s\ntrailing_comments:%s\n",
fieldDes->full_name().c_str(),
outLocation.start_line, outLocation.start_column, outLocation.end_line, outLocation.end_column,
outLocation.leading_comments.c_str(), outLocation.trailing_comments.c_str());
for (auto comment : outLocation.leading_detached_comments)
{
printf("leading_detached_comments:%s\n", comment.c_str());
}
}
else
{
std::cout << "fail" << std::endl;
}
} #if 0
// 现在可以从编译器中提取类型的描述信息.
auto descriptor1 = importer.pool()->FindMessageTypeByName("T.Test.InMsg"); // 创建一个动态的消息工厂.
google::protobuf::DynamicMessageFactory factory;
// 从消息工厂中创建出一个类型原型.
auto proto1 = factory.GetPrototype(descriptor1);
// 构造一个可用的消息.
auto message1 = proto1->New();
// 下面是通过反射接口给字段赋值.
auto reflection1 = message1->GetReflection();
auto filed1 = descriptor1->FindFieldByName("id");
reflection1->SetUInt32(message1, filed1, ); // 打印看看
std::cout << message1->DebugString() << std::endl; std::string output;
google::protobuf::util::MessageToJsonString(*message1, &output);
std::cout << output << std::endl; // 删除消息.
delete message1;
#endif
return ;
} #define Log(format, ...) printf(format, __VA_ARGS__) void printOneField(const google::protobuf::FieldDescriptor *fieldDescriptor)
{
Log(" field[%d]: name %s, full name %s, json name %s, type %s, cpp type %s\n",
fieldDescriptor->index(), fieldDescriptor->name().c_str(), fieldDescriptor->full_name().c_str(), fieldDescriptor->json_name().c_str(),
fieldDescriptor->type_name(), fieldDescriptor->cpp_type_name());
Log(" debug string:%s\n", fieldDescriptor->DebugString().c_str());
} void printOneMessage(const google::protobuf::Descriptor *descriptor)
{
// 消息的总体信息
Log("msg[%d]: name %s, full name %s, field count %d, nested type count %d\n",
descriptor->index(), descriptor->name().c_str(), descriptor->full_name().c_str(), descriptor->field_count(),
descriptor->nested_type_count());
Log("\tdebug string: %s\n", descriptor->DebugString().c_str()); // 遍历消息的所有字段
for (int fieldLoop = ; fieldLoop < descriptor->field_count(); fieldLoop++)
{
const google::protobuf::FieldDescriptor *fieldDescriptor = descriptor->field(fieldLoop); printOneField(fieldDescriptor);
} // 遍历消息的所有嵌套消息
for (int nestedLoop = ; nestedLoop < descriptor->nested_type_count(); nestedLoop++)
{
const google::protobuf::Descriptor *nestedDescriptor = descriptor->nested_type(nestedLoop); printOneMessage(nestedDescriptor);
}
} void printOneFile(const google::protobuf::FileDescriptor *fileDescriptor)
{
Log("******** message info in proto file, msg count %d ********\n", fileDescriptor->message_type_count()); // 遍历文件中的所有顶层消息
for (int msgLoop = ; msgLoop < fileDescriptor->message_type_count(); msgLoop++)
{
const google::protobuf::Descriptor *descriptor = fileDescriptor->message_type(msgLoop); printOneMessage(descriptor);
}
} bool testProto(const char *protoIncludePath, const char *testProtoPath, const char *testProtoFile)
{
// 配置文件系统
google::protobuf::compiler::DiskSourceTree sourceTree;
sourceTree.MapPath("", protoIncludePath);
sourceTree.MapPath("data", testProtoPath);
//sourceTree.MapPath("data", "./data");
//sourceTree.MapPath("", "D:\\Documents\\Tools\\protobuf-3.0.2\\install\\x86\\debug\\include");
// 配置动态编译器
google::protobuf::compiler::Importer importer(&sourceTree, NULL);
// 动态编译proto源文件
const google::protobuf::FileDescriptor *fileDescriptor = importer.Import("data/" + std::string(testProtoFile)); if (fileDescriptor == NULL)
{
printf("import \"%s\" failed, last error msg: %s\n", testProtoFile, sourceTree.GetLastErrorMessage().c_str());
return false;
} printOneFile(fileDescriptor); return true;
} int main()
{
const char *protoIncludePath = "D:\\Documents\\Tools\\protobuf-3.0.2\\install\\x86\\debug\\include";
const char *testProtoPath = "C:\\Users\\Administrator\\Desktop\\Document\\C++\\protobufTest\\protobufTest\\data";
const char *testProtoFile = "complex.proto"; testProto(protoIncludePath, testProtoPath, testProtoFile); //parseProtoFile();
//printf("Hello world!\n");
return ;
}

protobuf lib库的使用的更多相关文章

  1. lib库dll库的使用方法与关系

    一.lib库 lib库有两种:一种是静态lib(static Lib),也就是最常见的lib库,在编译时直接将代码加入程序当中.静态lib中,一个lib文件实际上是任意个obj文件的集合,obj文件是 ...

  2. 在VS中添加lib库的三种方法

    注意: 1.每种方法也要复制相应的DLL文件到相应目录,或者设定DLL目录的位置,具体方法为:"Properties" -> "Configuration Prop ...

  3. 如何把一个android工程作为另外一个android工程的lib库

    http://zhidao.baidu.com/question/626166873330652844 一个工程包含另一个工程.相当于一个jar包的引用.但又不是jar包反而像个package 在网上 ...

  4. Android so lib库远程http下载和动态注册

    一.背景 在开发Android应用程序的实现,有时候需要引入第三方so lib库,但第三方so库比较大,例如开源第三方播放组件ffmpeg库, 如果直接打包的apk包里面, 整个应用程序会大很多.经过 ...

  5. VS2010中添加lib库引用

    VS2010中添加lib库引用: 1 菜单  项目---> 属性--->配置属性-->链接器---->输入---附加依赖项,  加入库名,如: my_API.lib; 或是在c ...

  6. lib库依赖解决

    当前环境之前是装过MySQL官方版本5.6.22,想测试Percona版本MySQL.启动Percona-MySQL报错. [root@dg7 support-files]# /etc/init.d/ ...

  7. .h头文件、 .lib库文件、 .dll动态链接库文件之间的关系

    转自.h头文件. .lib库文件. .dll动态链接库文件之间的关系 h头文件作用:声明函数接口 dll动态链接库作用:含有函数的可执行代码 lib库有两种: (1)静态链接库(Static Liba ...

  8. .h头文件 .lib库文件 .dll动态库文件之间的关系

    .h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib不是.dll,若生成了DLL,则肯定也生成 LIB文件.如果要完成源代码的编译和链接,有头文件和lib就够 ...

  9. LIB库加载方法-引用百度百科

    LIB库加载方法,有三种,如下: 1.LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中\"Add Files to Project\ ...

随机推荐

  1. Spring MVC处理(下周完善)

    http://www.cnblogs.com/xiepeixing/p/4244574.html http://blog.csdn.net/kobejayandy/article/details/12 ...

  2. 在bmp上添加字符2

    void CTextOnbmpDlg::OnButton2() {  // TODO: Add your control notification handler code here  FILE *f ...

  3. 图像采集系统的Camera Link标准接口设计

    高速数据采集系统可对相机采集得到的实时图像进行传输.实时处理,同时实现视频采集卡和计算机之间的通信.系统连接相机的接口用的是Camera Link接口,通过Camera Link接口把实时图像高速传输 ...

  4. STM32 下的库函数和寄存器操作比较

    以 led闪烁中的flashLed函数例子: 库函数操作简单,但是效率不如寄存器操作的高: 寄存器操作很复杂,因为要熟悉上百个寄存器,但是程序效率很高 /**下面是通过直接操作库函数的方式实现IO控制 ...

  5. Flex动态获取方法报错

    1.错误描述 2.错误原因 由于Flex文件修改后,需要将其编译成swf文件,刚修改的方法没有编译,再加上历史缓存的原因,导致报错 3.解决办法 将Flex项目重新clean一下,并将MyEclips ...

  6. SDK、JDK、JRE、ADB、AVD到底都是啥?

    SDK:Software Development Kit,软件开发工具包是一些被软件工程师用于为特定的软件包.软件框架.硬件平台.操作系统等创建应用软件的开发工具的集合,一般而言SDK即开发 Wind ...

  7. VS2013 图片背景·全透明背景图(转)

    Note: 1.xaml编辑器和个别的编辑器(如HTML的)因为是承载在VS的一个子窗口上,所以背景依然是黑色的. 2.该背景必须在VS实验环境下使用. 效果图: 1.准备工作 1.先准备Visual ...

  8. 滚动条实现RGB颜色的调制(窗体程序)--JAVA基础

    1.用到的JFrame类的对象frame的方法: frame.setLayout(); 设置框架布局格式,有frame.setLayout(new GridLayout(5,1));为网格布局格式 f ...

  9. 【CJOJ1167】【洛谷1894】[USACO4.2]完美的牛栏

    题面 Description 农夫约翰上个星期刚刚建好了他的新牛棚,他使用了最新的挤奶技术.不幸的是,由于工程问题,每个牛栏都不一样.第一个星期,农夫约翰随便地让奶牛们进入牛栏,但是问题很快地显露出来 ...

  10. data数据不一致的问题

    经常会遇到that.data能打印出来(能访问到),而that.data.xxx不能打印(为空)的情况.特别是在调用了云方法,然后setData的时候,为什么会出现这样的情况不明. 解决方法,将需要用 ...