protobuf生成的cpp库相关的API接口

protobuf语法:syntax = "proto2"

定义一个测试文件:test.proto 内容如下

syntax = "proto2";

message People
{
	required int32 id = 1;
	optional string name = 2;
	optional string address = 3;
	repeated string phone = 4;
}

使用protoc编译过后生成test.pb.h和test.pb.cc两个文件

关于required字段(以id为例),提供以下几个公共接口:

bool has_id() const;
void clear_id();
int32_t id() const;
void set_id(int32_t value);

关于optional字段(以name为例),提供以下几个公共接口:

bool has_name() const;
void clear_name() const;
const std::string& name() const;

template <typename ArgT0 = const std::string&, typename... ArgT>
void set_name(ArgT0&& arg0, ArgT... args);

std::string* mutable_name();//返回一个string* 没有就create一个
std::string* release_name();//不存在name就返回nullptr
void set_allocated_name(std::string* name);

关于repeated字段(以phone为例),提供以下几个公共接口:

int phone_size() const;//返回当前数量
void clear_phone();
std::string* add_phone();
const std::string& phone(int index) const;
std::string* mutable_phone(int index);
void set_phone(int index, const std::string& value);
void set_phone(int index, std::string&& value);
void set_phone(int index, const char* value);
void set_phone(int index, const char* value, size_t size);
void add_phone(const std::string& value);
void add_phone(std::string&& value);
void add_phone(const char* value);
void add_phone(const char* value, size_t size);
const ::PROTOBUF_NAMESPACE_ID::RepeatedPtrField<std::string>& phone() const;
::PROTOBUF_NAMESPACE_ID::RepeatedPtrField<std::string>* mutable_phone();

People类维护了一个phone的成员phone_并提供了以下该方法

inline bool RepeatedPtrField<Element>::empty() const {

inline int RepeatedPtrField<Element>::size() const {

inline const Element& RepeatedPtrField<Element>::Get(int index) const {

inline Element* RepeatedPtrField<Element>::Mutable(int index) {

inline Element* RepeatedPtrField<Element>::Add() {

inline void RepeatedPtrField<Element>::Add(Element&& value) {

inline void RepeatedPtrField<Element>::RemoveLast() {

inline void RepeatedPtrField<Element>::DeleteSubrange(int start, int num) {

inline void RepeatedPtrField<Element>::Clear() {

inline void RepeatedPtrField<Element>::MergeFrom(

inline void RepeatedPtrField<Element>::CopyFrom(

RepeatedPtrField<Element>::erase(const_iterator position) {

RepeatedPtrField<Element>::erase(const_iterator first, const_iterator last) {

inline Element** RepeatedPtrField<Element>::mutable_data() {

inline const Element* const* RepeatedPtrField<Element>::data() const {

inline void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) {

inline void RepeatedPtrField<Element>::UnsafeArenaSwap(

inline void RepeatedPtrField<Element>::SwapElements(int index1, int index2) {

inline Arena* RepeatedPtrField<Element>::GetArenaNoVirtual() const {

inline size_t RepeatedPtrField<Element>::SpaceUsedExcludingSelfLong() const {

inline void RepeatedPtrField<Element>::UnsafeArenaAddAllocated(Element* value) {

inline Element* RepeatedPtrField<Element>::ReleaseLast() {

inline Element* RepeatedPtrField<Element>::UnsafeArenaReleaseLast() {

inline int RepeatedPtrField<Element>::ClearedCount() const {

inline void RepeatedPtrField<Element>::AddCleared(Element* value) {

inline Element* RepeatedPtrField<Element>::ReleaseCleared() {

inline void RepeatedPtrField<Element>::Reserve(int new_size) {

inline int RepeatedPtrField<Element>::Capacity() const {


版权声明:本文为weixin_43387492原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。