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


边栏推荐
- 破解数字化转型困局,企业分析协同场景案例解析
- MarkDown常用代码片段和工具
- [Deep Learning] Today's bug (August 2)
- 2021年数据泄露成本报告解读
- Spark entry learning-2
- spark入门学习-1
- [QT] Qt project demo: data is displayed on the ui interface, double-click the mouse to display specific information in a pop-up window
- posgresql 到 es 报这个错误 ,啥意思
- 托尔斯泰:生活中只有两种不幸
- 攻防世界----bug
猜你喜欢

【Unity入门计划】基本概念(7)-Input Manager&Input类

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

Introduction to spark learning - 1

spark入门学习-2

使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)

如何分析周活跃率?

Fortinet产品导入AWS AMI操作文档
![[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
![[Deep Learning] Today's bug (August 2)](/img/c5/c4c6d97276bd9997c49ed886aa24cf.png)
[Deep Learning] Today's bug (August 2)

Small Tools(4) 整合Seata1.5.2分布式事务
随机推荐
Windows 事件转发到 SQL 数据库
美国国防部更“青睐”光量子系统研究路线
甲方不让用开源【监控软件】?大不了我自己写一个
leetcode-693.交替位二进制数
生态剧变,电子签名SaaS模式迎来新突破,网络效应加速到来
socket快速理解
使用VS Code搭建ESP-IDF环境
Difference and performance comparison between HAL and LL library of STM32
技术干货|如何将 Pulsar 数据快速且无缝接入 Apache Doris
I am doing open source in Didi
我在滴滴做开源
Introduction to the advantages of the new generation mesh network protocol T-Mesh wireless communication technology
如何选择合适的损失函数,请看......
STM32 GPIO LED and buzzer implementation [Day 4]
泰山OFFICE技术讲座:段落边框的绘制难点在哪里?
Common distributed theories (CAP, BASE) and consensus protocols (Gosssip, Raft)
EA 改口,称单人游戏是产品组合中“非常重要的一部分”
Kubernetes 笔记 / 入门 / 生产环境 / 用部署工具安装 Kubernetes / 用 kubeadm 启动集群 / 安装 kubeadm
一文看懂推荐系统:召回01:基于物品的协同过滤(ItemCF),item-based Collaboration Filter的核心思想与推荐过程
深入浅出Flask PIN