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


边栏推荐
- Prometheus complete process of configuring alertmanager
- Tested interviewed Zuckerberg: reveal more details of four VR prototypes
- 广和通&高通物联网技术开放日成功举办
- 【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】
- Nat experiment demonstration (Huawei switch equipment configuration)
- How can enterprises successfully complete cloud migration?
- Lvs+keepalived high availability deployment practical application
- 图书馆借阅系统「建议收藏」
- 第六七八次作业
- Color finder actual combat (QT including source code)
猜你喜欢

云原生编程挑战赛火热开赛,51 万奖金等你来挑战!

Job CE

Explain the imported 3D model in unity

Unit editor details

Interesting pictures and words

What is data center? What value does the data center bring_ Light spot technology

Space shooting Lesson 10: score (painting and writing)

58岁安徽人,干出瑞士今年最大IPO 投资界

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

It is not only convenient, safe + intelligent, but also beautiful. Fluorite releases the Big Dipper face lock dl30f and Aurora face video lock y3000fv
随机推荐
Unit editor details
Using viewpager to slide through pages in fragment
Two written interview questions about regular
Unity knowledge points summary (1)
Record an error in runtime. Getruntime().Exec ("command")
58岁安徽人,干出瑞士今年最大IPO 投资界
作业 ce
Dom4J的Bug
C language final review questions
Use order by to sort
C # basic 1-events and commissions
MobileViT:挑战MobileNet端侧霸主
Integrating database Ecology: using eventbridge to build CDC applications
Zcmu--5066: dark corridor
Explain the script data transmission and notification in unity
3D laser slam: Interpretation of logo-loam paper - Introduction
[C language brush questions] explanation of linked list application
Explain in detail the rays and radiographic testing in unity
Baklib|为什么说企业需要重视客户体验?
Baklib | why do enterprises need to pay attention to customer experience?