【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符
生活随笔
收集整理的這篇文章主要介紹了
【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
00. 目錄
文章目錄
- 00. 目錄
- 01. 限定修飾符介紹
- 02. 字段API分析
- 03. 測試代碼
- 04. 編譯和測試
- 05. 參考
01. 限定修飾符介紹
repeated 代表可重復,我們可以理解為數組
syntax = "proto3";//指定版本信息,不指定會報錯message Person //message為關鍵字,作用為定義一種消息類型 {string name = 1; //姓名int32 id = 2; //idstring email = 3; //郵件 }message AddressBook {repeated Person people = 1; }02. 字段API分析
deng@itcast:/mnt/hgfs/LinuxHome/day03$ protoc addressbook.proto --cpp_out=./ deng@itcast:/mnt/hgfs/LinuxHome/day03$而對于字段修飾符為repeated的字段生成的函數,則稍微有一些不同,如people字段,則編譯器會為其產生如下的代碼:
enum : int {kPeopleFieldNumber = 1,}; // repeated .Person people = 1;int people_size() const;private:int _internal_people_size() const;public:void clear_people();::Person* mutable_people(int index);::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::Person >*mutable_people();private:const ::Person& _internal_people(int index) const;::Person* _internal_add_people();public:const ::Person& people(int index) const;::Person* add_people();const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField< ::Person >&people() const;03. 測試代碼
參考代碼如下:
#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 *p2 = obj.add_people(); //新增加一個Personp2->set_name("jim");p2->set_id(2);p2->set_email("jim@qq.com");Person *p3 = obj.add_people(); //新增加一個Personp3->set_name("abc");p3->set_id(3);p3->set_email("abc@qq.com");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 << 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; }04. 編譯和測試
編譯和測試
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.com第2個信息 name = jim id = 2 email = jim@qq.com第3個信息 name = abc id = 3 email = abc@qq.comdeng@itcast:/mnt/hgfs/LinuxHome/day03$05. 參考
官方參考文檔:https://developers.google.cn/protocol-buffers/docs/reference/cpp-generated#repeatednumeric
測試代碼下載:測試代碼下載
總結
以上是生活随笔為你收集整理的【Protocol Buffer】Protocol Buffer入门教程(五):repeated限定修饰符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——a
- 下一篇: 【Protocol Buffer】Pro