【Protocol Buffer】Protocol Buffer入门教程(六):枚举和包
生活随笔
收集整理的這篇文章主要介紹了
【Protocol Buffer】Protocol Buffer入门教程(六):枚举和包
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
00. 目錄
文章目錄
- 00. 目錄
- 01. 枚舉消息格式
- 02. 枚舉測試代碼
- 03. 編譯和測試
- 04. 包的消息格式
- 05. 包的測試程序
- 06. 編譯和測試
- 07. 附錄
01. 枚舉消息格式
當需要定義一個消息類型的時候,可能想為一個字段指定某“預定義值序列”中的一個值,這時候可以通過枚舉實現。
syntax = "proto3";//指定版本信息,不指定會報錯message Person //message為關鍵字,作用為定義一種消息類型 {string name = 1; //姓名int32 id = 2; //idstring 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; }生成對應文件
deng@itcast:/mnt/hgfs/LinuxHome/day03$ protoc addressbook.proto --cpp_out=./ deng@itcast:/mnt/hgfs/LinuxHome/day03$02. 枚舉測試代碼
#include "addressbook.pb.h" #include <iostream> #include <fstream>using namespace std;void set_addressbook() {AddressBook obj;Person *p1 = obj.add_people(); //新增加一個Personp1->set_name("tom");p1->set_id(1);p1->set_email("tom@qq.com");Person::PhoneNumber *phone1 = p1->add_phones(); //增加一個phonephone1->set_number("110");phone1->set_type(Person::MOBILE);Person::PhoneNumber *phone2 = p1->add_phones(); //增加一個phonephone2->set_number("120");phone2->set_type(Person::HOME);fstream output("pb.itcast", 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.itcast", 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個peoplecout << "第" << 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;} }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; }03. 編譯和測試
編譯和測試結果
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf` deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 第1個信息 name = tom id = 1 email = tom@qq.comMobile phone #: 110Home phone #: 120deng@itcast:/mnt/hgfs/LinuxHome/day03$04. 包的消息格式
.proto文件新增一個可選的package聲明符,用來防止不同的消息類型有命名沖突。包的聲明符會根據使用語言的不同影響生成的代碼。對于C++,產生的類會被包裝在C++的命名空間中。
syntax = "proto3";//指定版本信息,不指定會報錯package tutorial; //package聲明符message Person //message為關鍵字,作用為定義一種消息類型 {string name = 1; //姓名int32 id = 2; //idstring 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; }05. 包的測試程序
#include "addressbook.pb.h" #include <iostream> #include <fstream>using namespace std;void set_addressbook() {tutorial::AddressBook obj;tutorial::Person *p1 = obj.add_people(); //新增加一個Personp1->set_name("tom");p1->set_id(1);p1->set_email("tom@qq.com");tutorial::Person::PhoneNumber *phone1 = p1->add_phones(); //增加一個phonephone1->set_number("110");phone1->set_type(tutorial::Person::MOBILE);tutorial::Person::PhoneNumber *phone2 = p1->add_phones(); //增加一個phonephone2->set_number("120");phone2->set_type(tutorial::Person::HOME);fstream output("pb.itcast", 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.itcast", 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個peoplecout << "第" << 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;} }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; }06. 編譯和測試
編譯和測試
deng@itcast:/mnt/hgfs/LinuxHome/day03$ g++ test.cpp addressbook.pb.cc `pkg-config --libs --cflags protobuf` deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 第1個信息 name = tom id = 1 email = tom@qq.comMobile phone #: 110Home phone #: 120deng@itcast:/mnt/hgfs/LinuxHome/day03$07. 附錄
官方參考:https://developers.google.cn/protocol-buffers/docs/reference/cpp-generated#package
測試代碼下載:測試代碼下載
總結
以上是生活随笔為你收集整理的【Protocol Buffer】Protocol Buffer入门教程(六):枚举和包的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Protocol Buffer】Pro
- 下一篇: 【Protocol Buffer】Pro