接口类,在类MessageLite的基础上增加了descriptors和reflection
//google::protobuf::Message const Descriptor* GetDescriptor() const; const Reflection* GetReflection() const;描述一种Message类型(不是一个单独的message对象)的meta信息
//Descriptor //返回message的属性个数 int field_count() const; //返回第index个属性 const FieldDescriptor* field(int index) const; //通过proto文件里面定义的tag, 返回属性 const FieldDescriptor* FindFieldByNumber(int number) const; //通过属性名,返回属性 const FieldDescriptor* FindFieldByName(const std::string& name) const; //通过小写的属性名,返回属性 const FieldDescriptor* FindFieldByLowercaseName( const std::string& lowercase_name) const; //通过驼峰属性名,返回属性 const FieldDescriptor* FindFieldByCamelcaseName( const std::string& camelcase_name) const; //返回枚举类型的数量 int enum_type_count() const; //返回第index个枚举类型属性 const EnumDescriptor* enum_type(int index) const; //通过名称,返回枚举属性 const EnumDescriptor* FindEnumTypeByName(const std::string& name) const; //通过属性值,返回枚举值属性 const EnumValueDescriptor* FindEnumValueByName(const std::string& name) const;接口类,提供方法来动态访问/修改message中的field的接口类
//检查message中的非repeated属性是否存在 bool HasField(const Message& message, const FieldDescriptor* field) const; //返回message中repeated属性的长度 int FieldSize(const Message& message, const FieldDescriptor* field) const; //清除message中的属性,非repeated属性HashField返回false,repeated属性FieldSize返回0 void ClearField(Message* message, const FieldDescriptor* field) const; //返回message中除Unknown的属性,包括所有HashField返回为true,FieldSize非0的属性 void ListFields(const Message& message, std::vector<const FieldDescriptor*>* output) const; //返回message对应属性的,对应类型的返回值 int32 GetInt32(const Message& message, const FieldDescriptor* field) const; int64 GetInt64(const Message& message, const FieldDescriptor* field) const; uint32 GetUInt32(const Message& message, const FieldDescriptor* field) const; uint64 GetUInt64(const Message& message, const FieldDescriptor* field) const; float GetFloat(const Message& message, const FieldDescriptor* field) const; double GetDouble(const Message& message, const FieldDescriptor* field) const; bool GetBool(const Message& message, const FieldDescriptor* field) const; std::string GetString(const Message& message, const FieldDescriptor* field) const; const EnumValueDescriptor* GetEnum(const Message& message, const FieldDescriptor* field) const; //返回message,属性的value的int类型值 int GetEnumValue(const Message& message, const FieldDescriptor* field) const; const Message& GetMessage(const Message& message, const FieldDescriptor* field, MessageFactory* factory = nullptr) const; //设置message,对应属性的对应类型的值 void SetInt32(Message* message, const FieldDescriptor* field, int32 value) const; void SetInt64(Message* message, const FieldDescriptor* field, int64 value) const; void SetUInt32(Message* message, const FieldDescriptor* field, uint32 value) const; void SetUInt64(Message* message, const FieldDescriptor* field, uint64 value) const; void SetFloat(Message* message, const FieldDescriptor* field, float value) const; void SetDouble(Message* message, const FieldDescriptor* field, double value) const; void SetBool(Message* message, const FieldDescriptor* field, bool value) const; void SetString(Message* message, const FieldDescriptor* field, const std::string& value) const; void SetEnum(Message* message, const FieldDescriptor* field, const EnumValueDescriptor* value) const; void SetEnumValue(Message* message, const FieldDescriptor* field, int value) const; Message* MutableMessage(Message* message, const FieldDescriptor* field, MessageFactory* factory = nullptr) const; //返回message对应属性repeated字段的index位置的值 int32 GetRepeatedInt32(const Message& message, const FieldDescriptor* field, int index) const; int64 GetRepeatedInt64(const Message& message, const FieldDescriptor* field, int index) const; uint32 GetRepeatedUInt32(const Message& message, const FieldDescriptor* field, int index) const; uint64 GetRepeatedUInt64(const Message& message, const FieldDescriptor* field, int index) const; float GetRepeatedFloat(const Message& message, const FieldDescriptor* field, int index) const; double GetRepeatedDouble(const Message& message, const FieldDescriptor* field, int index) const; bool GetRepeatedBool(const Message& message, const FieldDescriptor* field, int index) const; std::string GetRepeatedString(const Message& message, const FieldDescriptor* field, int index) const; const EnumValueDescriptor* GetRepeatedEnum(const Message& message, const FieldDescriptor* field, int index) const; int GetRepeatedEnumValue(const Message& message, const FieldDescriptor* field, int index) const; const Message& GetRepeatedMessage(const Message& message, const FieldDescriptor* field, int index) const; //设置message的repeated属性的index位置对应类型的value的值 void SetRepeatedInt32(Message* message, const FieldDescriptor* field, int index, int32 value) const; void SetRepeatedInt64(Message* message, const FieldDescriptor* field, int index, int64 value) const; void SetRepeatedUInt32(Message* message, const FieldDescriptor* field, int index, uint32 value) const; void SetRepeatedUInt64(Message* message, const FieldDescriptor* field, int index, uint64 value) const; void SetRepeatedFloat(Message* message, const FieldDescriptor* field, int index, float value) const; void SetRepeatedDouble(Message* message, const FieldDescriptor* field, int index, double value) const; void SetRepeatedBool(Message* message, const FieldDescriptor* field, int index, bool value) const; void SetRepeatedString(Message* message, const FieldDescriptor* field, int index, const std::string& value) const; void SetRepeatedEnum(Message* message, const FieldDescriptor* field, int index, const EnumValueDescriptor* value) const; void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field, int index, int value) const; Message* MutableRepeatedMessage(Message* message, const FieldDescriptor* field, int index) const;Message类的具体属性的描述,包含类型,名称,label,tag,proto文件等等信息
//是否是必须的 bool is_required() const; // shorthand for label() == LABEL_REQUIRED //是否是可选的 bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL //是否是repeated bool is_repeated() const; // shorthand for label() == LABEL_REPEATED //是否可以pack bool is_packable() const; // shorthand for is_repeated() && // IsTypePackable(type()) //是否是packed bool is_packed() const; // shorthand for is_packable() && // options().packed() //是否是message bool is_map() const; // shorthand for type() == TYPE_MESSAGE && // message_type()->options().map_entry() //属性在message的索引位置 int index() const;保存协议中未知的属性集合
//返回UnknownField的数量 inline int field_count() const; //返回index位置的UnknownField inline const UnknownField& field(int index) const; //返回index位置的UnknownField指针,可修改 inline UnknownField* mutable_field(int index); //添加UnknownField void AddVarint(int number, uint64 value); void AddFixed32(int number, uint32 value); void AddFixed64(int number, uint64 value); void AddLengthDelimited(int number, const std::string& value); std::string* AddLengthDelimited(int number); // Adds an unknown field from another set. void AddField(const UnknownField& field);C++ Google Protobuf 反射技术基础API C++ 实用技术 – google protobuf反射技术 – 转成JSON格式 C++ 实用技术 - google protobuf反射技术 - 转成YAML格式