ProtoBuf
5. proto3语法详解
5.5 oneof类型
如果消息中有很多可选字段,但是将来只会有一个字段被设置,那么就可以利用oneof类型加强这一行为,也能有节约内存的效果。
我们定义了一个名为 other_contact 的 oneof 字段,其中包含两个可选字段:qq 和 weixin。这两个字段只能有一个会被设置。
oneof other_contact
{ // 其他联系⽅式:多选⼀
string qq = 1;
string weixin = 2;
}
同时也要注意:
可选字段的编号不可与非可选字段编号相同,以防冲突。
oneof 中不能使用 repeated 字段。
未来设置 oneof 字段值时,若设置多个,仅保留最后一次设置的成员,之前设置的会自动清除。
我们向我们的通讯录中添加oneof类型作为一种联系方式选择:
syntax = "proto3";
package contacts;
import "google/protobuf/any.proto"; // 引⼊ any.proto ⽂件
// 地址
message Address
{
string home_address = 1; // 家庭地址
string unit_address = 2; // 单位地址
}
// 联系⼈
message PeopleInfo
{
string name = 1; // 姓名
int32 age = 2; // 年龄
message Phone
{
string number = 1; // 电话号码
enum PhoneType
{
MP = 0; // 移动电话
TEL = 1; // 固定电话
}
PhoneType type = 2; // 类型
}
repeated Phone phone = 3; // 电话
google.protobuf.Any data = 4;
oneof other_contact
{ // 其他联系⽅式:多选⼀
string qq = 5;
string weixin = 6;
}
}
// 通讯录
message Contacts
{
repeated PeopleInfo contacts = 1;
}
protoc编译文件:
protoc --cpp_out=. contacts.proto
查看contacts.pb.h文件,这就是自动生成的有关oneo的函数:
判断类:
_internal_has_qq
:判断是否设置了 qq 字段。
has_qq
:通过调用 _internal_has_qq 获取是否设置了 qq 字段的信息。
设置类:
set_has_qq
:设置 _oneof_case _ [0] 为 kQq 。
set_qq
:在未设置 qq 时进行清理和初始化操作,然后设置 qq 的值。
_internal_set_qq
:设置 qq 的值。
set_allocated_qq
:根据传入的 qq 指针进行相应的设置和清理操作。
清除类:
clear_qq
:在 _internal_has_qq 为真时,销毁 qq 字段并清除相关设置。
获取类:
qq
:获取 qq 字段的值。
mutable_qq
:获取可修改的 qq 字段。
_internal_qq
:在设置了 qq 时获取其值,否则返回空字符串。
_internal_mutable_qq
:在未设置 qq 时进行清理和初始化,然后返回可修改的 qq 字段。
release_qq
:在设置了 qq 时释放并清除相关设置,未设置时返回空指针。
更新 write.cc :
#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
* 新增联系⼈
*/
void AddPeopleInfo(PeopleInfo *people_info_ptr)
{
cout << "-------------新增联系⼈-------------" << endl;
cout << "请输⼊联系⼈姓名: ";
string name;
getline(cin, name);
people_info_ptr->set_name(name);
cout << "请输⼊联系⼈年龄: ";
int age;
cin >> age;
people_info_ptr->set_age(age);
cin.ignore(256, '\n');
for (int i = 1;; i++)
{
cout << "请输⼊联系⼈电话" << i << "(只输⼊回⻋完成电话新增): ";
string number;
getline(cin, number);
if (number.empty())
{
break;
}
PeopleInfo_Phone *phone = people_info_ptr->add_phone();
phone->set_number(number);
cout << "选择此电话类型 (1、移动电话 2、固定电话) : ";
int type;
cin >> type;
cin.ignore(256, '\n');
switch (type)
{
case 1:
phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
break;
case 2:
phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
break;
default:
cout << "⾮法选择,使⽤默认值!" << endl;
break;
}
}
Address address;
cout << "请输⼊联系⼈家庭地址: ";
string home_address;
getline(cin, home_address);
address.set_home_address(home_address);
cout << "请输⼊联系⼈单位地址: ";
string unit_address;
getline(cin, unit_address);
address.set_unit_address(unit_address);
google::protobuf::Any *data = people_info_ptr->mutable_data();
data->PackFrom(address);
cout << "选择添加⼀个其他联系⽅式 (1、qq号 2、微信号) : ";
int other_contact;
cin >> other_contact;
cin.ignore(256, '\n');
if (1 == other_contact)
{
cout << "请输⼊qq号: ";
string qq;
getline(cin, qq);
people_info_ptr->set_qq(qq);
}
else if (2 == other_contact)
{
cout << "请输⼊微信号: ";
string weixin;
getline(cin, weixin);
people_info_ptr->set_weixin(weixin);
}
else
{
cout << "⾮法选择,该项设置失败!" << endl;
}
cout << "-----------添加联系⼈成功-----------" << endl;
}
int main(int argc, char *argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " CONTACTS_FILE" << endl;
return -1;
}
Contacts contacts;
// 先读取已存在的 contacts
fstream input(argv[1], ios::in | ios::binary);
if (!input)
{
cout << argv[1] << ": File not found. Creating a new file." << endl;
}
else if (!contacts.ParseFromIstream(&input))
{
cerr << "Failed to parse contacts." << endl;
input.close();
return -1;
}
// 新增⼀个联系⼈
AddPeopleInfo(contacts.add_contacts());
// 向磁盘⽂件写⼊新的 contacts
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!contacts.SerializeToOstream(&output))
{
cerr << "Failed to write contacts." << endl;
input.close();
output.close();
return -1;
}
input.close();
output.close();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
更新 read.cc :
#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
* 打印联系⼈列表
*/
void PrintfContacts(const Contacts &contacts)
{
for (int i = 0; i < contacts.contacts_size(); ++i)
{
const PeopleInfo &people = contacts.contacts(i);
cout << "------------联系⼈" << i + 1 << "------------" << endl;
cout << "姓名:" << people.name() << endl;
cout << "年龄:" << people.age() << endl;
int j = 1;
for (const PeopleInfo_Phone &phone : people.phone())
{
cout << "电话" << j++ << ": " << phone.number();
cout << " (" << phone.PhoneType_Name(phone.type()) << ")" << endl;
}
if (people.has_data() && people.data().Is<Address>())
{
Address address;
people.data().UnpackTo(&address);
if (!address.home_address().empty())
{
cout << "家庭地址:" << address.home_address() << endl;
}
if (!address.unit_address().empty())
{
cout << "单位地址:" << address.unit_address() << endl;
}
}
/* if (people.has_qq()) {
} else if (people.has_weixin()) {
} */
switch (people.other_contact_case())
{
case PeopleInfo::OtherContactCase::kQq:
cout << "qq号: " << people.qq() << endl;
break;
case PeopleInfo::OtherContactCase::kWeixin:
cout << "微信号: " << people.weixin() << endl;
break;
case PeopleInfo::OtherContactCase::OTHER_CONTACT_NOT_SET:
break;
}
}
}
int main(int argc, char *argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << "CONTACTS_FILE" << endl;
return -1;
}
// 以⼆进制⽅式读取 contacts
Contacts contacts;
fstream input(argv[1], ios::in | ios::binary);
if (!contacts.ParseFromIstream(&input))
{
cerr << "Failed to parse contacts." << endl;
input.close();
return -1;
}
// 打印 contacts
PrintfContacts(contacts);
input.close();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
makfile编译:
all:write read
write:write.cc contacts.pb.cc
g++ -o $@ $^ -std=c++11 -lprotobuf
read:read.cc contacts.pb.cc
g++ -o $@ $^ -std=c++11 -lprotobuf
.PHONY:clean
clean:
rm -f write read
5.6 map类型
语法支持创建⼀个关联映射字段,也就是可以使用map类型去声明字段类型,格式为:
map<key_type, value_type> map_field = N;
注意:
key_type 可以是除 float 和 bytes 类型之外的任何标量类型,如 int 、 string 、 bool 等。
value_type 则没有太多限制,可以是任何类型,包括自定义类型、其他复合类型等。
map 字段不能用 repeated 修饰,所以不能写成类似于 repeated map<int, string> user_ages 这样的形式。
map 中存储的元素是无序的。
我们可以这样定义备注字段类型:
map<string, string> remark = 7; // 备注
我们向我们的通讯录中添加map类型作为备注:
protoc编译文件:
protoc --cpp_out=. contacts.proto
查看contacts.pb.h文件,这就是自动生成的有关map的函数:
与 remark 相关的函数:
_internal_remark_size
:获取 remark 的大小。
remark_size
:通过调用 _internal_remark_size 获取 remark 的大小。
clear_remark
:清空 remark 。
_internal_remark
:获取 remark 的内部映射。
remark
:通过特定的插入点获取 remark 的内部映射。
_internal_mutable_remark
:获取可修改的 remark 映射。
mutable_remark
:通过特定的插入点获取可修改的 remark 映射。
更新 write.cc :
#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
* 新增联系⼈
*/
void AddPeopleInfo(PeopleInfo *people_info_ptr)
{
cout << "-------------新增联系⼈-------------" << endl;
cout << "请输⼊联系⼈姓名: ";
string name;
getline(cin, name);
people_info_ptr->set_name(name);
cout << "请输⼊联系⼈年龄: ";
int age;
cin >> age;
people_info_ptr->set_age(age);
cin.ignore(256, '\n');
for (int i = 1;; i++)
{
cout << "请输⼊联系⼈电话" << i << "(只输⼊回⻋完成电话新增): ";
string number;
getline(cin, number);
if (number.empty())
{
break;
}
PeopleInfo_Phone *phone = people_info_ptr->add_phone();
phone->set_number(number);
cout << "选择此电话类型 (1、移动电话 2、固定电话) : ";
int type;
cin >> type;
cin.ignore(256, '\n');
switch (type)
{
case 1:
phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_MP);
break;
case 2:
phone -> set_type(PeopleInfo_Phone_PhoneType::PeopleInfo_Phone_PhoneType_TEL);
break;
default:
cout << "⾮法选择,使⽤默认值!" << endl;
break;
}
}
Address address;
cout << "请输⼊联系⼈家庭地址: ";
string home_address;
getline(cin, home_address);
address.set_home_address(home_address);
cout << "请输⼊联系⼈单位地址: ";
string unit_address;
getline(cin, unit_address);
address.set_unit_address(unit_address);
google::protobuf::Any *data = people_info_ptr->mutable_data();
data->PackFrom(address);
cout << "选择添加⼀个其他联系⽅式 (1、qq号 2、微信号) : ";
int other_contact;
cin >> other_contact;
cin.ignore(256, '\n');
if (1 == other_contact)
{
cout << "请输⼊qq号: ";
string qq;
getline(cin, qq);
people_info_ptr->set_qq(qq);
}
else if (2 == other_contact)
{
cout << "请输⼊微信号: ";
string weixin;
getline(cin, weixin);
people_info_ptr->set_weixin(weixin);
}
else
{
cout << "⾮法选择,该项设置失败!" << endl;
}
for (int i = 1;; i++)
{
cout << "请输⼊备注" << i << "标题 (只输⼊回⻋完成备注新增): ";
string remark_key;
getline(cin, remark_key);
if (remark_key.empty())
{
break;
}
cout << "请输⼊备注" << i << "内容: ";
string remark_value;
getline(cin, remark_value);
people_info_ptr->mutable_remark()->insert({remark_key, remark_value});
}
cout << "-----------添加联系⼈成功-----------" << endl;
}
int main(int argc, char *argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << " CONTACTS_FILE" << endl;
return -1;
}
Contacts contacts;
// 先读取已存在的 contacts
fstream input(argv[1], ios::in | ios::binary);
if (!input)
{
cout << argv[1] << ": File not found. Creating a new file." << endl;
}
else if (!contacts.ParseFromIstream(&input))
{
cerr << "Failed to parse contacts." << endl;
input.close();
return -1;
}
// 新增⼀个联系⼈
AddPeopleInfo(contacts.add_contacts());
// 向磁盘⽂件写⼊新的 contacts
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!contacts.SerializeToOstream(&output))
{
cerr << "Failed to write contacts." << endl;
input.close();
output.close();
return -1;
}
input.close();
output.close();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
更新 read.cc :
#include <iostream>
#include <fstream>
#include "contacts.pb.h"
using namespace std;
using namespace contacts;
/**
* 打印联系⼈列表
*/
void PrintfContacts(const Contacts &contacts)
{
for (int i = 0; i < contacts.contacts_size(); ++i)
{
const PeopleInfo &people = contacts.contacts(i);
cout << "------------联系⼈" << i + 1 << "------------" << endl;
cout << "姓名:" << people.name() << endl;
cout << "年龄:" << people.age() << endl;
int j = 1;
for (const PeopleInfo_Phone &phone : people.phone())
{
cout << "电话" << j++ << ": " << phone.number();
cout << " (" << phone.PhoneType_Name(phone.type()) << ")" << endl;
}
if (people.has_data() && people.data().Is<Address>())
{
Address address;
people.data().UnpackTo(&address);
if (!address.home_address().empty())
{
cout << "家庭地址:" << address.home_address() << endl;
}
if (!address.unit_address().empty())
{
cout << "单位地址:" << address.unit_address() << endl;
}
}
/* if (people.has_qq()) {
} else if (people.has_weixin()) {
} */
switch (people.other_contact_case())
{
case PeopleInfo::OtherContactCase::kQq:
cout << "qq号: " << people.qq() << endl;
break;
case PeopleInfo::OtherContactCase::kWeixin:
cout << "微信号: " << people.weixin() << endl;
break;
case PeopleInfo::OtherContactCase::OTHER_CONTACT_NOT_SET:
break;
}
if (people.remark_size())
{
cout << "备注信息: " << endl;
}
for (auto it = people.remark().cbegin(); it != people.remark().cend();
++it)
{
cout << " " << it->first << ": " << it->second << endl;
}
}
}
int main(int argc, char *argv[])
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2)
{
cerr << "Usage: " << argv[0] << "CONTACTS_FILE" << endl;
return -1;
}
// 以⼆进制⽅式读取 contacts
Contacts contacts;
fstream input(argv[1], ios::in | ios::binary);
if (!contacts.ParseFromIstream(&input))
{
cerr << "Failed to parse contacts." << endl;
input.close();
return -1;
}
// 打印 contacts
PrintfContacts(contacts);
input.close();
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
makfile编译:
all:write read
write:write.cc contacts.pb.cc
g++ -o $@ $^ -std=c++11 -lprotobuf
read:read.cc contacts.pb.cc
g++ -o $@ $^ -std=c++11 -lprotobuf
.PHONY:clean
clean:
rm -f write read
5.7 默认值
对于 proto3 的语法,message 中的字段默认由 singular 修饰。当被 singular 修饰的字段在序列化时未被设置值,protobuf 的序列化方法不会对该字段进行编码。同理,在反序列化时,若在反序列化序列中未找到 message 中某一字段的值,protobuf 则会用该字段的默认值来填充此字段。
在反序列化消息时,不同类型字段在二进制序列中未包含时所采用的默认值规则:
类型 | 默认值 |
---|---|
字符串 | 默认空字符串 |
字节 | 默认空字节 |
布尔值 | 默认 false |
数值类型 | 默认 0 |
枚举 | 默认第一个定义且值为 0 的枚举值 |
消息字段 | 未设置 |
repeated | 字段默认空列表 |
对于 消息字段 、 oneof 字段 和 any 字段 | 在 C++ 和 Java 中可以通过 has_ 方法检测是否被设置 |