Ggoogle Protocol Buffer的使用 (基于C++语言)
首先说明的是Protocol Buffle是灵活高效的.它的一个很好的优点(很重要的,我认为)就是后向兼容性——当我们扩展了了.proto文件后,我们照样可以用它来读取之前生成的文件。
之前已经写了关于.proto文件定义的相关知识,见http://www.cnblogs.com/yinheyi/p/6080244.html,现在写基于C语言的Ggoogle Protocol Buffer的使用.
下面的例子,为了实现我们的一个应用 :通讯录,它包含一个人的名字,ID,电子邮件,联系电话,并实现存储与读取。
第一步,定义一个 .proto 文件
/* 首先是一个 package名称的声明,它的作用就是防止出现名字的冲突; 当生成C++代码的时候,对应的pacage名称会生成一个相同名字的命名空间。 */ package tutorial; message Person { required ; required int32 id = ; optional ; enum PhoneType { MOBILE = ; HOME = ; WORK = ; } message PhoneNumber { required ; optional PhoneType type = [default = HOME]; } repeated PhoneNumber phone = ; } message AddressBook { repeated Person person = ; }
第二步:用生成protobuf 编译器编译写的.proto文件。
命令为:
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto其中,-I=$SRC_DIR,表示指明源文件的目录,也可以用 –proto_path, 它们是等价的。当我们有import .proto文件时,它也会从这里指定的目录去搜索。
-cpp_out=$DST_DIR, –cpp_out,表示我们输出的是C++类型的代码,$DST_DIR表示输出的目录。
后面的$SRC_DIR/addressbook.proto表示我们经编译的源文件,它在-I指定的目录下的哦;
运行完命令后,会输出.pb.cc文件与.pb.h文件。
例如,我的.proto文件名为:tutorial.addressbook.proto, 那么我生成的文件为: tutorial.addressbook.pb.h和tutorial.addressbook.pb.cc文件。
第三步,在我们写的程序中调用Protocol Buffe的C++的API来使用我们定义的AddressBook类。
在我们想知道怎么使用它的时候,不防先看看生成的文件里面是什么,让我们打开 tutorial.addressbook.bh.h文件,比如Person类开定义,我们会发现如下的代码:
// accessors ------------------------------------------------------- // required string name = 1; inline bool has_name() const; inline void clear_name(); ; inline const ::std::string& name() const; inline void set_name(const ::std::string& value); inline void set_name(const char* value); inline void set_name(const char* value, size_t size); inline ::std::string* mutable_name(); inline ::std::string* release_name(); inline void set_allocated_name(::std::string* name); // required int32 id = 2; inline bool has_id() const; inline void clear_id(); ; inline ::google::protobuf::int32 id() const; inline void set_id(::google::protobuf::int32 value); // optional string email = 3; inline bool has_email() const; inline void clear_email(); ; inline const ::std::string& email() const; inline void set_email(const ::std::string& value); inline void set_email(const char* value); inline void set_email(const char* value, size_t size); inline ::std::string* mutable_email(); inline ::std::string* release_email(); inline void set_allocated_email(::std::string* email); // repeated .tutorial.Person.PhoneNumber phone = 4; inline int phone_size() const; inline void clear_phone(); ; inline const ::tutorial::Person_PhoneNumber& phone(int index) const; inline ::tutorial::Person_PhoneNumber* mutable_phone(int index); inline ::tutorial::Person_PhoneNumber* add_phone(); inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const; inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phone();上面它就是定义了访问Person类成员函数的方法。如,可以通过name()方法得到它的值。可以通过set_name()方法设置它的值。 通过has_name()判断是否有这个值,通过clear_name()清空它的值。
另外,string类型也numeic类型相比较,还多出了几项,如通过mutable_name()可以得到指针的。
还有,对于repeated的关键字的来说 ,又有一些不同。如,可以通过name_size()得到有多个项(就是它因为可以重复嘛,重复了多少次呢),也可以通过 index 得到特定的第几个的值,等等。
如果想要详细看的话,可以https://developers.google.com/protocol-buffers/docs/reference/cpp-generated看这里哈。
它,也提供了可以能message进行全局操作的标准message 方法, 如:
bool IsInitialized() const; 用于检查是不是所有的required 字段已经初始化、
string DebugString() const; 返回一个人可以读懂的representation of the message,在debug时,很有用的。
void CopyFrom(const Person& from), 用另一个类为一个类赋值;
void Clear() 把类中的所有元素的值变为空状态(unset)
解析与序列化(就是读与写啦)
如:bool SerializeToString(string* output) const ,序列化我们的Message,并把得到的bytes放到string类型中。(注意,这里的bytes仍然是二进制文件,我们只不过使用string作为一个容器来装我们的bytes.)
bool ParseFromString(const string& data); 解析;
bool SerializeToOstream(ostream* output) const;
bool ParseFromIstream(istream* input);
编写我们自己的程序:
写文件的程序:
#include<iostream>
#include<fstream>
#include<string>
$include "tutorial.addressbook.pb.h"
using namespace std;
//定义一个函数,用于设置person对象的值;
//参数为: 一个person 对象的指针; 返回为空;
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number :";
int id;
cin >> id;
person->set_id(id);
cin.ignore(, '\n') //作用:用于清除缓冲区的内容,防止对下次输入产生影响.
cout << "Enter name: ";
getline(cin, *person->mutable_name()); //mutable_name()返回一个指针,再加上*,要干什么?看来要补补getline()函数了;
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::Person::PhoneNumber* phone_number = person->add_phone();
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::Person::MOBILE);
} else if (type == "home") {
phone_number->set_type(tutorial::Person::HOME);
} else if (type =="work") {
phone_number->set_type(tutorial::Person::WORK);
} else {
cout << "Unknown phone type. Using default." <<endl;
}
}
}
int main(int argc, char* argv[]) {
GOOGLE_PROTOBUF_VERIFY_VERSION; //这是一个宏命令,用于检查看看用的protobuf的库是否兼容;最好加上它;
) {
cerr << ] << "ADDRESS_BOOK_FILE" << endl;
;
}
tutorial::AddressBook address_book;
//读文件,如果不存在,就创建;
fstream input(argv[], ios::in | ios::binary);
if (!input) {
cout << argv[] << ":File not found, Creating a new file." <<endl;
} else if (!address_book.ParseFromeIstream(&input)) {
cerr << "Failed to parse address book." << endl;
;
}
//增加address;
PromptForAddress(address_book.add_person());
//写放盘中;
fstream output(argv[], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failed to write address book." << endl;
;
}
//用于把全局的由libprotobuf申请的对象空间;
google::protobuf::ShutdownProtobufLibrary();
;
}
GOOGLE_PROTOBUF_VERIFY_VERSION是一个宏命令,我们最好加上它,因为它的作用可以用于检查我们的library是否不兼的问题。如果 你不想加,也没有办法哦;
ShutdownProtobufLibrary() 它的作用为:delete any global objects that were allocated by the Protocol Buffer library.通常或许我们不用加它,因为我们运行完我们的程序,我们就退出了,然后操作系统会回收内存的。但是如果不加的话,当我们用内存泄漏检查器检查程序时,它会说内存泄漏的。
读取文件的程序:
#include <iostream>
#include <fstream>
#include <string>
#include "addressbook.pb.h"
using namespace std;
// Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook& address_book) {
; i < address_book.person_size(); i++) {
const tutorial::Person& person = address_book.person(i);
cout << "Person ID: " << person.id() << endl;
cout << " Name: " << person.name() << endl;
if (person.has_email()) {
cout << " E-mail address: " << person.email() << endl;
}
; j < person.phone_size(); j++) {
const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
switch (phone_number.type()) {
case tutorial::Person::MOBILE:
cout << " Mobile phone #: ";
break;
case tutorial::Person::HOME:
cout << " Home phone #: ";
break;
case tutorial::Person::WORK:
cout << " Work phone #: ";
break;
}
cout << phone_number.number() << endl;
}
}
}
// Main function: Reads the entire address book from a file and prints all
// the information inside.
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;
) {
cerr << ] << " ADDRESS_BOOK_FILE" << endl;
;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Failed to parse address book." << endl;
;
}
}
ListPeople(address_book);
// Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
;
}
很简单吧;
另外,说一点代码风格:
1. 消息名字:大写字母开头的驼峰表示法表示消息名称,如:SongServerRequest。
2. 字段名: 使用小写字母+下划线分隔表示字段名,如:song_name。
message SongServerRequest {
required ;
}
3. 枚举类型:大写字母开头的驼峰表示法表示枚举名称,使用大写字母+下划线表示值
enum Foo {
FIRST_VALUE = ;
SECOND_VALUE = ;
}
参考:
https://developers.google.com/protocol-buffers/docs/cpptutorial
https://my.oschina.net/macwe/blog/157862
最后,如果你觉得以上内容对你提供了实质的帮助, 如果你有钱的话,如果你愿意的话,可以打赏一下我这个穷学生哦,资助我买几本书.
钱也不用太多哦, 您可以选择:2毛,5毛、1元。(靠知识创造价值, 靠知识改变生活 ——爱你们的杰克船长,么么哒)

Ggoogle Protocol Buffer的使用 (基于C++语言)的更多相关文章
- gRPC in ASP.NET Core 3.x -- Protocol Buffer(2)Go语言的例子(下)
第一篇文章(大约半年前写的):https://www.cnblogs.com/cgzl/p/11246324.html gRPC in ASP.NET Core 3.x -- Protocol Buf ...
- gRPC in ASP.NET Core 3.x -- Protocol Buffer(2)Go语言的例子(上)
上一篇文章(大约半年前写的):https://www.cnblogs.com/cgzl/p/11246324.html 建立Go项目 在GOPATH的src下面建立一个文件夹 protobuf-go, ...
- Protocol Buffer技术详解(语言规范)
Protocol Buffer技术详解(语言规范) 该系列Blog的内容主体主要源自于Protocol Buffer的官方文档,而代码示例则抽取于当前正在开发的一个公司内部项目的Demo.这样做的目的 ...
- Hadoop基于Protocol Buffer的RPC实现代码分析-Server端
http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.google.co ...
- Hadoop基于Protocol Buffer的RPC实现代码分析-Server端--转载
原文地址:http://yanbohappy.sinaapp.com/?p=110 最新版本的Hadoop代码中已经默认了Protocol buffer(以下简称PB,http://code.goog ...
- 在 go/golang语言中使用 google Protocol Buffer
怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{ ...
- 一个基于protocol buffer的RPC实现
Protocol Buffer仅仅是提供了一套序列化和反序列化结构数据的机制,本身不具有RPC功能,但是可以基于其实现一套RPC框架. Services protocol buffer的Service ...
- 从零开始山寨Caffe·伍:Protocol Buffer简易指南
你为Class外访问private对象而苦恼嘛?你为设计序列化格式而头疼嘛? ——欢迎体验Google Protocol Buffer 面向对象之封装性 历史遗留问题 面向对象中最矛盾的一个特性,就是 ...
- Google Protocol Buffer 的使用和原理[转]
本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...
随机推荐
- Python开发【第九章】:线程、进程和协程
一.线程 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执行不同的任务 1.t ...
- 深入浅出Docker(一):Docker核心技术预览
Docker是PaaS供应商dotCloud开源的一个基于LXC 的高级容器引擎,源代码托管在 GitHub 上, 基于Go语言开发并遵从Apache 2.0协议开源.Docker提供了一种在安全.可 ...
- synchronized同步块和volatile同步变量
Java语言包含两种内在的同步机制:同步块(或方法)和 volatile 变量.这两种机制的提出都是为了实现代码线程的安全性.其中 Volatile 变量的同步性较差(但有时它更简单并且开销更低),而 ...
- 20145320《Java程序设计》第一次实验报告
20145320<Java程序设计>第一次实验报告 北京电子科技学院(BESTI)实验报告 课程:Java程序设计 班级:1453 指导教师:娄嘉鹏 实验日期:2016.04.08 18: ...
- js实现继承的五种方式
function Parent(firstname) { this.fname=firstname; ; this.sayAge=function() { console.log(this.age); ...
- 设置IE默认文本模式的方法
设置IE默认文本模式的方法 <meta http-equiv="X-UA-Compatible" content="IE=8" /> IE=5.6. ...
- 重新安装了mysql,以前的数据库如何导入到新的数据库
重新安装了mysql,以前的数据库如何导入到新的数据库,导入到新的数据库不能用真么办? 将之前的mysql中的data目录中的数据库文件夹,(需要哪个数据库复制哪个,不要都复制) D:/wamp/bi ...
- MVC过滤器使用案例:统一处理异常顺道精简代码
重构的乐趣在于精简代码,模块化设计,解耦功能……而对异常处理的重构则刚好满足上述三个方面,下面是我的一点小心得. 一.相关的学习 在文章<精简自己20%的代码>中,讨论了异常的统一处理,并 ...
- 阿里云OneinStack数据库相关
阿里云OneinStack数据库相关必须进入oneinstack目录下执行相关命令 ===================================源码安装目录: Nginx:/usr/loca ...
- Map.Entry
Map.Entry Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法,keySet()方法返回值是Map中 ...