Google's Protobuf technology detailed

Source: Internet
Author: User
1. Examples

Official examples of establishing a Test.proto content are as follows:

Syntax = "Proto2";

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 phones = 4;
}

Message AddressBook {
  repeated person people = 1;
}
2. Note 1, first of all. proto file should have a package declaration; 2, the message can be understood as a class, the variable can use the following declaration "bool, Int32, float, double, string", while the message can be nested definition. 3, in the message can be defined enum. 4, the number in the message = 1, or = 2 is for binary independent encoding. 1-15 occupy less space, can improve efficiency. After 16 occupy the space is big, the efficiency is low. Therefore, the commonly used variables are defined as 1-15 to improve efficiency. 5. Each filed in the message must have prefix-modifiers, which can be "required,optional,repeated". 3. Results

protoc–cpp_out= $DST _dir Your/*.proto
***.pb.h and ***.pb.cc can be generated and the APIs are generated after compilation.

Getters have exactly the name as the field in lowercase, and the setter methods begin with set_
There are also has_ methods for each singular (required or optional) field which return true if this field has been set. Finally, each field has a clear_ the "un-sets" field back to its empty state.
String and the Mutable_ method returns a pointer. 4. Calling Methods

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

Std
This function fills in a person message based on user input.
  void promptforaddress (Tutorial::P erson* person) {cout << ' Enter person ID number: ';
  int id;
  CIN >> ID;
  person->set_id (ID);

  Cin.ignore (256, ' \ n ');
  cout << "Enter name:";

  Getline (CIN, *person->mutable_name ());
  cout << "Enter email address (blank for none):";
  string email;
  Getline (cin, email);
  if (!email.empty ()) {person->set_email (email);
    } while (true) {cout << Enter a phone number (or leave blank to finish): ";
    string number;
    Getline (cin, number);
    if (Number.empty ()) {break;
    Tutorial::P erson::P honenumber* phone_number = Person->add_phones ();

    Phone_number->set_number (number);
    cout << "Is this a mobile, home, or work phone?";
    String type;
   Getline (cin, type); if (type = = "mobile") {Phone_number->set_type (tutorial::P erson::mobile);
    else if (type = = "Home") {Phone_number->set_type (tutorial::P erson::home);
    else if (type = = "Work") {Phone_number->set_type (tutorial::P erson::work);  else {cout << "Unknown phone type.
    Using default. "<< Endl; }}//Main function:reads the entire address book from a file,//Adds one person based on user input, then writ
Es it same//file. int main (int argc, char* argv[]) {//Verify that version of the library this we linked against IS//compatible W
  ith 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;

  } Tutorial::addressbook Address_book;
    {//Read the existing 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;
  }//Add an.

  Promptforaddress (Address_book.add_people ());
    {//Write the new address book back to disk.
    FStream output (argv[1], ios::out | ios::trunc | ios::binary); if (!address_book.
      Serializetoostream (&output)) {cerr << "Failed to write Address Book." << Endl;
    return-1;
  }//Optional:delete All global objects allocated by LIBPROTOBUF.

  Google::p rotobuf::shutdownprotobuflibrary ();
return 0; }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.