当前位置:网站首页>Protobuf compound data types, speaking, reading and writing
Protobuf compound data types, speaking, reading and writing
2022-07-30 04:42:00 【litanyuan】
背景
protobuf 在生成的 C++ 代码中为 .proto 文件中的每个 message 生成了对应的 C++ 类,并提供了数据成员的读写方法.
message 类型读写
①.message 示例
message Point
{
double lng = 1;
double lat = 2;
}
message DemoMsg
{
int32 id = 1;
Point pos = 2;
}
②.成员赋值
通过 mutable_xxx The method returns a pointer to member assignment:
protoTest::DemoMsg msg;
msg.set_id(100);
msg.mutable_pos()->set_lng(112.56);
msg.mutable_pos()->set_lat(56.78);
cout << msg.DebugString() << endl;

通过 copyFrom Method uses struct assignment:
protoTest::DemoMsg msg;
msg.set_id(100);
protoTest::Point m_p;
m_p.set_lng(112.56);
m_p.set_lat(56.78);
msg.mutable_pos()->CopyFrom(m_p);
cout << msg.DebugString() << endl;
通过 set_allocated_xxx Member functions pass in pointers for assignment:
protoTest::DemoMsg msg;
msg.set_id(100);
protoTest::Point * m_p = new protoTest::Point;
m_p->set_lng(112.56);
m_p->set_lat(56.78);
msg.set_allocated_pos(m_p);
cout << msg.DebugString() << endl;
③.成员读取
通过成员函数 has_xxx 可以判断 message whether a member of a type is assigned a value.
protoTest::Point* m_p = new protoTest::Point;
m_p->set_lng(112.56);
m_p->set_lat(56.78);
protoTest::DemoMsg msg;
msg.set_id(100);
msg.set_allocated_pos(m_p);
if (msg.has_pos())
{
auto& pos = msg.pos();
cout << pos.lng() << " " << pos.lat();
}

map 类型读写
①.message 示例
message DemoMsg
{
map<int32,string> a = 1;
}
②.成员赋值
map Type member assignment must pass mutable_xxx 方法进行:
protoTest::DemoMsg msg;
msg.mutable_a()->insert({
1,"aaaaa" });
msg.mutable_a()->insert({
2,"bbbbb" });
msg.mutable_a()->insert({
3,"ccccc" });
cout << msg.DebugString() << endl;

③.成员读取
protoTest::DemoMsg msg;
msg.mutable_a()->insert({
1,"aaaaa" });
msg.mutable_a()->insert({
2,"bbbbb" });
auto map = msg.mutable_a();
if (map->contains(1))
{
cout << map->at(1) << endl;
}

any 类型读写
①.概述
any Can store serialized data of any known type,Often used for repackaging of struct data,that can combine different types of message packaged in the same message 中进行传输.
②.message 示例
syntax = "proto3";
import "google/protobuf/any.proto";
package protoTest;
message Point
{
double lng = 1;
double lat = 2;
}
message Node
{
int32 id = 1;
string name = 2;
}
message DemoMsg
{
int32 id = 1;
google.protobuf.Any data = 2;
}
③.成员赋值
使用 any 类型的成员函数 PackFrom 来进行赋值.
protoTest::Point m_p;
m_p.set_lng(112.56);
m_p.set_lat(56.78);
protoTest::DemoMsg msg;
msg.set_id(1);
msg.mutable_data()->PackFrom(m_p);
cout << msg.DebugString() << endl;

④.message 类型判断
通过成员函数 Is Can determine whether it is a specified type of data.
protoTest::Point m_p;
m_p.set_lng(112.56);
m_p.set_lat(56.78);
protoTest::Node m_node;
m_node.set_id(1);
m_node.set_name("Zhangsan");
protoTest::DemoMsg msg1;
msg1.set_id(1);
msg1.mutable_data()->PackFrom(m_p);
protoTest::DemoMsg msg2;
msg2.set_id(2);
msg2.mutable_data()->PackFrom(m_node);
auto checkAny = [&](const protoTest::DemoMsg& msg) {
if (msg.data().Is<protoTest::Point>())
cout << " msgID:" << msg.id() << " 中 data 类型为 Point " << endl;
else if( msg.data().Is<protoTest::Node>() )
cout << " msgID:" << msg.id() << " 中 data 类型为 Node " << endl;
};
checkAny(msg1);
checkAny(msg2);

⑤.成员读取
通过成员函数 UnpackTo 可以把 any The data is restored to the corresponding structure data.
protoTest::Point m_p;
m_p.set_lng(112.56);
m_p.set_lat(56.78);
protoTest::DemoMsg msg;
msg.set_id(1);
msg.mutable_data()->PackFrom(m_p);
if (msg.data().Is<protoTest::Point>())
{
protoTest::Point pos;
if (msg.data().UnpackTo(&pos))
{
cout << pos.DebugString() << endl;
}
}

oneof 类型读写
①.概述
oneof 类型同 C++ 中的 union 类型,Only one type is valid at a time.
②.message 示例
message DemoMsg
{
oneof test{
int32 id = 1;
string name =2;
};
}
③.成员赋值
直接使用 set_xxx To the appropriate members of the assignment,but only the last assigned data is valid.
protoTest::DemoMsg msg;
msg.set_id(100);
msg.set_name("123");
cout << msg.DebugString() << endl;
④.数据类型判断
通过 has_xxx Method to determine whether the corresponding type member is assigned a value:
protoTest::DemoMsg msg;
msg.set_id(100);
msg.set_name("123");
if (msg.has_name())
{
cout << "msg 中为 string 类型" << endl;
}

通过 xxx_case The method currently judges the saved member type:
protoTest::DemoMsg msg;
msg.set_id(100);
msg.set_name("123");
switch (msg.test_case())
{
case protoTest::DemoMsg::kId:
cout << "msg 中为 int32 类型" << endl; break;
case protoTest::DemoMsg::kName:
cout << "msg 中为 string 类型" << endl; break;
default:
break;
}
⑤.成员读取
To determine the current first save the field type,再读取数据.
protoTest::DemoMsg msg;
msg.set_id(100);
msg.set_name("123");
switch (msg.test_case())
{
case protoTest::DemoMsg::kId:
cout << msg.id() << endl; break;
case protoTest::DemoMsg::kName:
cout << msg.name() << endl; break;
default:
break;
}


边栏推荐
- Weight line segment tree + line segment tree split/merge + CF1659D
- 【线性表】- LeetCode力扣三道练习题详解
- DAY17, CSRF vulnerability
- VisualStudio2022本地调试进入特别慢问题解决
- MySQL installation error solution
- Xiamen SenseCore Technology MC3172(1): Introduction and Environment Construction
- VUX Datetime 组件compute-days-function动态设置日期列表
- Shanxi group (enterprises) in the second network security skills competition part problem WP (8)
- 软件测试员必看!数据库知识mysql查询语句大全
- [MRCTF2020]Hello_misc
猜你喜欢

Code open source design and implementation ideas

nSoftware.PowerShell.Server.2020

VisualStudio2022本地调试进入特别慢问题解决

如何与墨西哥大众VW Mexico建立EDI连接

GCC Rust获批将被纳入主线代码库,或将于GCC 13中与大家见面

How does MySql find out the latest data row that meets the conditions?

1. Get data - requests.get()

六、读取应用配置+日志配置
![[C language] Program environment and preprocessing](/img/d2/a0437da7d651e755951dd2a15827cf.png)
[C language] Program environment and preprocessing

Repetition XXL - JOB scheduling center background arbitrary command execution
随机推荐
The Double Pointer Problem (Part 2)
七、自定义配置
Naive Bayes Classification
Discourse Custom Header Links
handler+message [message mechanism]
文件系统二
需求设计文档和产品经理的角色改变
小程序 wx.miniProgram.navigateTo 跳转地址不能是tabbar地址
Chapter8 Support Vector Machines
[MRCTF2020]Hello_misc
2.6归并排序
Web page element parsing a tag
[MRCTF2020]Hello_ misc
Learning of redis_Basic part
SVN 查看用户名密码
Requirements design document and the changing role of the product manager
DAY17: weak password detection and test
A must see for software testers!Database knowledge MySQL query statement Daquan
字符串问题(下)
1. 获取数据-requests.get()