Getting started with gRPC (3)-- Protobuf Application example
Getting started with gRPC (3)-- Protobuf Application example 1. Protobuf usage process
The process of using Protobuf in project development is as follows:
(1) define the proto description file with proto as the suffix name.
(2) use Protobuf compiler protoc to generate programming language code files, and describe the message format in a specific language.
(3) use the API provided by Protobuf library to write the application.
2. Protobuf C++ example 1, setting export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/libexport LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/libexport PATH=$PATH:/usr/local/protobuf/binexport PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/2 of environment variable and defining proto file
The addressbook.proto file is as follows:
/ / [START declaration] syntax = "proto3"; package Book;import "google/protobuf/timestamp.proto"; / / [END declaration] / / [START messages] message Person {string name = 1; int32 id = 2; / / Unique ID number for this person. String email = 3; enum PhoneType {MOBILE = 0; HOME = 1; WORK = 2;} message PhoneNumber {string number = 1; PhoneType type = 2;} repeated PhoneNumber phones = 4; google.protobuf.Timestamp last_updated = 5;} / / Our address book file is just one of these.message AddressBook {repeated Person people = 1;} / / [END messages] 3, generate C++ code
Protoc-cpp_out=. Addressbook.proto
Generate C++ code addressbook.pb.h and addressbook.pb.cc.
Addressbook.pb.h file:
/ / Generated by the protocol buffer compiler. DO NOT EDIT!// source: addressbook.proto#ifndef PROTOBUF_INCLUDED_addressbook_2eproto#define PROTOBUF_INCLUDED_addressbook_2eproto#include # include # if GOOGLE_PROTOBUF_VERSION
< 3006001#error This file was generated by a newer version of protoc which is#error incompatible with your Protocol Buffer headers. Please update#error your headers.#endif#if 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION#error This file was generated by an older version of protoc which is#error incompatible with your Protocol Buffer headers. Please#error regenerate this file with a newer version of protoc.#endif#include #include #include #include #include #include #include #include #include // IWYU pragma: export#include // IWYU pragma: export#include #include // @@protoc_insertion_point(includes)#define PROTOBUF_INTERNAL_EXPORT_protobuf_addressbook_2eproto namespace protobuf_addressbook_2eproto {// Internal implementation detail -- do not use these members.struct TableStruct { static const ::google::protobuf::internal::ParseTableField entries[]; static const ::google::protobuf::internal::AuxillaryParseTableField aux[]; static const ::google::protobuf::internal::ParseTable schema[3]; static const ::google::protobuf::internal::FieldMetadata field_metadata[]; static const ::google::protobuf::internal::SerializationTable serialization_table[]; static const ::google::protobuf::uint32 offsets[];};} // namespace protobuf_addressbook_2eprotonamespace book {class AddressBook;class AddressBookDefaultTypeInternal;extern AddressBookDefaultTypeInternal _AddressBook_default_instance_;class Person;class PersonDefaultTypeInternal;extern PersonDefaultTypeInternal _Person_default_instance_;class Person_PhoneNumber;class Person_PhoneNumberDefaultTypeInternal;extern Person_PhoneNumberDefaultTypeInternal _Person_PhoneNumber_default_instance_;} // namespace booknamespace google {namespace protobuf {template ::book::AddressBook* Arena::CreateMaybeMessage(Arena*);template ::book::Person* Arena::CreateMaybeMessage(Arena*);template ::book::Person_PhoneNumber* Arena::CreateMaybeMessage(Arena*);} // namespace protobuf} // namespace googlenamespace book {enum Person_PhoneType { Person_PhoneType_MOBILE = 0, Person_PhoneType_HOME = 1, Person_PhoneType_WORK = 2, Person_PhoneType_Person_PhoneType_INT_MIN_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32min, Person_PhoneType_Person_PhoneType_INT_MAX_SENTINEL_DO_NOT_USE_ = ::google::protobuf::kint32max};bool Person_PhoneType_IsValid(int value);const Person_PhoneType Person_PhoneType_PhoneType_MIN = Person_PhoneType_MOBILE;const Person_PhoneType Person_PhoneType_PhoneType_MAX = Person_PhoneType_WORK;const int Person_PhoneType_PhoneType_ARRAYSIZE = Person_PhoneType_PhoneType_MAX + 1;// ===================================================================class Person_PhoneNumber : public ::google::protobuf::MessageLite /* @@protoc_insertion_point(class_definition:book.Person.PhoneNumber) */ { public: Person_PhoneNumber(); virtual ~Person_PhoneNumber(); Person_PhoneNumber(const Person_PhoneNumber& from); inline Person_PhoneNumber& operator=(const Person_PhoneNumber& from) { CopyFrom(from); return *this; } #if LANG_CXX11 Person_PhoneNumber(Person_PhoneNumber&& from) noexcept : Person_PhoneNumber() { *this = ::std::move(from); } inline Person_PhoneNumber& operator=(Person_PhoneNumber&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); } else { CopyFrom(from); } return *this; } #endif static const Person_PhoneNumber& default_instance(); static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY static inline const Person_PhoneNumber* internal_default_instance() { return reinterpret_cast( &_Person_PhoneNumber_default_instance_); } static constexpr int kIndexInFileMessages = 0; void Swap(Person_PhoneNumber* other); friend void swap(Person_PhoneNumber& a, Person_PhoneNumber& b) { a.Swap(&b); } // implements Message ---------------------------------------------- inline Person_PhoneNumber* New() const final { return CreateMaybeMessage(NULL); } Person_PhoneNumber* New(::google::protobuf::Arena* arena) const final { return CreateMaybeMessage(arena); } void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from) final; void CopyFrom(const Person_PhoneNumber& from); void MergeFrom(const Person_PhoneNumber& from); void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; bool MergePartialFromCodedStream( ::google::protobuf::io::CodedInputStream* input) final; void SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const final; void DiscardUnknownFields(); int GetCachedSize() const final { return _cached_size_.Get(); } private: void SharedCtor(); void SharedDtor(); void SetCachedSize(int size) const; void InternalSwap(Person_PhoneNumber* other); private: inline ::google::protobuf::Arena* GetArenaNoVirtual() const { return NULL; } inline void* MaybeArenaPtr() const { return NULL; } public: ::std::string GetTypeName() const final; // nested types ---------------------------------------------------- // accessors ------------------------------------------------------- // string number = 1; void clear_number(); static const int kNumberFieldNumber = 1; const ::std::string& number() const; void set_number(const ::std::string& value); #if LANG_CXX11 void set_number(::std::string&& value); #endif void set_number(const char* value); void set_number(const char* value, size_t size); ::std::string* mutable_number(); ::std::string* release_number(); void set_allocated_number(::std::string* number); // .book.Person.PhoneType type = 2; void clear_type(); static const int kTypeFieldNumber = 2; ::book::Person_PhoneType type() const; void set_type(::book::Person_PhoneType value); // @@protoc_insertion_point(class_scope:book.Person.PhoneNumber) private: ::google::protobuf::internal::InternalMetadataWithArenaLite _internal_metadata_; ::google::protobuf::internal::ArenaStringPtr number_; int type_; mutable ::google::protobuf::internal::CachedSize _cached_size_; friend struct ::protobuf_addressbook_2eproto::TableStruct;};// -------------------------------------------------------------------class Person : public ::google::protobuf::MessageLite /* @@protoc_insertion_point(class_definition:book.Person) */ { public: Person(); virtual ~Person(); Person(const Person& from); inline Person& operator=(const Person& from) { CopyFrom(from); return *this; } #if LANG_CXX11 Person(Person&& from) noexcept : Person() { *this = ::std::move(from); } inline Person& operator=(Person&& from) noexcept { if (GetArenaNoVirtual() == from.GetArenaNoVirtual()) { if (this != &from) InternalSwap(&from); } else { CopyFrom(from); } return *this; } #endif static const Person& default_instance(); static void InitAsDefaultInstance(); // FOR INTERNAL USE ONLY static inline const Person* internal_default_instance() { return reinterpret_cast( &_Person_default_instance_); } static constexpr int kIndexInFileMessages = 1; void Swap(Person* other); friend void swap(Person& a, Person& b) { a.Swap(&b); } // implements Message ---------------------------------------------- inline Person* New() const final { return CreateMaybeMessage(NULL); } Person* New(::google::protobuf::Arena* arena) const final { return CreateMaybeMessage(arena); } void CheckTypeAndMergeFrom(const ::google::protobuf::MessageLite& from) final; void CopyFrom(const Person& from); void MergeFrom(const Person& from); void Clear() final; bool IsInitialized() const final; size_t ByteSizeLong() const final; bool MergePartialFromCodedStream( ::google::protobuf::io::CodedInputStream* input) final; void SerializeWithCachedSizes( ::google::protobuf::io::CodedOutputStream* output) const final; void DiscardUnknownFields(); int GetCachedSize() const final { return _cached_size_.Get(); } private: void SharedCtor(); void SharedDtor(); void SetCachedSize(int size) const; void InternalSwap(Person* other); private: inline ::google::protobuf::Arena* GetArenaNoVirtual() const { return NULL; } inline void* MaybeArenaPtr() const { return NULL; } public: ::std::string GetTypeName() const final; // nested types ---------------------------------------------------- typedef Person_PhoneNumber PhoneNumber; typedef Person_PhoneType PhoneType; static const PhoneType MOBILE = Person_PhoneType_MOBILE; static const PhoneType HOME = Person_PhoneType_HOME; static const PhoneType WORK = Person_PhoneType_WORK; static inline bool PhoneType_IsValid(int value) { return Person_PhoneType_IsValid(value); } static const PhoneType PhoneType_MIN = Person_PhoneType_PhoneType_MIN; static const PhoneType PhoneType_MAX = Person_PhoneType_PhoneType_MAX; static const int PhoneType_ARRAYSIZE = Person_PhoneType_PhoneType_ARRAYSIZE; // accessors ------------------------------------------------------- // repeated .book.Person.PhoneNumber phones = 4; int phones_size() const; void clear_phones(); static const int kPhonesFieldNumber = 4; ::book::Person_PhoneNumber* mutable_phones(int index); ::google::protobuf::RepeatedPtrField< ::book::Person_PhoneNumber >* mutable_phones (); const:: book::Person_PhoneNumber& phones (int index) const;:: book::Person_PhoneNumber* add_phones (); const:: google::protobuf::RepeatedPtrField
< ::book::Person_PhoneNumber >& phones () const; / / string name = 1; void clear_name (); static const int kNameFieldNumber = 1; const:: std::string& name () const; void set_name (const:: std::string& value); # if LANG_CXX11 void set_name (:: std::string&& value); # endif void set_name (const char* value); void set_name (const char* value, size_t size);: std::string* mutable_name () :: std::string* release_name (); void set_allocated_name (: std::string* name); / / string email = 3; void clear_email (); static const int kEmailFieldNumber = 3; const:: std::string& email () const; void set_email (const:: std::string& value); # if LANG_CXX11 void set_email (:: std::string&& value); # endif void set_email (const char* value) Void set_email (const char* value, size_t size);: std::string* mutable_email ();: std::string* release_email (); void set_allocated_email (:: std::string* email); / / .google.protobuf.timestamp last_updated = 5; bool has_last_updated () const; void clear_last_updated (); static const int kLastUpdatedFieldNumber = 5 Private: const:: google::protobuf::Timestamp& _ internal_last_updated () const; public: const:: google::protobuf::Timestamp& last_updated () const;:: google::protobuf::Timestamp* release_last_updated ();: google::protobuf::Timestamp* mutable_last_updated (); void set_allocated_last_updated (:: google::protobuf::Timestamp* last_updated); / / int32 id = 2; void clear_id () Static const int kIdFieldNumber = 2;:: google::protobuf::int32 id () const; void set_id (:: google::protobuf::int32 value); / / @ @ protoc_insertion_point (class_scope:book.Person) private: google::protobuf::internal::InternalMetadataWithArenaLite _ internal_metadata_;:: google::protobuf::RepeatedPtrField
< ::book::Person_PhoneNumber >Phones_;:: google::protobuf::internal::ArenaStringPtr name_;:: google::protobuf::internal::ArenaStringPtr email_;:: google::protobuf::Timestamp* last_updated_;:: google::protobuf::int32 id_; mutable:: google::protobuf::internal::CachedSize _ cached_size_; friend struct:: protobuf_addressbook_2eproto::TableStruct;} / /-class AddressBook: public:: google::protobuf::MessageLite / * @ @ protoc_insertion_point (class_definition:book.AddressBook) * / {public: AddressBook (); virtual ~ AddressBook (); AddressBook (const AddressBook& from) Inline AddressBook& operator= (const AddressBook& from) {CopyFrom (from); return * this;} # if LANG_CXX11 AddressBook (AddressBook&& from) noexcept: AddressBook () {* this =:: std::move (from);} inline AddressBook& operator= (AddressBook&& from) noexcept {if (GetArenaNoVirtual () = = from.GetArenaNoVirtual ()) {if (this! = & from) InternalSwap (& from);} else {CopyFrom (from) } return * this;} # endif static const AddressBook& default_instance (); static void InitAsDefaultInstance (); / / FOR INTERNAL USE ONLY static inline const AddressBook* internal_default_instance () {return reinterpret_cast (& _ AddressBook_default_instance_);} static constexpr int kIndexInFileMessages = 2; void Swap (AddressBook* other); friend void swap (AddressBook& a, AddressBook& b) {a.Swap (& b) } / / implements Message-inline AddressBook* New () const final {return CreateMaybeMessage (NULL);} AddressBook* New (:: google::protobuf::Arena* arena) const final {return CreateMaybeMessage (arena);} void CheckTypeAndMergeFrom (const:: google::protobuf::MessageLite& from) final Void CopyFrom (const AddressBook& from); void MergeFrom (const AddressBook& from); void Clear () final; bool IsInitialized () const final; size_t ByteSizeLong () const final; bool MergePartialFromCodedStream (:: google::protobuf::io::CodedInputStream* input) final; void SerializeWithCachedSizes (:: google::protobuf::io::CodedOutputStream* output) const final; void DiscardUnknownFields (); int GetCachedSize () const final {return _ cached_size_.Get ();} private: void SharedCtor () Void SharedDtor (); void SetCachedSize (int size) const; void InternalSwap (AddressBook* other); private: inline:: google::protobuf::Arena* GetArenaNoVirtual () const {return NULL;} inline void* MaybeArenaPtr () const {return NULL;} public: std::string GetTypeName () const final / / nested types-/ / accessors-- / / repeated .book.Person people = 1 Int people_size () const; void clear_people (); static const int kPeopleFieldNumber = 1;: book::Person* mutable_people (int index);:: google::protobuf::RepeatedPtrField
< ::book::Person >* mutable_people (); const:: book::Person& people (int index) const;:: book::Person* add_people (); const:: google::protobuf::RepeatedPtrField
< ::book::Person >& people () const; / / @ @ protoc_insertion_point (class_scope:book.AddressBook) private: google::protobuf::internal::InternalMetadataWithArenaLite _ internal_metadata_;:: google::protobuf::RepeatedPtrField
< ::book::Person >People_; mutable:: google::protobuf::internal::CachedSize _ cached_size_; friend struct:: protobuf_addressbook_2eproto::TableStruct;}; / / = # ifdef _ GNUC__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "- Wstrict-aliasing" # endif / / _ GNUC__// Person_PhoneNumber// string number = 1 endif void Person_PhoneNumber::clear_number () {number_.ClearToEmptyNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited ()) } inline const:: std::string& Person_PhoneNumber::number () const {/ / @ @ protoc_insertion_point (field_get:book.Person.PhoneNumber.number) return number_.GetNoArena ();} inline void Person_PhoneNumber::set_number (const:: std::string& value) {number_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (), value) / / @ @ protoc_insertion_point (field_set:book.Person.PhoneNumber.number)} # if LANG_CXX11inline void Person_PhoneNumber::set_number (:: std::string&& value) {number_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::move (value)) / / @ @ protoc_insertion_point (field_set_rvalue:book.Person.PhoneNumber.number)} # endifinline void Person_PhoneNumber::set_number (const char* value) {GOOGLE_DCHECK (value! = NULL); number_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::string (value)) / / @ @ protoc_insertion_point (field_set_char:book.Person.PhoneNumber.number)} inline void Person_PhoneNumber::set_number (const char* value, size_t size) {number_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::string (reinterpret_cast (value), size)) / / @ @ protoc_insertion_point (field_set_pointer:book.Person.PhoneNumber.number)} inline:: std::string* Person_PhoneNumber::mutable_number () {/ / @ @ protoc_insertion_point (field_mutable:book.Person.PhoneNumber.number) return number_.MutableNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited () } inline:: std::string* Person_PhoneNumber::release_number () {/ / @ @ protoc_insertion_point (field_release:book.Person.PhoneNumber.number) return number_.ReleaseNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited () } inline void Person_PhoneNumber::set_allocated_number (:: std::string* number) {if (number! = NULL) {} else {} number_.SetAllocatedNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (), number); / / @ @ protoc_insertion_point (field_set_allocated:book.Person.PhoneNumber.number)} / / .book.Person.PhoneType type = 2bot inline void Person_PhoneNumber::clear_type () {type_ = 0 } inline:: book::Person_PhoneType Person_PhoneNumber::type () const {/ / @ @ protoc_insertion_point (field_get:book.Person.PhoneNumber.type) return static_cast
< ::book::Person_PhoneType >(type_);} inline void Person_PhoneNumber::set_type (:: book::Person_PhoneType value) {type_ = value / / @ @ protoc_insertion_point (field_set:book.Person.PhoneNumber.type)} / /-/ / Person// string name = 1 Inline void Person::clear_name () {name_.ClearToEmptyNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited ();} inline const:: std::string& Person::name () const {/ / @ @ protoc_insertion_point (field_get:book.Person.name) return name_.GetNoArena ();} inline void Person::set_name (const:: std::string& value) {name_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (), value) / / @ @ protoc_insertion_point (field_set:book.Person.name)} # if LANG_CXX11inline void Person::set_name (:: std::string&& value) {name_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::move (value)) / / @ @ protoc_insertion_point (field_set_rvalue:book.Person.name)} # endifinline void Person::set_name (const char* value) {GOOGLE_DCHECK (value! = NULL); name_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::string (value)) / / @ @ protoc_insertion_point (field_set_char:book.Person.name)} inline void Person::set_name (const char* value, size_t size) {name_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::string (reinterpret_cast (value), size)) / / @ @ protoc_insertion_point (field_set_pointer:book.Person.name)} inline:: std::string* Person::mutable_name () {/ / @ @ protoc_insertion_point (field_mutable:book.Person.name) return name_.MutableNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited () } inline:: std::string* Person::release_name () {/ / @ @ protoc_insertion_point (field_release:book.Person.name) return name_.ReleaseNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited ());} inline void Person::set_allocated_name (:: std::string* name) {if (name! = NULL) {} else {} name_.SetAllocatedNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (), name) / / @ @ protoc_insertion_point (field_set_allocated:book.Person.name)} / / int32 id = 2 inline void Person::clear_id () {id_ = 0;} inline:: google::protobuf::int32 Person::id () const {/ / @ @ protoc_insertion_point (field_get:book.Person.id) return id_;} inline void Person::set_id (:: google::protobuf::int32 value) {id_ = value / / @ @ protoc_insertion_point (field_set:book.Person.id)} / / string email = 3X inline void Person::clear_email () {email_.ClearToEmptyNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited ());} inline const:: std::string& Person::email () const {/ / @ @ protoc_insertion_point (field_get:book.Person.email) return email_.GetNoArena () } inline void Person::set_email (const:: std::string& value) {email_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (), value); / / @ @ protoc_insertion_point (field_set:book.Person.email)} # if LANG_CXX11inline void Person::set_email (:: std::string&& value) {email_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::move (value)) / / @ @ protoc_insertion_point (field_set_rvalue:book.Person.email)} # endifinline void Person::set_email (const char* value) {GOOGLE_DCHECK (value! = NULL); email_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::string (value)) / / @ @ protoc_insertion_point (field_set_char:book.Person.email)} inline void Person::set_email (const char* value, size_t size) {email_.SetNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (),: std::string (reinterpret_cast (value), size)) / / @ @ protoc_insertion_point (field_set_pointer:book.Person.email)} inline:: std::string* Person::mutable_email () {/ / @ @ protoc_insertion_point (field_mutable:book.Person.email) return email_.MutableNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited () } inline:: std::string* Person::release_email () {/ / @ @ protoc_insertion_point (field_release:book.Person.email) return email_.ReleaseNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited ());} inline void Person::set_allocated_email (:: std::string* email) {if (email! = NULL) {} else {} email_.SetAllocatedNoArena (&:: google::protobuf::internal::GetEmptyStringAlreadyInited (), email) / / @ @ protoc_insertion_point (field_set_allocated:book.Person.email)} / / repeated .book.Person.PhoneNumber phones = 4ash inline int Person::phones_size () const {return phones_.size ();} inline void Person::clear_phones () {phones_.Clear () } inline:: book::Person_PhoneNumber* Person::mutable_phones (int index) {/ / @ @ protoc_insertion_point (field_mutable:book.Person.phones) return phones_.Mutable (index);} inline:: google::protobuf::RepeatedPtrField
< ::book::Person_PhoneNumber >* Person::mutable_phones () {/ / @ @ protoc_insertion_point (field_mutable_list:book.Person.phones) return & phones_;} inline const:: book::Person_PhoneNumber& Person::phones (int index) const {/ / @ @ protoc_insertion_point (field_get:book.Person.phones) return phones_.Get (index) } inline:: book::Person_PhoneNumber* Person::add_phones () {/ / @ @ protoc_insertion_point (field_add:book.Person.phones) return phones_.Add ();} inline const:: google::protobuf::RepeatedPtrField
< ::book::Person_PhoneNumber >& Person::phones () const {/ / @ @ protoc_insertion_point (field_list:book.Person.phones) return phones_;} / / .google.protobuf.Timestamp last_updated = 5 return this inline bool Person::has_last_updated () const {return this! = internal_default_instance () & & last_updated_! = NULL;} inline const:: google::protobuf::Timestamp& Person::_internal_last_updated () const {return * last_updated_ } inline const:: google::protobuf::Timestamp& Person::last_updated () const {const:: google::protobuf::Timestamp* p = last_updated_; / / @ @ protoc_insertion_point (field_get:book.Person.last_updated) return p! = NULL? * p: * reinterpret_cast (&:: google::protobuf::_Timestamp_default_instance_) } inline:: google::protobuf::Timestamp* Person::release_last_updated () {/ / @ @ protoc_insertion_point (field_release:book.Person.last_updated):: google::protobuf::Timestamp* temp = last_updated_; last_updated_ = NULL; return temp;} inline:: google::protobuf::Timestamp* Person::mutable_last_updated () {if (last_updated_ = = NULL) {auto* p = CreateMaybeMessage (GetArenaNoVirtual ()) Last_updated_ = p;} / @ @ protoc_insertion_point (field_mutable:book.Person.last_updated) return last_updated_;} inline void Person::set_allocated_last_updated (:: google::protobuf::Timestamp* last_updated) {:: google::protobuf::Arena* message_arena = GetArenaNoVirtual (); if (message_arena = = NULL) {delete reinterpret_cast
< ::google::protobuf::MessageLite*>(last_updated_);} if (last_updated) {:: google::protobuf::Arena* submessage_arena = reinterpret_cast (last_updated)-> GetArena (); if (message_arena! = submessage_arena) {last_updated =:: google::protobuf::internal::GetOwnedMessage (message_arena, last_updated, submessage_arena);} else {} last_updated_ = last_updated / / @ @ protoc_insertion_point (field_set_allocated:book.Person.last_updated)} / /-/ / AddressBook// repeated .book.Person people = 1 * * inline int AddressBook::people_size () const {return people_.size () } inline void AddressBook::clear_people () {people_.Clear ();} inline:: book::Person* AddressBook::mutable_people (int index) {/ / @ @ protoc_insertion_point (field_mutable:book.AddressBook.people) return people_.Mutable (index);} inline:: google::protobuf::RepeatedPtrField
< ::book::Person >* AddressBook::mutable_people () {/ / @ @ protoc_insertion_point (field_mutable_list:book.AddressBook.people) return & people_;} inline const:: book::Person& AddressBook::people (int index) const {/ / @ @ protoc_insertion_point (field_get:book.AddressBook.people) return people_.Get (index) } inline:: book::Person* AddressBook::add_people () {/ / @ @ protoc_insertion_point (field_add:book.AddressBook.people) return people_.Add ();} inline const:: google::protobuf::RepeatedPtrField
< ::book::Person >& AddressBook::people () const {/ / @ @ protoc_insertion_point (field_list:book.AddressBook.people) return people_ } # ifdef _ _ GNUC__ # pragma GCC diagnostic pop#endif / / _ GNUC__//-/-- -/ / @ @ protoc_insertion_point (namespace_scope)} / / namespace booknamespace google {namespace protobuf {template struct is_proto_enum
< ::book::Person_PhoneType>: std::true_type {};} / / namespace protobuf} / / namespace google// @ @ protoc_insertion_point (global_scope) # endif / / PROTOBUF_INCLUDED_addressbook_2eproto4, serialization API
Bool SerializeToString (string* output) const
Serialize the message and output the message object as string.
Bool ParseFromString (const string& data)
Deserialize the message, parsing the given string into a message object
Bool SerializeToOstream (ostream* output) const
Serializes the message, writing the message object to the given C++ ostream
Bool ParseFromIstream (istream* input)
Deserialize messages to parse message objects from a given C++ istream
5. Use examples
Main.cpp file:
# include # include "addressbook.pb.h" using namespace std;// needs to enter an address book file as a parameter int main (int argc, char* argv []) {/ / Verify that the version of the library that we linked against is / / compatible with the version of the headers we compiled against. GOOGLE_PROTOBUF_VERIFY_VERSION; if (argc! = 2) {cerr