国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

protobuf C++ 使用示例

2019-11-08 00:49:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1、在.PRoto文件中定義消息格式

2、使用protobuf編譯器

3、使用c++ api來(lái)讀寫(xiě)消息

 

0、為何使用protobuf?

 

1、原始內(nèi)存數(shù)據(jù)結(jié)構(gòu),可以以二進(jìn)制方式sent/saved.這種方式需要相同的內(nèi)存布局和字節(jié)序。

2、以ad-hoc方式將數(shù)據(jù)項(xiàng)編碼成一個(gè)簡(jiǎn)單字符串----比如,將4個(gè)int類型編碼成"12:3:-23:67"。這種方式簡(jiǎn)靈活。適用于簡(jiǎn)單數(shù)據(jù)。

3、將數(shù)據(jù)序列化為xml。這種方式很流行,因?yàn)閤ml可讀性好,編碼解碼方便,性能也好。僅僅XML dom樹(shù)比較復(fù)雜。

 

protobuf可以很好的解決上述問(wèn)題。你編寫(xiě)一個(gè).proto文件來(lái)描述數(shù)據(jù)結(jié)構(gòu)。protobuf編譯器使用它創(chuàng)建一個(gè)類,使用二進(jìn)制方式自動(dòng)編碼/解碼該數(shù)據(jù)結(jié)構(gòu)。生成的類提供getter/setter方法。

 

最重要的是,protobuf支持在此基礎(chǔ)上進(jìn)行格式擴(kuò)展。

 

示例

 

1、定義協(xié)議格式

 

package tutorial;  message Person {

   required string name = 1;

   required int32 id = 2;

   optional string email= 3;

  

    enum PhoneType {

        MOBILE = 0;

        HOME = 1;

        WORK = 2;

   }

 

   message PhoneNumber {

        required string number = 1;

        optional PhoneType type = 2 [default = HOME];  

   }

 

   repeated PhoneNumber phone= 4;

 

message AddressBook {

   repeated Personperson = 1;

}

 

 

該結(jié)構(gòu)與c++或java很像.

 

.proto文件以包聲明開(kāi)始,防止名字沖突。

簡(jiǎn)單類型:bool, int32, float, double, string.

其它類型:如上述的Person, PhoneNumber

 

類型可以嵌套。

“=1”, “=2”標(biāo)識(shí)唯一“tag”.tag數(shù)1-15需要至少一個(gè)字節(jié)。

 

required: 必須設(shè)置它的值

optional: 可以設(shè)置,也可以不設(shè)置它的值

repeated: 可以認(rèn)為是動(dòng)態(tài)分配的數(shù)組

google工程師認(rèn)為使用required威害更大,他們更喜歡使用optional, repeated.

 

 

2、編譯你的協(xié)議

 

運(yùn)行protoc 來(lái)生成c++文件:

protoc -I=$SRC_DIR --cpp_out=$DST_DIR$SRC_DIR/addressbook.proto

生成的文件為:

addressbook.pb.h, 

addressbook.pb.cc

 

3、protobuf API

 

生成的文件中有如下方法:

// name

  inline bool has_name() const;

  inline voidclear_name();

  inline const ::std::string& name() const;

  inline void set_name(const ::std::string& value);

  inline void set_name(const char* value);

  inline ::std::string*mutable_name();

 

  // id

  inline bool has_id() const;

  inline void clear_id();

  inline int32_t id() const;

  inline void set_id(int32_t value);

 

  // email

  inline bool has_email() const;

  inline voidclear_email();

  inline const ::std::string& email() const;

  inline void set_email(const ::std::string& value);

  inline void set_email(const char* value);

  inline ::std::string* mutable_email();

 

  // phone

  inline intphone_size() const;

  inline voidclear_phone();

  inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const;

  inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >*mutable_phone();

  inline const ::tutorial::Person_PhoneNumber& phone(int index) const;

  inline ::tutorial::Person_PhoneNumber*mutable_phone(int index);

  inline ::tutorial::Person_PhoneNumber* add_phone();

4、枚舉與嵌套類

 

生成的代碼包含一個(gè)PhoneType枚舉。Person::PhoneType, Person:MOBILE,Person::HOME, Person:WORK.

 

編譯器生成的嵌套類稱為Person::PhoneNumber. 實(shí)際生成類為Person_PhoneNumber.

 

5、標(biāo)準(zhǔn)方法

 

bool IsInitialized() const:               確認(rèn)required字段是否被設(shè)置

string DebugString() const:               返回消息的可讀表示,用于調(diào)試

void CopyFrom(const Person& from):         使用給定消息值copy

void Clear():                             清除所有元素為空狀態(tài)

 

6、解析與序列化

 

bool SerializeToString(string* output) const:        序列化消息,將存儲(chǔ)字節(jié)的以string方式輸出。注意字節(jié)是二進(jìn)制,而非文本;

bool ParseFromString(const string& data):            解析給定的string     

bool SerializeToOstream(ostream* output) const:      寫(xiě)消息給定的c++  ostream中

bool ParseFromIstream(istream* input):              從給定的c++ istream中解析出消息

7、protobuf和 oo設(shè)計(jì)

不要繼承生成類并在此基礎(chǔ)上添加相應(yīng)的行為

 

8、寫(xiě)消息

 

示例:它從一個(gè)文件中讀取AddressBook,基于io添加一個(gè)新的Person,并將新的AddressBook寫(xiě)回文件。

#include <iostream>

#include <fstream>

#include <string>

#include"addressbook.pb.h"

using namespace std;

 

// Thisfunction fills in a Person message based on user input.

void PromptForAddress(tutorial::Person* person) {

 cout << "Enter person ID number: ";

  int id;

  cin>> id;

  person->set_id(id);

  cin.ignore(256, '/n');

 

 cout << "Enter name: ";

 getline(cin, *person->mutable_name());

 

 cout << "Enter email address (blank for none):";

  string email;

 getline(cin, email);

  if (!email.empty()) {

   person->set_email(email);

  }

 

  while (true) {

   cout << "Enter a phone number (or leave blank tofinish): ";

   string number;

   getline(cin, number);

   if (number.empty()) {

     break;

   }

 

   tutorial::Person::PhoneNumber* phone_number = person->add_phone();

   phone_number->set_number(number);

 

   cout << "Is this a mobile, home, or work phone?";

   string type;

   getline(cin, type);

   if (type == "mobile") {

     phone_number->set_type(tutorial::Person::MOBILE);

   } else if (type == "home") {

     phone_number->set_type(tutorial::Person::HOME);

   } else if (type == "work") {

     phone_number->set_type(tutorial::Person::WORK);

   } else {

     cout << "Unknownphone type.  Using default." << endl;

   }

  }

}

 

// Mainfunction:  Reads the entire address book from a file,

//  adds one person based on user input, then writes it back out to the same

//  file.

int main(int argc, char* argv[]) {

  // Verifythat the version of the library that we linked against is

  //compatible with the version of the headers we compiled against.

  GOOGLE_PROTOBUF_VERIFY_VERSION;

 

  if (argc != 2) {

   cerr << "Usage:  " << argv[0] << "ADDRESS_BOOK_FILE" << endl;

   return -1;

  }

 

  tutorial::AddressBook address_book;

 

  {

   // Read the existing address book.

   fstream input(argv[1], ios::in | ios::binary);

   if (!input) {

     cout << argv[1] << ":File not found.  Creating a new file." << endl;

   } else if (!address_book.ParseFromIstream(&input)) {

     cerr << "Failedto parse address book." << endl;

     return -1;

   }

  }

 

  // Add anaddress.

  PromptForAddress(address_book.add_person());

 

  {

   // Write the new address book back to disk.

   fstream output(argv[1], ios::out | ios::trunc | ios::binary);

   if (!address_book.SerializeToOstream(&output)) {

     cerr << "Failedto write address book." << endl;

     return -1;

   }

  }

 

  //Optional:  Delete all global objects allocated by libprotobuf.

  google::protobuf::ShutdownProtobufLibrary();

 

  return 0;

}

注意使用GOOGLE_PROTOBUF_VERIFY_VERSION宏。每一個(gè).pb.cc文件在啟動(dòng)時(shí)都將自動(dòng)調(diào)用該宏。

 

注意在程序結(jié)尾處調(diào)用ShutdownProtobufLibrary()。

 

9、讀消息 

#include <iostream>

#include <fstream>

#include <string>

#include"addressbook.pb.h"

using namespace std;

 

//Iterates though all people in the AddressBook and prints info about them.

void ListPeople(const tutorial::AddressBook& address_book) {

  for (int i = 0; i < address_book.person_size(); i++) {

   const tutorial::Person& person = address_book.person(i);

 

   cout << "Person ID: " << person.id() << endl;

   cout << "  Name: " << person.name() << endl;

   if (person.has_email()) {

     cout << " E-mail address: " << person.email() << endl;

   }

 

   for (int j = 0; j < person.phone_size(); j++) {

     const tutorial::Person::PhoneNumber& phone_number = person.phone(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;

   }

  }

}

 

// Mainfunction:  Reads the entire address book from a file and prints all

//  the information inside.

int main(int argc, char* argv[]) {

  // Verifythat the version of the library that we linked against is

  //compatible with the version of the headers we compiled against.

  GOOGLE_PROTOBUF_VERIFY_VERSION;

 

  if (argc != 2) {

   cerr << "Usage:  " << argv[0] << "ADDRESS_BOOK_FILE" << endl;

   return -1;

  }

 

  tutorial::AddressBook address_book;

 

  {

   // Read the existing address book.

   fstream input(argv[1], ios::in | ios::binary);

   if (!address_book.ParseFromIstream(&input)) {

     cerr << "Failedto parse address book." << endl;

     return -1;

   }

  }

 

  ListPeople(address_book);

 

  //Optional:  Delete all global objects allocated by libprotobuf.

  google::protobuf::ShutdownProtobufLibrary();

 

  return 0;

}

10、擴(kuò)展protobuf

 

如果希望向后兼容,必須遵循:

a、不必更改tag數(shù)

b、不必添加或刪除任何required字段

c、可以刪除optional或repeated字段

d、可以添加新的optional或repeated字段,但你必須使用新的tag數(shù)。

 

11、優(yōu)化

c++的protobuf庫(kù),已經(jīng)極大地優(yōu)化了。合理使用可以改善性能。

a、如果可能,復(fù)用message對(duì)象。

b、關(guān)于多線程的內(nèi)存分配器

 

12、高級(jí)用法

 

protobuf的消息類的一個(gè)關(guān)鍵特性是,反射(reflection)??梢允褂脁ml或json來(lái)實(shí)現(xiàn)。參考。

 

 

================================================================

常見(jiàn)問(wèn)題:

1、undefined reference to`pthread_once' 

使用-lpthread:

 

2、error while loading shared libraries:libprotobuf.so.7: cannot open shared object file: No such file or directory

使用-Wl,-Bstatic -lprotobuf -Wl,-Bdynamic-lpthread


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 晋城| 樟树市| 白玉县| 亚东县| 修文县| 台南市| 襄汾县| 大宁县| 岳阳县| 锡林郭勒盟| 武隆县| 鄂托克前旗| 望谟县| 城固县| 山阳县| 买车| 鲜城| 渝中区| 沙坪坝区| 日照市| 安吉县| 额济纳旗| 油尖旺区| 富阳市| 柞水县| 宁国市| 榆中县| 温泉县| 正阳县| 易门县| 吐鲁番市| 内乡县| 盐亭县| 阳山县| 剑阁县| 龙海市| 观塘区| 阳山县| 同德县| 筠连县| 侯马市|