您的位置:首页 > Web前端

google protocol buffer试用

2011-01-27 21:08 399 查看
http://code.google.com/p/protobuf/上下载protobuf-2.3.0,解压后,打开vsprojects文件夹,然后用VS2005打开protobuf开始编译,然后生成libprotobuf,libprotobuf-lite,libprotoc三个lib,同时生成了include文件夹。。。具体操作看vsprojects下面的readme文件来看。。。。

之后新建一个记事本txt文件(book.txt)在里面写

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

message AddressBook {
repeated Person person = 1;
}

然后在dos命令行下对该文件进行重命名:rename book.txt book.proto,然后在dos命令行下protoc.exe -I=. --cpp_out=. book.proto,就可以生成两个文件book.pb.cpp, book.pb.h。

代码:(转载)

void PromptForAddress(tutorial::Person* person);
void ListPeople(const tutorial::AddressBook& addressbook);

int AddPerson();
int ViewPerson();

int main()
{

AddPerson();
ViewPerson();
return 0;
}

void PromptForAddress(tutorial::Person* person)
{
std::cout <<"Enter person ID number: ";
int id;
std::cin>> id;
person->set_id(id);
std::cin.ignore(256, '/n');

std::cout <<"Enter name: ";
std::getline(std::cin, *person->mutable_name());

std::cout <<"Enter email address (blank for none): ";
std::string email;
std::getline(std::cin, email);
if (!email.empty())
person->set_email(email);

while (true)
{
std::cout <<"Enter a phone number (or leave blank to finish): ";
std::string number;
std::getline(std::cin, number);
if (number.empty())
break;

tutorial::Person::PhoneNumber* phonenum = person->add_phone();
phonenum->set_number(number);

std::cout <<"Is it a mobile, home or work phone? ";
std::string type;
std::getline(std::cin, type);
if ("mobile" == type)
phonenum->set_type(tutorial::Person::MOBILE);
else if ("home" == type)
phonenum->set_type(tutorial::Person::HOME);
else if ("work" == type)
phonenum->set_type(tutorial::Person::WORK);
else
std::cout <<"Unknown phone type. Using default." <<std::endl;
}
}

void ListPeople(const tutorial::AddressBook& addressbook)
{
for (int i = 0; i <addressbook.person_size(); i++)
{
const tutorial::Person& person = addressbook.person(i);
std::cout <<"Person ID: " <<person.id() <<std::endl;
std::cout <<"Name: " <<person.name() <<std::endl;
if (person.has_email())
std::cout <<"Email: " <<person.email() <<std::endl;

for (int j = 0; j <person.phone_size(); j++)
{
const tutorial::Person::PhoneNumber& phonenum = person.phone(j);
switch (phonenum.type())
{
case tutorial::Person::MOBILE:
std::cout <<"Mobile phone #: ";
break;
case tutorial::Person::HOME:
std::cout <<"Home phone #: ";
break;
case tutorial::Person::WORK:
std::cout <<"Work phone #: ";
break;
}
std::cout <<phonenum.number() <<std::endl;
}
}
}

int AddPerson()
{
tutorial::AddressBook addressbook;
const std::string& filename = "addressbook";
std::fstream input(filename.c_str(), ios::in | ios::binary);
if (!input)
std::cout <<filename <<": File not found. Creating a new file." <<std::endl;
else if (!addressbook.ParseFromIstream(&input))
{
std::cerr <<"Failed to parse address book." <<std::endl;
return -1;
}
PromptForAddress(addressbook.add_person());
std::fstream output(filename.c_str(), ios::out | ios::trunc | ios::binary);
if (!addressbook.SerializeToOstream(&output))
{
std::cerr <<"Failed to write address book." <<std::endl;
return -1;
}
return 0;
}

int ViewPerson()
{
tutorial::AddressBook addressbook;
const std::string& filename = "addressbook";
std::fstream input(filename.c_str(), ios::in | ios::binary);
if (!addressbook.ParseFromIstream(&input))
{
std::cerr <<"Failed to parse address book." <<std::endl;
return -1;
}
ListPeople(addressbook);
return 0;
}

注意:编译库的时候是使用的debug非release。。。

把工程设置成:C C++;代码生成;多线程DLL(/MD)才行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: