当前位置:网站首页>protobuf 反射使用总结
protobuf 反射使用总结
2022-08-03 16:05:00 【litanyuan】
背景
反射是指可以动态获取任意类的属性和方法以及动态调用任意对象的属性和方法的机制。C++ 本身没有反射机制,但通过 protobuf 可以在运行时获取和修改对象的字段。
反射相关类
①.Descriptor
Descriptor 包含了对 message 的描述,以及其所有字段的描述。
②.FieldDescriptor
FieldDescriptor 包含了对 message 中单个字段的详细描述。
③.Reflection
Reflection 提供了对 message 中单个字段进行动态读写的方法。
使用反射创建 message
①.概述
DescriptorPool 中存储了所有 message 的元信息;
MessageFactory 是一个实例创建工厂,可以根据 message 的描述信息得到其默认实例;
根据类型的默认实例可以创建同类型的 message 对象;
mesaage 的类型名称要带上其 package 名称。
②.示例 message
message DemoMsg
{
int32 id = 1;
string name = 2;
}
③.使用反射创建 message
auto getMessageByName = [](const string & msgType){
auto desc = google::protobuf::DescriptorPool::generated_pool()->FindMessageTypeByName(msgType);
if (!desc) return shared_ptr<google::protobuf::Message>(nullptr);
auto instance = google::protobuf::MessageFactory::generated_factory()->GetPrototype(desc);
if (!instance) return shared_ptr<google::protobuf::Message>(nullptr);
std::shared_ptr<google::protobuf::Message> msg = std::shared_ptr<google::protobuf::Message>(instance->New());
return msg;
};
string msgType = "protoTest.DemoMsg";
auto msg = getMessageByName(msgType);
if (msg) cout << msgType << " 创建成功" << endl;

使用反射读写字段
①.概述
根据 message 的描述可以得到字段的描述;
使用 message 的反射和字段的描述可以进行字段值的读写。
②.使用反射设置字段值
string msgType = "protoTest.DemoMsg";
auto msg = getMessageByName(msgType);
auto desc = msg->GetDescriptor();
auto refl = msg->GetReflection();
auto field = desc->FindFieldByName("name");
refl->SetString(msg.get(), field, "1234");
cout << msg->DebugString() << endl;

③.使用反射读取字段值
if( refl->HasField(*msg, field) )
cout << refl->GetString(*msg, field) << endl;

使用反射遍历字段
①.概述
使用反射可以在运行时对 message 的所有字段进行遍历。
②.使用反射遍历字段
protoTest::DemoMsg msg;
auto desc = msg.GetDescriptor();
auto refl = msg.GetReflection();
int size = desc->field_count();
for (int i = 0; i < size; ++i)
{
auto field = desc->field(i);
cout << field->name() << " " << field->type_name() << endl;
}


边栏推荐
猜你喜欢

Why do I strongly recommend using smart async?

【899. Ordered Queue】

leetcode-268.丢失的数字

2021年数据泄露成本报告解读

为教育插上数字化的翅膀,网易云信发布「互联网+教育」整体解决方案

【Unity入门计划】基本概念(8)-瓦片地图 TileMap 01

To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
![[微信小程序开发者工具] × #initialize](/img/38/ea90525f53de3933a808f0d75028b0.png)
[微信小程序开发者工具] × #initialize

Small Tools(4) 整合Seata1.5.2分布式事务

QT QT 】 【 to have developed a good program for packaging into a dynamic library
随机推荐
window.open不显示favicon.icon
用友YonSuite与旺店通数据集成对接-技术篇2
CPU个数_核心数_线程数之间的关系
《安富莱嵌入式周报》第276期:2022.07.25--2022.07.31
"Avnet Embedded Weekly" Issue 276: 2022.07.25--2022.07.31
元宇宙系列--Value creation in the metaverse
我写了个”不贪吃蛇“小游戏
面了个腾讯35k出来的,他让我见识到什么叫精通MySQL调优
【Unity入门计划】基本概念(8)-瓦片地图 TileMap 02
devops-2:Jenkins的使用及Pipeline语法讲解
技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
请问大家,MySQL全量怎么样可以提高性能呢?我这里瓶颈是在Source上,在不增加并行度的情况下,
Spark entry learning-2
leetcode SVM
To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
移动应用出海,你的“网络优化”拖后腿了吗?
Small Tools (4) integrated Seata1.5.2 distributed transactions
详谈RDMA技术原理和三种实现方式
生态剧变,电子签名SaaS模式迎来新突破,网络效应加速到来
WordPress建站技术笔记