probuf 學習之二

.proto 文件是這樣的 數組

package http;


message user_info
{
   required int32  _id=1;
   required string _loginame=2;
   required string _nickname=3;
   required string _realname=4;
   required string _email=5;
   required string _mobile=6;
   required string _portraitcrc=7;
   required string _impresa=8;
   required int32  _optype=9 ;   //看這個值的變化
}

message user_tab
{
    repeated user_info user=1;
}

 

repeated函數

inline int user_info::_optype_size() const {     //與required不一樣的
  return _optype_.size();
}
inline void user_info::clear__optype() {
  _optype_.Clear();
}
inline ::google::protobuf::int32 user_info::_optype(int index) const {
  return _optype_.Get(index);
}
inline void user_info::set__optype(int index, ::google::protobuf::int32 value) {
  _optype_.Set(index, value);
}
inline void user_info::add__optype(::google::protobuf::int32 value) {
  _optype_.Add(value);
}
inline const ::google::protobuf::RepeatedField< ::google::protobuf::int32 >&
user_info::_optype() const {
  return _optype_;
}
inline ::google::protobuf::RepeatedField< ::google::protobuf::int32 >*
user_info::mutable__optype() {
  return &_optype_;
}

optional ui

// optional int32 _optype = 9 [default = 0];
inline bool user_info::has__optype() const {
  return (_has_bits_[0] & 0x00000100u) != 0;
}
inline void user_info::set_has__optype() {
  _has_bits_[0] |= 0x00000100u;
}
inline void user_info::clear_has__optype() {
  _has_bits_[0] &= ~0x00000100u;
}
inline void user_info::clear__optype() {
  _optype_ = 0;
  clear_has__optype();
}
inline ::google::protobuf::int32 user_info::_optype() const {
  return _optype_;
}
inline void user_info::set__optype(::google::protobuf::int32 value) {
  set_has__optype();
  _optype_ = value;
}

requiredgoogle

// required int32 _optype = 9;
inline bool user_info::has__optype() const {
  return (_has_bits_[0] & 0x00000100u) != 0;
}
inline void user_info::set_has__optype() {
  _has_bits_[0] |= 0x00000100u;
}
inline void user_info::clear_has__optype() {
  _has_bits_[0] &= ~0x00000100u;
}
inline void user_info::clear__optype() {
  _optype_ = 0;
  clear_has__optype();
}
inline ::google::protobuf::int32 user_info::_optype() const {
  return _optype_;
}
inline void user_info::set__optype(::google::protobuf::int32 value) {
  set_has__optype();
  _optype_ = value;
}

 

required:一個格式良好的消息必定要含有1個這種字段。表示該值是必需要設置的;編碼

optional:消息格式中該字段能夠有0個或1個值(不超過1個)。spa

repeated:在一個格式良好的消息中,這種字段能夠重複任意屢次(包括0次)。重複的值的順序會被保留。表示該值能夠重複。code

進一步說明:blog

Required: 表示是一個必須字段,必須相對於發送方,在發送消息以前必須設置該字段的值,對於接收方,必須可以識別該字段的意思。發送以前沒有設置required字段或者沒法識別required字段都會引起編解碼異常,致使消息被丟棄。接口

Optional:表示是一個可選字段,可選對於發送方,在發送消息時,能夠有選擇性的設置或者不設置該字段的值。對於接收方,若是可以識別可選字段就進行相應的處理,若是沒法識別,則忽略該字段,消息中的其它字段正常處理。---由於optional字段的特性,不少接口在升級版本中都把後來添加的字段都統一的設置爲optional字段,這樣老的版本無需升級程序也能夠正常的與新的軟件進行通訊,只不過新的字段沒法識別而已,由於並非每一個節點都須要新的功能,所以能夠作到按需升級和平滑過渡。string

Repeated:表示該字段能夠包含0~N個元素。其特性和optional同樣,可是每一次能夠包含多個值。能夠看做是在傳遞一個數組的值。

主要看repeated的狀況:

repeated就像是一個vector,咱們能夠添加元素,知道這個vector的大小,還可以返回對應下標的元素;

inline int user_info::_optype_size() const {     //與required不一樣的
  return _optype_.size();
}
inline void user_info::add__optype(::google::protobuf::int32 value) {//添加元素
  _optype_.Add(value);
}
inline ::google::protobuf::int32 user_info::_optype(int index) const {//獲取對應下標的值
  return _optype_.Get(index);
}

因爲一些歷史緣由,基本數值類型的repeated的字段並無被儘量地高效編碼。在新的代碼中,用戶應該使用特殊選項[packed=true]來保證更高效的編碼。

required是永久性的:在將一個字段標識爲required的時候,應該特別當心。若是在某些狀況下不想寫入或者發送一個required的 字段,將原始該字段修飾符更改成optional可能會遇到問題——舊版本的使用者會認爲不含該字段的消息是不完整的,從而可能會無目的的拒絕解析。在這 種狀況下,你應該考慮編寫特別針對於應用程序的、自定義的消息校驗函數。Google的一些工程師得出了一個結論:使用required弊多於利;他們更 願意使用optional和repeated而不是required。固然,這個觀點並不具備廣泛性。

相關文章
相關標籤/搜索