Protocol Buffer C++实例教程: 发送消息
现在让我们尝试使用您的protocol buffer类。你希望地址簿程序能够做的第一件事就是将个人详细信息写入地址簿文件。为此,你需要创建并填充protocol buffer类的实例,然后将它们写入输出流。
这个程序从文件中AddressBook,根据用户输入向其中添加一个新的Person,并将新的AddressBook再次写回到文件中。下面是代码:
#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::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;
}
}
}
// 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;
}请注意GOOGLE_PROTOBUF_VERIFY_VERSION宏。在使用C++协议缓冲库之前,执行这个宏是一个很好的实践——虽然不是绝对必要的。它验证您没有意外地链接到与您编译的标题版本不兼容的库版本。如果检测到版本不匹配,程序将中止。请注意,每个. pb.cc文件都会在启动时自动调用该宏。
另外,请注意在程序结束时对ShutdownProtobufLibrary()的调用。所有这些都是销毁由Protocol Buffer库分配的所有全局对象。对于大多数程序来说,这是不必要的,因为无论如何这个进程都将退出,操作系统将负责回收所有的内存。但是,如果您使用的内存泄漏检查器要求释放最后一个对象,或者如果您正在编写一个库,该库可能由一个进程多次加载和卸载,那么您可能希望强制Protocol Buffer销毁所有内容。