当前位置:网站首页>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;
}
边栏推荐
- Fortinet产品导入AWS AMI操作文档
- 如何分析周活跃率?
- Kubernetes 笔记 / 入门 / 生产环境 / 容器运行时
- 【深度学习】今日bug(8月2)
- "Avnet Embedded Weekly" Issue 276: 2022.07.25--2022.07.31
- 简易网络传输方法
- Small Tools(4) 整合Seata1.5.2分布式事务
- 【QT】Qt 给已经开发好的程序快速封装成动态库
- Kubernetes 笔记 / 任务 / 管理集群 / 用 kubeadm 管理集群 / 配置一个 cgroup 驱动
- To add digital wings to education, NetEase Yunxin released the overall solution of "Internet + Education"
猜你喜欢
面了个腾讯35k出来的,他让我见识到什么叫精通MySQL调优
Fortinet产品导入AWS AMI操作文档
MATLAB | 七夕节快到了,还不给朋友安排上这个咕呱小青蛙?
Cookie和Session的关系
Why do I strongly recommend using smart async?
使用Make/CMake编译ARM裸机程序(基于HT32F52352 Cortex-M0+)
攻防世界----bug
为什么我强烈推荐使用智能化async?
Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)
不可忽略!户外LED显示屏的特点及优势
随机推荐
详谈RDMA技术原理和三种实现方式
如何启动 NFT 集合
我写了个”不贪吃蛇“小游戏
ModelWhale 云端运行 WRF 中尺度数值气象模式,随时随地即开即用的一体化工作流
请问下,flink cdc监控oracle,我看源码是通过sid方式的,请问怎么改成service
全新探险者以40万的产品击穿豪华SUV价格壁垒
一文看懂推荐系统:概要02:推荐系统的链路,从召回粗排,到精排,到重排,最终推荐展示给用户
小熊派——无线联网开发
Yii2安装遇到Loading composer repositories with package information
我在滴滴做开源
移动应用出海,你的“网络优化”拖后腿了吗?
带你了解什么是 Web3.0
Research on power flow in DC microgrid based on Newton's method (Matlab code implementation)
【翻译】关于扩容一个百万级别用户系统的六个课程
"Avnet Embedded Weekly" Issue 276: 2022.07.25--2022.07.31
CPU个数_核心数_线程数之间的关系
一文看懂推荐系统:召回02:Swing 模型,和itemCF很相似,区别在于计算相似度的方法不一样
Small Tools (4) integrated Seata1.5.2 distributed transactions
QT QT 】 【 to have developed a good program for packaging into a dynamic library
uniapp的webview滑动缩放