Google Protocol Buffer 的常规用法需要使用 protoc.proto 编译成 .pb.h.pb.cc,这样做效率非常高,但是耦合性也很高。在某些追求通用性而不追求性能的场景下,需要使用 .proto 直接操作 protobuf 数据。

本例使用的 .proto 文件来自 https://developers.google.com/protocol-buffers/docs/cpptutorial ,但是把它拆成了两个 .proto 文件

// ./proto/person.proto
syntax = "proto2";
package tutorial; message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3; enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
} message PhoneNumber {
optional string number = 1;
optional PhoneType type = 2 [default = HOME];
} repeated PhoneNumber phones = 4;
}
// ./proto/person.proto
syntax = "proto2";
package tutorial;
import "person.proto"; message AddressBook {
repeated Person people = 1;
}=

示例代码

#include <iostream>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/util/json_util.h> using namespace google::protobuf; /*
构造 Importer 必须指定 error_collector 用于处理错误信息
AddError 是纯虚函数,必须 override
*/
class MyMultiFileErrorCollector : public compiler::MultiFileErrorCollector
{
virtual void AddError(const std::string& filename, int line, int column, const std::string& message) override
{
std::cout << "file: " << filename << ", line: " << line << ", col: " << column<< ", message: " << message << std::endl;
}
}; int main()
{
/*
构造 DiskSourceTree,并添加虚拟路径。protobuf 使用 Importor 导入 .proto 文件时,会虚拟路径进行查找
在本例中,导入 addressbook.proto 时会使用 ./proto/addressbook.proto
*/
compiler::DiskSourceTree disk_source_tree;
disk_source_tree.MapPath("", "proto"); MyMultiFileErrorCollector error_collector; /*
导入 addressbook.proto 时,会自动导入所有依赖的 .proto 文件
在本例中,person.proto 也会被自动导入
*/
compiler::Importer importer(&disk_source_tree, &error_collector);
const FileDescriptor* file_descriptor = importer.Import("addressbook.proto");
if (!file_descriptor) {
exit(-1);
} // 把 addressbook.proto 和 person.proto 都打印出来
std::cout << "====== all .proto files ======" << std::endl;
std::cout << file_descriptor->DebugString() << std::endl;
for (int i = 0; i < file_descriptor->dependency_count(); ++i) {
std::cout << file_descriptor->dependency(i)->DebugString() << std::endl;
} // 构造 DynamicMessageFactory 用于动态构造 Message
DynamicMessageFactory message_factory(importer.pool()->generated_pool()); /*
查找 Person 的 Descriptor
不能使用 file_descriptor 查找,它只包含 addresssbook.proto ,只能找到 AddressBook,而 DescriptorPool 包含了所有数据
在使用 DescriptorPool 查找时需要使用全名,如:tutorial.Person
在使用 FileDescritor 查找需要使用顶级名字,如 AddressBook,而不是 tutorial.AddressBook
*/
const Descriptor* person_descriptor = importer.pool()->FindMessageTypeByName("tutorial.Person"); /*
使用工厂创建默认 Message,然后构造一个可以用来修改的 Message
这个 Message 的生命周期由 New 调用者管理
*/
const Message* default_person = message_factory.GetPrototype(person_descriptor);
Message* person = default_person->New(); // 使用 Reflection 修改 Message 的数据
const Reflection* reflection = person->GetReflection();
reflection->SetString(person, person_descriptor->FindFieldByName("name"), "abc");
reflection->SetInt32(person, person_descriptor->FindFieldByName("id"), 123456);
reflection->SetString(person, person_descriptor->FindFieldByName("email"), "abc@163.com"); // 把动态设置的 Message 的数据以 JSON 格式输出
util::JsonPrintOptions json_options;
json_options.add_whitespace = true;
json_options.preserve_proto_field_names = true;
string output;
util::MessageToJsonString(*person, &output, json_options);
std::cout << "====== Person data ======" << std::endl;
std::cout << output; // 析构 person
delete person;
}

输出

====== all .proto files ======
syntax = "proto2"; import "person.proto";
package tutorial; message AddressBook {
repeated .tutorial.Person people = 1;
} syntax = "proto2"; package tutorial; message Person {
message PhoneNumber {
optional string number = 1;
optional .tutorial.Person.PhoneType type = 2 [default = HOME];
}
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
repeated .tutorial.Person.PhoneNumber phones = 4;
} ====== Person data ======
{
"name": "abc",
"id": 123456,
"email": "abc@163.com"
}

https://developers.google.com/protocol-buffers/docs/reference/cpp

Protobuf 动态加载 .proto 文件并操作 Message的更多相关文章

  1. Protobuf 动态加载 .pb 文件并操作 Message

    之前写了<Protobuf 动态加载 .proto 文件并操作 Message>.除了直接读取 .proto 文件之外,还有一种类似的方法.先把 .proto 文件编译成 .pb 文件,再 ...

  2. Java_Java中动态加载jar文件和class文件

    转自:http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...

  3. 动态加载JS文件,并根据JS文件的加载状态来执行自己的回调函数

    动态加载JS文件,并根据JS文件的加载状态来执行自己的回调函数, 在很多场景下,我们需要在动态加载JS文件的时候,根据加载的状态来进行后续的操作,需要在JS加载成功后,执行另一方法,这个方法是依托在加 ...

  4. [转载] Java中动态加载jar文件和class文件

    转载自http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...

  5. javascript动态加载js文件主流浏览器兼容版

    一.代码示例: <html> <head> <meta http-equiv="Content-Type" content="text/ht ...

  6. Style样式的四种使用(包括用C#代码动态加载资源文件并设置样式)

    Posted on 2012-03-23 11:21 祥叔 阅读(2886) 评论(6) 编辑 收藏 在Web开发中,我们通过CSS来控制页面元素的样式,一般常用三种方式: 1.       内联样式 ...

  7. Android应用安全之外部动态加载DEX文件风险

    1. 外部动态加载DEX文件风险描述 Android 系统提供了一种类加载器DexClassLoader,其可以在运行时动态加载并解释执行包含在JAR或APK文件内的DEX文件.外部动态加载DEX文件 ...

  8. js动态加载css文件和js文件的方法

    今天研究了下js动态加载js文件和css文件的方法. 网上发现一个动态加载的方法.摘抄下来,方便自己以后使用 [code lang="html"] <html xmlns=& ...

  9. 两种动态加载JavaScript文件的方法

    两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...

随机推荐

  1. SNP 过滤(二)

    本文转载于https://www.jianshu.com/p/e6d5dd774c6e SNP位点过滤 SNP过滤有两种情况,一种是仅根据位点质量信息(测序深度,回帖质量等)对SNP进行粗过滤.如果使 ...

  2. RNA-seq 生物学重复相关性验证

    根据拿到的表达矩阵设为exprSet 1.用scale 进行标准化 数据中心化:数据集中的各个数字减去数据集的均值 数据标准化:中心化之后的数据在除以数据集的标准差. 在R中利用scale方法来对数据 ...

  3. 【R】表达矩阵指定绘制两样本的相关性散点图?

    表达矩阵 要做两两样本的相关性散点图,并计算标明相关系数. 编写函数要点: 直接在aes中传参是不行的 线性回归表达式 函数 方法1:用!!ensym myplot <- function(in ...

  4. R包 tidyverse 分列

    代码: 1 library(tidyverse) 2 separate(data = df,col=chr_pos,into=c("chr","pos"),se ...

  5. nginx——网站显示问题

    一般来说修改3个位置,一个是nginx.h.另一个是ngx_http_header_filter_module.c.还有一个ngx_http_special_response.c. 提示:一般修改都是 ...

  6. 22-reverseString-Leetcode

    思路:so easy class Solution { public: string reverseString(string s) { int n = s.size(); for(int i=0;i ...

  7. Linux搭建yum仓库

    1.安装nginx 2.为nginx搭建共享目录 3.安装createrepo,创建存储库 4.客户端测试 1.安装nginx yum list |grep nginx #查看是否有可用的nginx包 ...

  8. 8.深入TiDB:解析Hash Join实现原理

    本文基于 TiDB release-5.1进行分析,需要用到 Go 1.16以后的版本 我的博客地址:https://www.luozhiyun.com/archives/631 所谓 Hash Jo ...

  9. 使用flock命令查看nas存储是否支持文件锁

    上锁 文件锁有两种 shared lock 共享锁 exclusive lock 排他锁 当文件被上了共享锁之后,其他进程可以继续为此文件加共享锁,但此文件不能被加排他锁,此文件会有一个共享锁计数,加 ...

  10. 14. GLIBCXX_3.4.9' not found - 解决办法

    在Linux中安装交叉编译器arm-linux-gcc 4.4.3,然后编译mini2440内核出错: /usr/lib/libstdc++.so.6: version GLIBCXX_3.4.9' ...