Google google protobuf usage examples __protobuf

Source: Internet
Author: User

Here is an introduction to paste http://blog.csdn.net/hailong0715/article/details/52016682

Here on how to install the installation of the Caffe installed together, here to introduce how to use this library, Caffe use him as a data transmission means he soon

A new proto defines the structure of the data transfer, which is similar to the data transmission on ROS.

Syntax = "Proto2";
Package Caffe; Domain Name message person
{  
  Required String name = 1;  
  Required Int32 age = 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];  
  }  
  
  Optional PhoneNumber phone = 4; 
  
}  

Package refers to the domain name, which is Std.

Write.cpp

#include <iostream>  
#include <fstream>  
#include "caffe.pb.h"  
  
using namespace std;  
using namespace Caffe;  
  
int main () {person by person  
    ;  
  
    Person.set_name ("Flamingo");     
    Person.set_age ();   
    Person.set_email ("majianfei1023@gmail.com");  
    Person.mutable_phone ()->set_number ("135525");
    
    Write  
    fstream output ("./log", Ios::out | ios::trunc | ios::binary);  
  
    if (!person. Serializetoostream (&output)) {  
        cerr << "Failed to write Msg." << Endl;  
        return-1;  
    }  
  
    System ("pause");  
    return 0;  
}  

In fact, I was looking at the functions defined in the caffe.pb.h he produced, and found that these functions can be used directly

Read.cpp

#include <iostream>  
#include <fstream>  
#include "caffe.pb.h"  
  

using namespace std;  
using namespace Caffe;  
  
void Printinfo (const person & person) {   
    cout << person.name () << Endl;   
    cout << person.age () << Endl;   
    cout << person.email () << Endl;  
    cout << Person.phone (). Number () << Endl; 
    cout << Person.phone (). Type () << Endl;   
  
int main () {person by person  
    ;    
  
    FStream input ("./log", Ios::in | ios::binary);  
      
    if (!person. Parsefromistream (&input)) {  
        cerr << "Failed to parse Address Book." << Endl;  
        return-1;  
    }  
  
    Printinfo (person);  

    return 0;  
}
Then compile and join the library.
Protoc--cpp_out=./Caffe.proto   #在当前目录生成. h and. CC
g++ write.cpp caffe.pb.cc-o write-lprotobuf
g++ Read.cpp Caffe.pb.cc-o Read-lprotobuf
This digest from: http://blog.csdn.net/cai13160674275/article/details/71214552








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.