2 protobuf proto3语言指南

2 protobuf proto3语言指南

2.1 导入定义(import)

Import 可选项用于包含其它 proto 文件中定义的 message或 enum等。标准格式如 下

1
import "info.proto";

info.proto文件内容如下:

1
2
3
4
5
6
7
8
9
syntax = "proto3";//指定版本信息,不指定会报错

package infopack; //package声明符

message info //message为关键字,作用为定义一种消息类型
{
string addr = 1; //地址
string group = 2; //分组
}

addressbook.proto文件内容如下,addressbook.proto文件需要导入info.proto文件的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
syntax = "proto3";//指定版本信息,不指定会报错

import "info.proto"; //导入定义

package tutorial; //package声明符

message Person //message为关键字,作用为定义一种消息类型
{
string name = 1; //姓名
int32 id = 2; //id
string email = 3; //邮件

enum PhoneType //枚举消息类型
{
MOBILE = 0; //proto3版本中,首成员必须为0,成员不应有相同的值
HOME = 1;
WORK = 2;
}

message PhoneNumber
{
string number = 1;
PhoneType type = 2;
}

repeated PhoneNumber phones = 4; //phones为数组

//info定义在"info.proto"
//类型格式:包名.信息名
infopack.info tmp = 5;
}

message AddressBook
{
repeated Person people = 1;
}

编译proto文件

1
protoc -I=./ --cpp_out=./ *.proto

测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "addressbook.pb.h"
#include <iostream>
#include <fstream>
using namespace std;

void set_addressbook()
{
tutorial::AddressBook obj;

tutorial::Person *p1 = obj.add_people(); //新增加一个Person
p1->set_name("mike");
p1->set_id(1);
p1->set_email("mike@qq.com");

tutorial::Person::PhoneNumber *phone1 = p1->add_phones(); //增加一个phone
phone1->set_number("110");
phone1->set_type(tutorial::Person::MOBILE);

tutorial::Person::PhoneNumber *phone2 = p1->add_phones(); //增加一个phone
phone2->set_number("120");
phone2->set_type(tutorial::Person::HOME);

//info addr和group的使用
infopack::info *p_info = p1->mutable_tmp(); //取出info的对象指针
p_info->set_addr("China"); //地址
p_info->set_group("A"); //组

fstream output("pb.xxx", ios::out | ios::trunc | ios::binary);

bool flag = obj.SerializeToOstream(&output);//序列化
if (!flag)
{
cerr << "Failed to write file." << endl;
return;
}

output.close();//关闭文件
}

void get_addressbook()
{
tutorial::AddressBook obj;
fstream input("./pb.xxx", ios::in | ios::binary);
obj.ParseFromIstream(&input); //反序列化
input.close(); //关闭文件

for (int i = 0; i < obj.people_size(); i++)
{
const tutorial::Person& person = obj.people(i);//取第i个people
cout << "第" << i + 1 << "个信息\n";
cout << "name = " << person.name() << endl;
cout << "id = " << person.id() << endl;
cout << "email = " << person.email() << endl;

for (int j = 0; j < person.phones_size(); j++)
{
const tutorial::Person::PhoneNumber& phone_number = person.phones(j);

switch (phone_number.type())
{
case tutorial::Person::MOBILE:
cout << " Mobile phone #: ";
break;
case tutorial::Person::HOME:
cout << " Home phone #: ";
break;
case tutorial::Person::WORK:
cout << " Work phone #: ";
break;
}

cout << phone_number.number() << endl;
}

//info addr和group的使用
infopack::info info = person.tmp(); //取出info的对象
cout << "addr = " << info.addr() << endl;
cout << "group = " << info.group() << endl;

cout << endl;
}
}

int main()
{
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;

set_addressbook(); //序列化
get_addressbook(); //反序列化

// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();

return 0;
}