protobuf入門教程(五):列舉(enum)、包(package)
阿新 • • 發佈:2018-11-15
列舉(enum)
訊息格式
當需要定義一個訊息型別的時候,可能想為一個欄位指定某“預定義值序列”中的一個值,這時候可以通過列舉實現。
syntax = "proto3";//指定版本資訊,不指定會報錯 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為陣列 }message AddressBook { repeated Person people = 1; }
測試程式
void set_addressbook() { AddressBook obj; Person *p1 = obj.add_people(); //新增加一個Person p1->set_name("mike"); p1->set_id(1); p1->set_email("[email protected]"); Person::PhoneNumber *phone1 = p1->add_phones(); //增加一個phone phone1->set_number("110"); phone1->set_type(Person::MOBILE); Person::PhoneNumber *phone2 = p1->add_phones(); //增加一個phone phone2->set_number("120"); phone2->set_type(Person::HOME); 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() { 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 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 Person::PhoneNumber& phone_number = person.phones(j); switch (phone_number.type()) { case Person::MOBILE: cout << " Mobile phone #: "; break; case Person::HOME: cout << " Home phone #: "; break; case Person::WORK: cout << " Work phone #: "; break; } cout << phone_number.number() << endl; } cout << endl; } }
包(package)
訊息格式
.proto檔案新增一個可選的package宣告符,用來防止不同的訊息型別有命名衝突。包的宣告符會根據使用語言的不同影響生成的程式碼。對於C++,產生的類會被包裝在C++的名稱空間中。
syntax = "proto3";//指定版本資訊,不指定會報錯 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為陣列 }message AddressBook { repeated Person people = 1; }
測試程式
void set_addressbook()
{
tutorial::AddressBook obj;
tutorial::Person *p1 = obj.add_people(); //新增加一個Person
p1->set_name("mike");
p1->set_id(1);
p1->set_email("[email protected]");
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);
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;
}
cout << endl;
}
}
本教程原始碼下載地址:http://download.csdn.net/detail/tennysonsky/9884335(在下載包中,有在資料夾下有addressbook.pb.h和addressbook.pb.cc檔案由protoc編譯器依據addressbook.proto檔案生成)
轉自:https://blog.csdn.net/tennysonsky/article/details/73921295