protocol buffers 使用方法ios
咱们接下来要使用的例子是一个很是简单的"地址簿"应用程序,它能从文件中读取联系人详细信息。地址簿中的每个人都有一个名字、ID、邮件地址和联系电话。c++
如何序列化和获取结构化的数据?这里有几种解决方案:git
12:3:-23:67
。虽然它须要编写一次性的编码和解码代码且解码须要耗费一点运行时成本,但这是一种简单灵活的方法。这最适合编码很是简单的数据。Protocol buffers 是针对这个问题的一种灵活、高效、自动化的解决方案。使用 Protocol buffers,你须要写一个 .proto
说明,用于描述你所但愿存储的数据结构。利用 .proto
文件,protocol buffer 编译器能够建立一个类,用于实现对高效的二进制格式的 protocol buffer 数据的自动化编码和解码。产生的类提供了构造 protocol buffer 的字段的 getters 和 setters,而且做为一个单元来处理读写 protocol buffer 的细节。重要的是,protocol buffer 格式支持格式的扩展,代码仍然能够读取以旧格式编码的数据。github
安装:
https://github.com/protocolbuffers/protobuf/blob/master/src/README.mdshell
.proto的文件的源代码:微信
// [START declaration] syntax = "proto3"; package tutorial; import "google/protobuf/timestamp.proto"; // [END declaration] // [START messages] message Person { string name = 1; int32 id = 2; // Unique ID number for this person. string email = 3; enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; } message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phones = 4; google.protobuf.Timestamp last_updated = 5; } // Our address book file is just one of these. message AddressBook { repeated Person people = 1; } // [END messages]
这里,咱们对.proto文件所使用的语法进行简单讲解。数据结构
1)protobuf使用的.proto文件以包声明开始,包声明和C++中的namespace对应,在某个包声明中定义的消息,会出如今对应的namespace命名空间中。import语句用来导入其余.proto文件中的消息定义,这样就能够在多个.proto文件中定义消息,而后关联使用了。ide
2)而后,你须要定义消息结构。一个消息包括多个带类型的成员。protobuf有许多标准的简单数据类型,包括bool, int32, float,double以及string, protobuf自带的.proto文件中也有一些消息结构定义,例如上面出现的google.protobuf.Timestamp。固然,你也能够根据这些类型,进一步构造其余消息,例如上面的Person包含了PhoneNumber消息,AddressBook包含了Person消息。你也能够在其余消息中定义消息类型,例如上面出如今PhoneNUmber在Person中进行定义。你还能够定义enum类型,例如上面的PhoneType,包含MOBILE,HOME和WORK三个可选值。布局
“=1”, “=2”是用来在二进制编码中标识对应字段的tag。tag在1-15范围内只须要一个byte来编码,而较大的数字须要两个byte来编码,因此对于经常使用的那些字段,可使用1-15范围内的tag。性能
另外,每个tag可使用以下修饰符修饰:
(1)singular: 表示这个字段能够有一个,也能够没有。若是没有的话,在编码的时候,不会占用空间。
(2)repeated: 表示这个字段会重复0次或者更屡次,这个字段里的值会按照顺序编码。
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/address.proto
其中-I指定proto文件所在的位置,$DST_DIR指定生成文件所在的位置,这里--cpp_out表示生成文件为C++文件,生成目录在$DST_DIR,$SRC_DIR/addressbook.proto。
若是你在proto所在文件调用上述命令,能够简写以下:
protoc --cpp_out=. addressbook.proto
调用上述命令,生成的文件为addressbook.pb.h和addressbook.pb.cc。能够推测,对于xxx.proto,生成文件应该为xxx.pb.h和xxx.pb.cc。
作成pb格式的文件:
#include <ctime> #include <fstream> #include <google/protobuf/util/time_util.h> #include <iostream> #include <string> #include "addressbook.pb.h" using namespace std; using google::protobuf::util::TimeUtil; // This function fills in a Person message based on user input. void PromptForAddress(tutorial::Person* 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::Person::PhoneNumber* 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::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; } } *person->mutable_last_updated() = TimeUtil::SecondsToTimestamp(time(NULL)); } // Main function: Reads the entire address book from a file, // adds one person based on user input, then writes it back out to the same // file. 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; 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 address. 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::protobuf::ShutdownProtobufLibrary(); return 0; }
解析pb文件
#include <fstream> #include <google/protobuf/util/time_util.h> #include <iostream> #include <string> #include "addressbook.pb.h" using namespace std; using google::protobuf::util::TimeUtil; // Iterates though all people in the AddressBook and prints info about them. void ListPeople(const tutorial::AddressBook& address_book) { for (int i = 0; i < address_book.people_size(); i++) { const tutorial::Person& person = address_book.people(i); cout << "Person ID: " << person.id() << endl; cout << " Name: " << person.name() << endl; if (person.email() != "") { cout << " E-mail address: " << person.email() << endl; } for (int j = 0; j < person.phones_size(); j++) { const tutorial::Person::PhoneNumber& phone_number = person.phones(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; default: cout << " Unknown phone #: "; break; } cout << phone_number.number() << endl; } if (person.has_last_updated()) { cout << " Updated: " << TimeUtil::ToString(person.last_updated()) << 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; 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 (!address_book.ParseFromIstream(&input)) { cerr << "Failed to parse address book." << endl; return -1; } } ListPeople(address_book); // Optional: Delete all global objects allocated by libprotobuf. google::protobuf::ShutdownProtobufLibrary(); return 0; }
执行命令:
$ g++ addressbook.pb.cc write.cpp -o write `pkg-config --cflags --libs protobuf`
关于命令pkg-config的用法:https://blog.csdn.net/luotuo44/article/details/24836901
pkg-config可以省略编译时的一堆参数