protobuf c++ 常用API

序列化与反序列化
bool SerializeToString(string* output) const;: serializes the message and stores the bytes in the given string. Note that the bytes are binary, not text; we only use the string class as a convenient container.
bool ParseFromString(const string& data);: parses a message from the given string.
bool SerializeToOstream(ostream* output) const;: writes the message to the given C++ ostream.
bool ParseFromIstream(istream* input);: parses a message from the given C++ istream.
操作protobufAPI
bool IsInitialized() const;: checks if all the required fields have been set.
string DebugString() const;: returns a human-readable representation of the message, particularly useful for debugging.
void CopyFrom(const Person& from);: overwrites the message with the given message's values.
void Clear();: clears all the elements back to the empty state.
常用小技巧

1 一般的get函数的接口跟你的列名一样即get函数 fieldnum,并且返回的是const 类型 。set函数的接口的是set_fieldnum。
2 mutable_fieldnum 如果存在则返回存在的,不存在返回一个空的对象
3 Protobuf 只要fieldnum 不改,即使该列的类型名更改也不影响兼容,老代码同样能兼容新pb文件

更改前
a.proto
message Iner_a {
}
message b {
  optional Iner_a field_name;
}
更改后
message Iner_b {
}
message b {
  optional Iner_b field_name;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值