1. 程式人生 > >gRPC快速入門(三)——Protobuf應用示例

gRPC快速入門(三)——Protobuf應用示例

gRPC快速入門(三)——Protobuf應用示例

一、Protobuf使用流程

在工程開發中使用Protobuf流程如下:
(1)定義proto描述檔案,以proto作為字尾名。
(2)使用Protobuf編譯器protoc來生成程式語言程式碼檔案,對訊息格式以特定的語言方式描述。
(3)使用Protobuf庫提供的API來編寫應用程式 。

二、Protobuf C++示例

1、環境變數設定

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/protobuf/lib
export LIBRARY_PATH=$LIBRARY_PATH:/usr/local/protobuf/lib
export PATH=$PATH:/usr/local/protobuf/bin
export PKG_CONFIG_PATH=/usr/local/protobuf/lib/pkgconfig/

2、定義proto檔案

addressbook.proto檔案如下:

// [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、生成C++程式碼

protoc --cpp_out=. addressbook.proto
生成C++程式碼addressbook.pb.h和addressbook.pb.cc。
addressbook.pb.h檔案:

// Generated by the protocol buffer compiler.  DO NOT EDIT!
// source: addressbook.proto

#ifndef PROTOBUF_INCLUDED_addressbook_2eproto
#define PROTOBUF_INCLUDED_addressbook_2eproto

#include <string>

#include <google/protobuf/stubs/common.h>

#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 <google/protobuf/io/coded_stream.h>
#include <google/protobuf/arena.h>
#include <google/protobuf/arenastring.h>
#include <google/protobuf/generated_message_table_driven.h>
#include <google/protobuf/generated_message_util.h>
#include <google/protobuf/inlined_string_field.h>
#include <google/protobuf/metadata_lite.h>
#include <google/protobuf/message_lite.h>
#include <google/protobuf/repeated_field.h>  // IWYU pragma: export
#include <google/protobuf/extension_set.h>  // IWYU pragma: export
#include <google/protobuf/generated_enum_util.h>
#include <google/protobuf/timestamp.pb.h>
// @@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_2eproto
namespace 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 book
namespace google {
namespace protobuf {
template<> ::book::AddressBook* Arena::CreateMaybeMessage<::book::AddressBook>(Arena*);
template<> ::book::Person* Arena::CreateMaybeMessage<::book::Person>(Arena*);
template<> ::book::Person_PhoneNumber* Arena::CreateMaybeMessage<::book::Person_PhoneNumber>(Arena*);
}  // namespace protobuf
}  // namespace google
namespace 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<const Person_PhoneNumber*>(
               &_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<Person_PhoneNumber>(NULL);
  }

  Person_PhoneNumber* New(::google::protobuf::Arena* arena) const final {
    return CreateMaybeMessage<Person_PhoneNumber>(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<const Person*>(
               &_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<Person>(NULL);
  }

  Person* New(::google::protobuf::Arena* arena) const final {
    return CreateMaybeMessage<Person>(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<const AddressBook*>(
               &_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<AddressBook>(NULL);
  }

  AddressBook* New(::google::protobuf::Arena* arena) const final {
    return CreateMaybeMessage<AddressBook>(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;
inline 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_CXX11
inline 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)
}
#endif
inline 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<const char*>(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 = 2;
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_CXX11
inline 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)
}
#endif
inline 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<const char*>(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 = 3;
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_CXX11
inline 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)
}
#endif
inline 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<const char*>(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 = 4;
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;
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<const ::google::protobuf::Timestamp*>(
      &::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<::google::protobuf::Timestamp>(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<::google::protobuf::MessageLite*>(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 book

namespace 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_2eproto

4、序列化介面

bool SerializeToString(string* output) const
序列化訊息,將訊息物件以string方式輸出。
bool ParseFromString(const string& data)
反序列化訊息,解析給定的string為訊息物件     
bool SerializeToOstream(ostream* output) const
序列化訊息,將訊息物件寫入給定的c++  ostream中
bool ParseFromIstream(istream* input)
反序列化訊息,從給定的c++ istream中解析出訊息物件

5、使用示例

main.cpp檔案:

#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;

// 需要輸入地址簿檔案作為引數
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 << "Usage:  " << argv[0] << " ADDRESS_BOOK_FILE" << endl;
        return -1;
    }
    /********************************************
     * 反序列化
     * 從地址簿檔案讀取資料
     * *******************************************/
    Book::AddressBook address_book;
    // 從地址簿檔案讀取地址簿資訊
    fstream input(argv[1], ios::in | ios::binary);
    if (!input)
    {
        cout << argv[1] << ": File not found.  Creating a new file." << endl;
    }
    else if (!address_book.ParseFromIstream(&input))
    {
        cerr << "Failed to parse address book." << endl;
        return -1;
    }
    cout << "*********print people**************" << endl;
    for (int i = 0; i < address_book.people_size(); i++)
    {
        const Book::Person& person = address_book.people(i);
        cout << "ID: " << person.id() << endl;
        cout << "Name: " << person.name() << endl;
        cout << "EMail: " << person.email() << endl;
        const Book::Person_PhoneNumber& phone = address_book.people(i).phones(0);
        cout << "PhoneNumber: " << phone.number() << endl;
    }

    /************************************
     * 序列化
     * 地址簿增加一個人並寫入到地址簿檔案
     * *********************************/
    // 地址簿增加一個人
    cout << "*****************add a people**************" << endl;
    Book::Person* person = address_book.add_people();
    person->set_id(10001);
    person->set_name("scorpio");
    person->set_email("[email protected]");
    Book::Person::PhoneNumber* phone = person->add_phones();
    phone->set_number("139xxxxxxxx");
    phone->set_type(Book::Person_PhoneType_HOME);
    // 寫入地址簿檔案
    fstream output(argv[1], ios::out | ios::trunc | ios::binary);
    if (!address_book.SerializeToOstream(&output))
    {
        cerr << "Failed to write address book." << endl;
        return -1;
    }

    /***************************************
     * 清理libprotobuf分配的所有全域性物件
     * ************************************/
    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}
// output:
//*********print people**************
//ID: 10001
//Name: scorpio
//EMail: [email protected]
//PhoneNumber: 139xxxxxxxx
//ID: 10001
//Name: scorpio
//EMail: [email protected]
//PhoneNumber: 139xxxxxxxx
//*****************add a people**************

6、執行結果測試

編譯程式:
g++ addressbook.pb.cc main.cpp -o readwritepkg-config --cflags --libs protobuf-std=c++11
執行程式:
./readwrite book
結果如下:

*********print people**************
ID: 10001
Name: scorpio
EMail: [email protected]
PhoneNumber: 139xxxxxxxx
ID: 10001
Name: scorpio
EMail: [email protected]
PhoneNumber: 139xxxxxxxx
*****************add a people**************

三、Protobuf Go示例

1、定義proto檔案

book/book.proto檔案如下:

syntax="proto3";
package book;

// 出版社
message Publisher{
    string name = 1;
}  
// 書籍資訊
message Book {
     string name = 1;
    message Author {
        string name = 1;
        string address = 2;
    }
    Author author = 3;

    enum BookType{
        SCIENCE = 0;
        LITERATURE = 1;
    }

    BookType type = 4;
    Publisher publisher = 5;
}

2、生成Go程式碼

protoc --go_out=. book.proto
生成book/book.pb.go檔案,內容如下:

package proto

import (
   fmt "fmt"
   proto "github.com/golang/protobuf/proto"
   math "math"
)

// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf

// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package

type Book_BookType int32

const (
   Book_SCIENCE    Book_BookType = 0
   Book_LITERATURE Book_BookType = 1
)

var Book_BookType_name = map[int32]string{
   0: "SCIENCE",
   1: "LITERATURE",
}

var Book_BookType_value = map[string]int32{
   "SCIENCE":    0,
   "LITERATURE": 1,
}

func (x Book_BookType) String() string {
   return proto.EnumName(Book_BookType_name, int32(x))
}

func (Book_BookType) EnumDescriptor() ([]byte, []int) {
   return fileDescriptor_1e89d0eaa98dc5d8, []int{1, 0}
}

// 出版社
type Publisher struct {
   Name                 string   `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
   XXX_NoUnkeyedLiteral struct{} `json:"-"`
   XXX_unrecognized     []byte   `json:"-"`
   XXX_sizecache        int32    `json:"-"`
}

func (m *Publisher) Reset()         { *m = Publisher{} }
func (m *Publisher) String() string { return proto.CompactTextString(m) }
func (*Publisher) ProtoMessage()    {}
func (*Publisher) Descriptor() ([]byte, []int) {
   return fileDescriptor_1e89d0eaa98dc5d8, []int{0}
}

func (m *Publisher) XXX_Unmarshal(b []byte) error {
   return xxx_messageInfo_Publisher.Unmarshal(m, b)
}
func (m *Publisher) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
   return xxx_messageInfo_Publisher.Marshal(b, m, deterministic)
}
func (m *Publisher) XXX_Merge(src proto.Message) {
   xxx_messageInfo_Publisher.Merge(m, src)
}
func (m *Publisher) XXX_Size() int {
   return xxx_messageInfo_Publisher.Size(m)
}
func (m *Publisher) XXX_DiscardUnknown() {
   xxx_messageInfo_Publisher.DiscardUnknown(m)
}

var xxx_messageInfo_Publisher proto.InternalMessageInfo

func (m *Publisher) GetName() string {
   if m != nil {
      return m.Name
   }
   return ""
}

// 書籍資訊
type Book struct {
   Name                 string        `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
   Author               *Book_Author  `protobuf:"bytes,3,opt,name=author,proto3" json:"author,omitempty"`
   Type                 Book_BookType `protobuf:"varint,4,opt,name=type,proto3,enum=book.Book_BookType" json:"type,omitempty"`
   Publisher            *Publisher    `protobuf:"bytes,5,opt,name=publisher,proto3" json:"publisher,omitempty"`
   XXX_NoUnkeyedLiteral struct{}      `json:"-"`
   XXX_unrecognized     []byte        `json:"-"`
   XXX_sizecache        int32         `json:"-"`
}

func (m *Book) Reset()         { *m = Book{} }
func (m *Book) String() string { return proto.CompactTextString(m) }
func (*Book) ProtoMessage()    {}
func (*Book) Descriptor() ([]byte, []int) {
   return fileDescriptor_1e89d0eaa98dc5d8, []int{1}
}

func (m *Book) XXX_Unmarshal(b []byte) error {
   return xxx_messageInfo_Book.Unmarshal(m, b)
}
func (m *Book) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
   return xxx_messageInfo_Book.Marshal(b, m, deterministic)
}
func (m *Book) XXX_Merge(src proto.Message) {
   xxx_messageInfo_Book.Merge(m, src)
}
func (m *Book) XXX_Size() int {
   return xxx_messageInfo_Book.Size(m)
}
func (m *Book) XXX_DiscardUnknown() {
   xxx_messageInfo_Book.DiscardUnknown(m)
}

var xxx_messageInfo_Book proto.InternalMessageInfo

func (m *Book) GetName() string {
   if m != nil {
      return m.Name
   }
   return ""
}

func (m *Book) GetAuthor() *Book_Author {
   if m != nil {
      return m.Author
   }
   return nil
}

func (m *Book) GetType() Book_BookType {
   if m != nil {
      return m.Type
   }
   return Book_SCIENCE
}

func (m *Book) GetPublisher() *Publisher {
   if m != nil {
      return m.Publisher
   }
   return nil
}

type Book_Author struct {
   Name                 string   `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"`
   Address              string   `protobuf:"bytes,2,opt,name=address,proto3" json:"address,omitempty"`
   XXX_NoUnkeyedLiteral struct{} `json:"-"`
   XXX_unrecognized     []byte   `json:"-"`
   XXX_sizecache        int32    `json:"-"`
}

func (m *Book_Author) Reset()         { *m = Book_Author{} }
func (m *Book_Author) String() string { return proto.CompactTextString(m) }
func (*Book_Author) ProtoMessage()    {}
func (*Book_Author) Descriptor() ([]byte, []int) {
   return fileDescriptor_1e89d0eaa98dc5d8, []int{1, 0}
}

func (m *Book_Author) XXX_Unmarshal(b []byte) error {
   return xxx_messageInfo_Book_Author.Unmarshal(m, b)
}
func (m *Book_Author) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) {
   return xxx_messageInfo_Book_Author.Marshal(b, m, deterministic)
}
func (m *Book_Author) XXX_Merge(src proto.Message) {
   xxx_messageInfo_Book_Author.Merge(m, src)
}
func (m *Book_Author) XXX_Size() int {
   return xxx_messageInfo_Book_Author.Size(m)
}
func (m *Book_Author) XXX_DiscardUnknown() {
   xxx_messageInfo_Book_Author.DiscardUnknown(m)
}

var xxx_messageInfo_Book_Author proto.InternalMessageInfo

func (m *Book_Author) GetName() string {
   if m != nil {
      return m.Name
   }
   return ""
}

func (m *Book_Author) GetAddress() string {
   if m != nil {
      return m.Address
   }
   return ""
}

func init() {
   proto.RegisterEnum("book.Book_BookType", Book_BookType_name, Book_BookType_value)
   proto.RegisterType((*Publisher)(nil), "book.Publisher")
   proto.RegisterType((*Book)(nil), "book.Book")
   proto.RegisterType((*Book_Author)(nil), "book.Book.Author")
}

func init() { proto.RegisterFile("book.proto", fileDescriptor_1e89d0eaa98dc5d8) }

var fileDescriptor_1e89d0eaa98dc5d8 = []byte{
   // 222 bytes of a gzipped FileDescriptorProto
   0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x4a, 0xca, 0xcf, 0xcf,
   0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x01, 0xb1, 0x95, 0xe4, 0xb9, 0x38, 0x03, 0x4a,
   0x93, 0x72, 0x32, 0x8b, 0x33, 0x52, 0x8b, 0x84, 0x84, 0xb8, 0x58, 0xf2, 0x12, 0x73, 0x53, 0x25,
   0x18, 0x15, 0x18, 0x35, 0x38, 0x83, 0xc0, 0x6c, 0xa5, 0x7f, 0x8c, 0x5c, 0x2c, 0x4e, 0xf9, 0xf9,
   0xd9, 0xd8, 0x24, 0x85, 0x34, 0xb9, 0xd8, 0x12, 0x4b, 0x4b, 0x32, 0xf2, 0x8b, 0x24, 0x98, 0x15,
   0x18, 0x35, 0xb8, 0x8d, 0x04, 0xf5, 0xc0, 0x16, 0x80, 0xd4, 0xeb, 0x39, 0x82, 0x25, 0x82, 0xa0,
   0x0a, 0x84, 0xd4, 0xb9, 0x58, 0x4a, 0x2a, 0x0b, 0x52, 0x25, 0x58, 0x14, 0x18, 0x35, 0xf8, 0x8c,
   0x84, 0x91, 0x14, 0x82, 0x88, 0x90, 0xca, 0x82, 0xd4, 0x20, 0xb0, 0x02, 0x21, 0x5d, 0x2e, 0xce,
   0x02, 0x98, 0x8b, 0x24, 0x58, 0xc1, 0xc6, 0xf2, 0x43, 0x54, 0xc3, 0x1d, 0x1a, 0x84, 0x50, 0x21,
   0x65, 0xc6, 0xc5, 0x06, 0xb1, 0x09, 0xab, 0x03, 0x25, 0xb8, 0xd8, 0x13, 0x53, 0x52, 0x8a, 0x52,
   0x8b, 0x8b, 0x25, 0x98, 0xc0, 0xc2, 0x30, 0xae, 0x92, 0x3a, 0x17, 0x07, 0xcc, 0x62, 0x21, 0x6e,
   0x2e, 0xf6, 0x60, 0x67, 0x4f, 0x57, 0x3f, 0x67, 0x57, 0x01, 0x06, 0x21, 0x3e, 0x2e, 0x2e, 0x1f,
   0xcf, 0x10, 0xd7, 0x20, 0xc7, 0x90, 0xd0, 0x20, 0x57, 0x01, 0xc6, 0x24, 0x36, 0x70, 0x70, 0x19,
   0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0xca, 0xa2, 0x4d, 0x21, 0x3c, 0x01, 0x00, 0x00,
}

3、使用示例

main.go檔案如下:

package main

import (
   pb "book/proto"
   "github.com/golang/protobuf/proto"
   "fmt"
)

func main(){
   // 將物件轉為proto編碼
   var b = &pb.Book{Name:"HyperLedger Fabric", Author:&pb.Book_Author{Name:"scorpio"}}
   protoBook, err := proto.Marshal(b)
   if err != nil{
      fmt.Println(err.Error())
   }
   fmt.Println(protoBook)

   // proto編碼轉化為物件
   var b2 pb.Book
   err = proto.Unmarshal(protoBook, &b2)
   if err != nil{
      fmt.Println(err.Error())
   }
   fmt.Println(b2.Name,b2.Author)
}
// output:
//[10 18 72 121 112 101 114 76 101 100 103 101 114 32 70 97 98 114 105 99 26 9 10 7 115 99 111 114 112 105 111]
//HyperLedger Fabric name:"scorpio"