protobuf lib库的使用
问题记录:
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库的使用的更多相关文章
- lib库dll库的使用方法与关系
一.lib库 lib库有两种:一种是静态lib(static Lib),也就是最常见的lib库,在编译时直接将代码加入程序当中.静态lib中,一个lib文件实际上是任意个obj文件的集合,obj文件是 ...
- 在VS中添加lib库的三种方法
注意: 1.每种方法也要复制相应的DLL文件到相应目录,或者设定DLL目录的位置,具体方法为:"Properties" -> "Configuration Prop ...
- 如何把一个android工程作为另外一个android工程的lib库
http://zhidao.baidu.com/question/626166873330652844 一个工程包含另一个工程.相当于一个jar包的引用.但又不是jar包反而像个package 在网上 ...
- Android so lib库远程http下载和动态注册
一.背景 在开发Android应用程序的实现,有时候需要引入第三方so lib库,但第三方so库比较大,例如开源第三方播放组件ffmpeg库, 如果直接打包的apk包里面, 整个应用程序会大很多.经过 ...
- VS2010中添加lib库引用
VS2010中添加lib库引用: 1 菜单 项目---> 属性--->配置属性-->链接器---->输入---附加依赖项, 加入库名,如: my_API.lib; 或是在c ...
- lib库依赖解决
当前环境之前是装过MySQL官方版本5.6.22,想测试Percona版本MySQL.启动Percona-MySQL报错. [root@dg7 support-files]# /etc/init.d/ ...
- .h头文件、 .lib库文件、 .dll动态链接库文件之间的关系
转自.h头文件. .lib库文件. .dll动态链接库文件之间的关系 h头文件作用:声明函数接口 dll动态链接库作用:含有函数的可执行代码 lib库有两种: (1)静态链接库(Static Liba ...
- .h头文件 .lib库文件 .dll动态库文件之间的关系
.h头文件是编译时必须的,lib是链接时需要的,dll是运行时需要的. 附加依赖项的是.lib不是.dll,若生成了DLL,则肯定也生成 LIB文件.如果要完成源代码的编译和链接,有头文件和lib就够 ...
- LIB库加载方法-引用百度百科
LIB库加载方法,有三种,如下: 1.LIB文件直接加入到工程文件列表中 在VC中打开File View一页,选中工程名,单击鼠标右键,然后选中\"Add Files to Project\ ...
随机推荐
- activiti怎么实现用户自定义流程?请先看这里
最近一两个星期收到了好几个qq好友添加的请求和csdn的私信,里面基本都是询问activiti相关的问题. 尤其是今天有个朋友给我发了私信,内容如下: 你好,请问你关于activiti工作流的问题:怎 ...
- 常用u-boot命令详解(全)
U-boot发展到现在,他的命令行模式已经非常接近Linux下的shell了,命令行模式模式下支持"Tab"键的命令补全和命令的历史记录功能.而且如果你输入的命令的前几个字符和别的 ...
- Linux开机启动图片修改
Linux启动时会在屏幕上显示一个默认的开机图片,我们可以修改成为自己的图片,需要做以下工作 软件gimp下载地址:http://www.rayfile.com/zh-cn/files/0bb556b ...
- USB转串口驱动代码分析
1.USB插入时,创建设备 [plain] view plaincopy DriverObject->DriverExtension->AddDevice = USB2COM_PnPAdd ...
- P1141 01迷宫
https://www.luogu.org/problemnew/show/P1141 题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样 ...
- 你还在为如何区分ASCII编码、GB2312编码、Unicod、UTF-8编码而烦恼吗,一篇文章让你柳暗花明
字符编码 我们已经讲过了,字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采用8个比特 ...
- HALCON学习-下载、安装
下载地址: 官网 HALCON学习网 安装: 直接安装文件halcon-12.0-windows-x86.exe 破解,汉化: 在HALCON学习网中有Licence文件“license_suppor ...
- 内置函数:filter函数
功能: filter函数用于过滤序列,将满足条件的元素取出来构成新的序列. 用法: filter(function, iterable) 接受两个参数,第一个函数为过滤函数(返回True后者False ...
- 促进客户转化,提高客单价!酷客多小程序发布版本V1.0.9!
商户和企业主的又一次福音!酷客多小程序新零售o2o商城系统酷爱用户,为了追求极致的用户体验,没日没夜地沉浸于新功能的开发,经过一番努力,新功能终于上线啦! 此次版本迭代,在原有功能基础上做了大幅提升, ...
- 试着讲清楚:js代码运行机制
一. js运行机制 js执行引擎 经常看文章的说到js是带线程的,其实这个说法非常的模糊,准确的是js执行引擎是单线程的,js执行引擎就是js代码的执行器,有了这个概念就可以下来说说js是如何运行的了 ...