turn from: Http://www.cellopoint.com/cn/media_resources/blogs/2011/05/Protocol_Buffers
Introduction Introduction
The Protocol buffers is a flexible, efficient, automated mechanism (possibly coding) tool for serializing structural data.
Like XML, but smaller, faster, and simpler. You define your structured data, and then you can use the special code generated by the tool to easily use various languages (c + + python java)
Read and write your structured data from a variety of data streams. You can even renew your data structure (hot deployment) without interrupting a program that has already been deployed.
Why not just use XML? Why not XML?
The protocol buffer has many advantages that XML does not have:
1. Simple;
2. Small: 3-10 times
3. High efficiency: 20-100 times
4. Matchless meaning
5. Automatic tool generation
How do they work?
You just need to install Google's protocol buffer
Edit. Proto File
Perform protoc–cpp_out=. File name
Example: Example 1
Package tutorial;
Message person {
Required String name = 1;
Required Int32 id = 2;
Optional String email = 3;
Enum Phonetype {
MOBILE = 0;
home = 1;
WORK = 2;
}
Message PhoneNumber {
Required String number = 1;
Optional Phonetype type = 2 [default = home];
}
Repeated PhoneNumber phone = 4;
}
Message AddressBook {
Repeated person person = 1;
}
Three type three data types
Required: Field values must be supplied, otherwise the corresponding message will be considered "uninitialized." “
Optional: Either the field value is specified or not. If you do not specify an optional field value, it uses the default value.
Repeated: The field repeats n times (can be 0). The order of the duplicate values will be saved in the buffer in the protocol.
Required is permanent: you should be particularly careful when labeling a field as Required.
start working protoc-i= $SRC _dir--cpp_out= $DST _dir $SRC _dir/addressbook.proto
This will produce the following files
Addressbook.pb.h: Declares the header file of the class you are generating.
addressbook.pb.cc: The actual file of the class you generated.
Example: Example 2
Person of person;
Person.set_name ("John Doe");
PERSON.SET_ID (1234);
Person.set_email ("jdoe@example.com");
FStream output ("MyFile", Ios::out | ios::binary);
Person. Serializetoostream (&output);
FStream input ("myfile", Ios::in | ios::binary);
Person of person;
Person. Parsefromistream (&input);
cout << "Name:" << person.name () << Endl;
cout << "e-mail:" << person.email () << Endl;
Entire message, including: internal API
BOOL IsInitialized () Const: Checks whether all required fields are set (set) values.
String debugstring () const;: Returns a readable message representation that is particularly useful for reading.
void CopyFrom (const person& from);: Overwrite the value inside the caller's message with the value of the external message.
void Clear (): Returns all items to the empty state (empty).
BOOL Serializetostring (string* output) const;: Serializes the message and stores it in the specified string. Note that the contents are binary; we just use strings as a convenient container.
BOOL Parsefromstring (const string& data): Parsing a message from a given string.
BOOL Serializetoostream (ostream* output) const;: Writes the message to the given C + + Ostream.
BOOL Parsefromistream (istream* Input): Parsing the message from the given C + + istream.
Http://code.google.com/intl/zh-TW/apis/protocolbuffers/docs/overview.html
Http://www.cppprog.com/2010/0908/207_4.html