当前位置:网站首页>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;
}


边栏推荐
- 20. Valid Parentheses
- 土耳其国防部:联合协调中心将对首艘乌克兰粮船进行安全检查
- Kubernetes 笔记 / 入门 / 生产环境 / 容器运行时
- 瞌睡检测系统介绍
- leetcode-693.交替位二进制数
- Small Tools(4) 整合Seata1.5.2分布式事务
- 深度学习GPU最全对比,到底谁才是性价比之王?
- Convex Optimization of Optimal Power Flow (OPF) in Microgrids and DC Grids (Matlab Code Implementation)
- 将 Windows 事件日志错误加载到 SQL 表中
- CPU个数_核心数_线程数之间的关系
猜你喜欢

MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
![[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window](/img/3f/265c9d2703056260e03c346fa65a03.png)
[QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window

罗克韦尔AB PLC RSLogix5000中创建新项目、任务、程序和例程的具体方法和步骤

为什么我强烈推荐使用智能化async?

MySQL窗口函数

ModelWhale 云端运行 WRF 中尺度数值气象模式,随时随地即开即用的一体化工作流

spark入门学习-1

一个文件管理系统的软硬件配置清单

MySQL性能优化_小表驱动大表

Not to be ignored!Features and advantages of outdoor LED display
随机推荐
Windows 事件转发到 SQL 数据库
Detailed explanation of ReentrantReadWriteLock
下午见!2022京东云数据库新品发布会
Go Go 简单的很,标准库之 fmt 包的一键入门
ReentrantLock详解
使用VS Code搭建ESP-IDF环境
MySQL窗口函数 OVER()函数介绍
ffplay视频播放原理分析
Yii2安装遇到Loading composer repositories with package information
[微信小程序开发者工具] × #initialize
13 and OOM simulation
为什么我强烈推荐使用智能化async?
EA 改口,称单人游戏是产品组合中“非常重要的一部分”
AI+BI+Visualization, Deep Analysis of Sugar BI Architecture
MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
WordPress 5.2.3 更新,升级出现请求超时的解决方法
AI也有健忘症?英国41岁教授专访:解决灾难性遗忘
Some optional strategies and usage scenarios for PWA application Service Worker caching
uniapp隐藏导航栏和横屏显示设置
一文看懂推荐系统:召回01:基于物品的协同过滤(ItemCF),item-based Collaboration Filter的核心思想与推荐过程