Protocol Buffer的理解

Protocol Buffers是谷歌提供的一種用來序列化結構體數據的機制,相似於XML。官網上這麼定義:html

Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data.git

在proto中定義message以後,編譯器會給每一個field生成一個class。每一個class生成一些方法,以foo爲例:github

message Person {
  required string name = 1;
  required int32 id = 2;
  optional string foo = 3;
}

則其中的foo會自動生成一些系列的方法,ui

bool has_foo() const: 返回field是否被設置
int32 foo() const: 返回當前值
void set_foo(int32 value): 設置field的值
void clear_foo(): 清除field的值
string* mutable_foo():返回string field的指針

##參考google

https://github.com/google/protobuf指針

http://www.searchtb.com/2012/09/protocol-buffers.htmlcode

https://developers.google.com/protocol-buffers/?hl=zh-cnorm

https://developers.google.com/protocol-buffers/docs/reference/cpp-generated?hl=zh-cnhtm

相關文章
相關標籤/搜索