.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。固然,这个观点并不具备广泛性。