站长资讯 | 站长常用软件 | 站长工具 | 为奥运祝福!
文章投稿 当前位置:主页>新闻资讯>业界动态>文章:第一个 Protocol Buffers 小程序:电话本

第一个 Protocol Buffers 小程序:电话本

来源:http://hellobmw.com/archives/protocol-buffers-base 作者: 发布时间:2008-07-09 阅读次数:  

晚上刚写了一篇《Protocol Buffers 简介》,然后就开始动手实战试用一下 Protocol Buffers 这个 Google 出品的东东。

首先从 Protocol Buffers 的主页上把源码下回来,注重是源码,那个 Win32 包不必下,它里面只有一个编译好的 protoc.exe,没有开发所必须的库,光有那个 Protocol Buffers 编译器是没用。

将源码解压出来以后,直接到 vsprojects 目录下用 Visual Studio 2005(其它版本我没有试)打开解决方案文件 protobuf.sln 开始编译。编译成功结束后会在解决方案目录下生成一个 google 目录,里面是单元测试的程序;另外就是 Debug 目录和 Release 目录了,生成的对应版本的 .lib 和 .dll 还有 protoc.exe 编译器全在里面。

那么,头文件呢?在解决方案目录下有一个 extract_includes.bat 文件看到了吗,执行后会生成一个 include 目录,里面就是使用 Protocol Buffers 开发必须的所有头文件了。

HiaHia~ 有了这几样东西,Protocol Buffers 这个强大武器才算是真正到手了。在动手写我们自己的程序之前,为了方便,你也可以把库文件和头文件单独存放在别处,然后在 VC 里设置 VC++ 目录就行了,还有别忘了把 .dll 所在的目录加到系统的 Path 环境变量中。好了,现在我们可以开始动手写自己的程序啦!其实这个程序就是 Protocol Buffers 文档中的电话本例子,只不过我自己试了一遍而已。

第一步,写一个 .proto 文件描述数据结构:

// addressbook.proto
//

package tutorial;

option java_package = %26quot;com.example.tutorial%26quot;;
option java_outer_classname = %26quot;AddressBookProtos%26quot;;

message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
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;
}

// Our address book file is just one of these.
message AddressBook {
repeated Person person = 1;
}

第二步,使用命令 protoc.exe -I=. --cpp_out=. addressbook.proto 编译 addressbook.proto,或者方便起见,在 VC 里给 addressbook.proto 设置自定义生成步骤,命令还是这一行,输出文件设置为 addressbook.pb.h;addressbook.pb.cc。

第三步,新建 testProtocolBuffers.cpp,写入下面的代码:

// testProtocolBuffers.cpp : Defines the entry point for the console application.
//

#include %26lt;iostream%26gt;
#include %26lt;fstream%26gt;
#include %26lt;string%26gt;
#include %26quot;addressbook.pb.h%26quot;
#pragma comment(lib, %26quot;libprotobuf.lib%26quot;)

void PromptForAddress(tutorial::Person* person);
void ListPeople(const tutorial::AddressBook%26amp; addressbook);

int AddPerson();
int ViewPerson();

int _tmain(int argc, _TCHAR* argv[])
{
AddPerson();
ViewPerson();
return 0;
}

void PromptForAddress(tutorial::Person* person)
{
std::cout %26lt;%26lt;%26quot;Enter person ID number: %26quot;;
int id;
std::cin%26gt;%26gt; id;
person-%26gt;set_id(id);
std::cin.ignore(256, '\n');

std::cout %26lt;%26lt;%26quot;Enter name: %26quot;;
std::getline(std::cin, *person-%26gt;mutable_name());

std::cout %26lt;%26lt;%26quot;Enter email address (blank for none): %26quot;;
std::string email;
std::getline(std::cin, email);
if (!email.empty())
person-%26gt;set_email(email);

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

tutorial::Person::PhoneNumber* phonenum = person-%26gt;add_phone();
phonenum-%26gt;set_number(number);

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

void ListPeople(const tutorial::AddressBook%26amp; addressbook)
{
for (int i = 0; i %26lt;addressbook.person_size(); i++)
{
const tutorial::Person%26amp; person = addressbook.person(i);
std::cout %26lt;%26lt;%26quot;Person ID: %26quot; %26lt;%26lt;person.id() %26lt;%26lt;std::endl;
std::cout %26lt;%26lt;%26quot;Name: %26quot; %26lt;%26lt;person.name() %26lt;%26lt;std::endl;

文章地址:   http://www.xinasp.com/html/wangzhanyunying/yumingxinwen/20080709/36988.shtml
tag:一个 Protocol Buffers 程序 电话

共2页: 上一页 1 [2] 下一页
    评论加载中…
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 成员列表
© CopyRight 2002-2008, XINASP.COM, Inc. All Rights Reserved 客服QQ:762264 MAIL:QESY#163.COM
浙ICP备06014044号