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


边栏推荐
- Lvs+keepalived high availability deployment practical application
- 有奖征文 | 2022 云原生编程挑战赛征稿活动开启
- mysql还有哪些自带的函数呢?别到处找了,看这个就够了。
- 《软件设计师考试》易混淆知识点
- Unity3d tutorial notes - unity initial 04
- 【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD
- Unity3d tutorial notes - unity initial 02
- Color finder actual combat (QT including source code)
- What is "security"? Volvo tells you with its unique understanding and action
- Space shooting lesson 09: elf animation
猜你喜欢

融合数据库生态:利用 EventBridge 构建 CDC 应用

Guo Mingxuan: meta contraction is conducive to the development of VR competitors, and apple XR headshow will change the industry rules

Space shooting Lesson 13: explosion effect

JS chart scatter example

取色器实战(Qt含源码)

Unity foundation 1 - event execution sequence, custom events

Space shooting Lesson 15: props

【云原生】什么是 CI/CD ? | 摆平交付障碍的 CI/CD

MoCo V2:MoCo系列再升级

What is "security"? Volvo tells you with its unique understanding and action
随机推荐
Huawei cloud digital asset chain, "chain" connects the digital economy, infinite splendor
【input 身份证号】星号 代替,input 切割成 多个 小格格(类似)
Understanding of C # delegate
How to build internal Wikipedia
2 enjoy yuan mode
作业 ce
Observer mode, object pool
Baklib | why do enterprises need to pay attention to customer experience?
Space game Lesson 12: shield
Space shooting Lesson 10: score (painting and writing)
Read the recent trends of okaleido tiger and tap the value and potential behind it
Unity foundation 2 editor expansion
查询oracle视图创建语句及如何向视图中插入数据[通俗易懂]
What is "security"? Volvo tells you with its unique understanding and action
Space shooting Lesson 15: props
Why on earth is it not recommended to use select *?
两款吾爱破解优秀软件,批量查找文本,图像视频画质增强
C language function program example (super complete)
Explain in detail the rays and radiographic testing in unity
程序员最大的浪漫~