当前位置:网站首页>protobuf 中基础数据类型的读写
protobuf 中基础数据类型的读写
2022-07-28 19:13:00 【litanyuan】
背景
protobuf 在生成的 C++ 代码中为 .proto 文件中的每个 message 生成了对应的 C++ 类,并提供了数据成员的读写方法。
生成的 C++ 类
①.类名
protobuf 在生成的 C++ 代码中为 .proto 文件中的每个 message 生成了对应的 C++ 类,其类名和 message 名称一样。
②.默认值
protobuf 生成的 C++ 类会为没有复制的字段设置默认值:数字类型默认值是 0 ;字符串类型默认值是空字符串;bool 类型默认是 false ;枚举类型默认为第一个值
③.clear 方法
执行其成员函数 clear 会把结构体的所有成员清空,即恢复到默认值状态。
④.DebugString 方法
调用类的成员函数 DebugString 可以把结构体转换成可打印及可识别的文本字符串,以方便代码调试。
数字类型读写
①.message 示例
message DemoMsg
{
int32 a = 1;
double b = 2;
}
②.成员赋值
生成的类中成员的赋值函数名称是 set_XXX。
protoTest::DemoMsg msg;
msg.set_a(10);
msg.set_b(0.25);
cout << msg.DebugString() << endl;

③.成员读取
生成的类中成员的读取方法是成员的同名函数 XXX()。
protoTest::DemoMsg msg;
msg.set_a(10);
msg.set_b(0.25);
cout << "a " << msg.a() << endl;
cout << "b " << msg.b() << endl;

④.成员清空
使用 clear_xxx 方法,可以清空对应的成员,即把该成员还原成默认值。
protoTest::DemoMsg msg;
msg.set_a(10);
msg.set_b(0.25);
msg.clear_b();
cout << "a " << msg.a() << endl;
cout << "b " << msg.b() << endl;
文本类型读写
①.message 示例
message DemoMsg
{
string a = 1;
bytes b = 2;
}
②.成员赋值
使用 set 方法赋值:
protoTest::DemoMsg msg;
msg.set_a("1234");
msg.set_b("测试");
cout << msg.DebugString() << endl;
使用 mutable 方法赋值,mutable_xxx 方法获取成员的指针:
protoTest::DemoMsg msg;
msg.set_a("1234");
msg.mutable_b()->assign("测试");
cout << msg.DebugString() << endl;

使用 set_allocated 方法赋值,set_allocated _xxx 负责释放传入的指针:
protoTest::DemoMsg msg;
msg.set_a("1234");
string* s = new string("测试");
msg.set_allocated_b(s);
cout << msg.DebugString() << endl;
③.成员读取
字符串成员的读取也可以使用 mutable_xxx 方法:
protoTest::DemoMsg msg;
msg.set_a("1234");
msg.set_b("测试");
cout << "a " << msg.a() << endl;
cout << "b " << *msg.mutable_b() << endl;

数组类型读写
①.message 示例
message DemoMsg
{
repeated double a = 1;
}
②.添加新值
repeated 标记的成员使用 add_xxx 添加新的值:
protoTest::DemoMsg msg;
msg.add_a(0.2);
msg.add_a(0.5);
cout << msg.DebugString() << endl;

③.修改指定元素
repeated 标记的成员使用 set_xxx 方法修改指定位置的元素值,下标从 0 开始。
protoTest::DemoMsg msg;
msg.add_a(0.2);
msg.add_a(0.5);
msg.set_a(1, 0.9);
cout << msg.DebugString() << endl;

④.元素遍历
使用 xxx_size() 可以获取 repeated 标记的成员的个数:
protoTest::DemoMsg msg;
msg.add_a(0.2);
msg.add_a(0.5);
int size = msg.a_size();
for (int i = 0; i < size; ++i)
{
cout << msg.a(i) << " ";
}

⑤.元素删除
使用 mutable_xxx 方法获取成员的指针,可以删除指定位置的元素:
protoTest::DemoMsg msg;
msg.add_a(0.2);
msg.add_a(0.5);
msg.mutable_a()->erase(msg.mutable_a()->begin());
cout << msg.DebugString() << endl;

枚举类型读写
①.message 示例
enum demoEnum
{
A = 0;
B = 1;
C = 2;
}
message DemoMsg
{
demoEnum a = 1;
}
②.成员赋值
使用枚举值直接赋值:
protoTest::DemoMsg msg;
msg.set_a(protoTest::B);
cout << msg.DebugString() << endl;
使用整形数字赋值:
protoTest::DemoMsg msg;
msg.set_a(static_cast<protoTest::demoEnum>(2));
cout << msg.DebugString() << endl;

③.成员读取
protoTest::DemoMsg msg;
msg.set_a(protoTest::B);
auto a = msg.a();
cout << a << endl;


边栏推荐
- Space shooting Lesson 13: explosion effect
- PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
- Space game Lesson 12: shield
- source insight 使用快捷键
- 什么是低代码?哪些平台适合业务人员?用来开发系统靠不靠谱?
- C # basic 6-file IO and JSON
- Unity knowledge points summary (1)
- 3D laser slam: Interpretation of logo-loam paper - Introduction
- Baklib | why do enterprises need to pay attention to customer experience?
- 十七年运维老兵万字长文讲透优维低代码~
猜你喜欢

H5 wechat shooting game source code

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

JS page black and white background switch JS special effect

Easynlp Chinese text and image generation model takes you to become an artist in seconds

研发效能的思考总结

EasyNLP中文文图生成模型带你秒变艺术家

Explain the imported 3D model in unity

The EMC vnx5200 fault light is on, but there is no hardware fault prompt

4.2 Virtual Member Functions

Explain mesh Collider in unity
随机推荐
Introduction to redis I: redis practical reading notes
Introduction to singleton mode
Random talk on GIS data (VI) - projection coordinate system
Network layer performance test
Explain the imported 3D model in unity
C language final review questions
2 enjoy yuan mode
PostgreSQL数据库删库前是不是需要把所有连接断开才能删除?
C # basic 6-file IO and JSON
How do we do full link grayscale on the database?
mysql梳理复习内容--附思维导图
Meaning of disk status of EMC DataDomain
4.1 Member的各种调用方式
[C language brush questions] explanation of linked list application
什么是 CI/CD? | 实现更快更好的软件交付
H5 wechat shooting game source code
Space shooting Lesson 11: sound and music
Unity foundation 3- data persistence
既要便捷、安全+智能,也要颜值,萤石发布北斗星人脸锁DL30F和极光人脸视频锁Y3000FV
Explain the camera in unity and its application