protocol buffer

doc from:
https://developers.google.com/protocol-buffers/docs/cpptutorial

compilation:
for .proto file:
protoc -I=$SRC_DIR –cpp_out=$DST_DIR $SRC_DIR/addressbook.proto

for .cpp file:

#include “…pb.h”
g++ tutorial/readAddressbook.cpp tutorial/addressbook.pb.cc -o ./tutorial/read -lprotobuf

proto.buffer

package .. => namespace
message .. => class in the namespace
some const and enum, basic function

.pb.h file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// in proto file
package tutorial
message Person {
....
// declare field:`field_type field_name`
// eg. id, email, phone, name
......

message PhoneNumber
}

message AddressBook{
Person ..name_of_variable
}

// in proto.h file
namespace tutorial{

// declare some methods and variables, and class


class AddressBook
class Person
class Person_PhoneNumber

// definition:
class Person{
public:
....
// implements message --------

/** standard message methods:
* check or manipulate the entire msg
**/

bool IsInitialized() const; // checks if all required field is set

string DebugString() const; // return useful msg for debug

void CopyFrom(const Person& from); // overwrites the message with the given message's values.

void Clear();: clears all the elements back to the empty state.

// nested types ----------
typedef ...

// accesssors ----- methods for each field of msg Person declared in .proto file

// required int32 id = 2
# id
inline bool has_id() const;
inline void clear_id();
static const int kIdFieldNumber = 2; // id
inline int32_t id() const;
inline void set_id(int32_t value);
// type int only have the most basic method:`has_`, `clear_`, `set_`

// required string email = 3
# email: methods for email: `has_`, `clear_`, `set_`, mutable
inline bool has_email() const;
inline void clear_email();
static const int kEmailFieldNumber = 3;
inline const ::std::string& email() const;
inline void set_email(const ::std::string& value);
inline void set_email(const char* value);
inline ::std::string* mutable_email(); // spetial method for `string`

// define in subMsg `PhoneNumber` repreated ? phone = 4;
// repeated .tutotial.Person.PhoneNumber phone = 4;
# phone: methods for phone: size
// _size, clear_, id method:
inline int phone_size() const; // spetial method for `repeated`
inline void clear_phone();
static const int kPhoneFieldNumber = 4;
// constructor
inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const;
// mutable_, add_
inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phone();
inline const ::tutorial::Person_PhoneNumber& phone(int index) const;
inline ::tutorial::Person_PhoneNumber* mutable_phone(int index);
inline ::tutorial::Person_PhoneNumber* add_phone();
}

private:
....
// end class
}

.cpp file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "addressbook.pb.h"

int main(){
// initial
tutrorial::AddressBook address_book;
// class AddressBook have field person, type: msg Person
// therefore have method: `add_person`
PromptForAddress(address_book.add_person())

// address_book.add_person() is a pointer to object of class Person
}

void PromptForAddress(tutorial::Person* person){
person->set_id(2);
tutorial::Person::PhoneNumber* phone_number = person->add_phone();
phone_number->set_number(number);
phone_number->set_type(tutorial::Person::MOBILE);

}