google protobuf 使用心德


最近我们开发过程中用到 google/protobuf,因为之前没有接触过。网上的资料都是互相复制转载的。基本上都是讲 protobuf 怎么赋值怎么取值。。。

主要的消息映射讲的微乎其微。。。。。我根据网上说的做了下。。。问题我纠缠死了。。。-_-#  OK  看代码

 1 GOOGLE_PROTOBUF_VERIFY_VERSION;
2 string str = "";
3
4 tutorial::AddressBook address_book;
5 tutorial::Person *person = tutorial::Person::default_instance().New();
6 person->set_name("Ambition");
7 person->set_id(1000);
8 person->set_email("ambitiongxb@foxmail.com");
9 for (int i=0;i<4;i++)
10 {
11 tutorial::Person::PhoneNumber* phone_number = person->add_phone();
12 phone_number->set_number("13800138000");
13 if (i == 0)
14 phone_number->set_type(tutorial::Person::HOME);
15 else if(i==1)
16 phone_number->set_type(tutorial::Person::WORK);
17 else if(i==2)
18 phone_number->set_type(tutorial::Person::MOBILE);
19 }

  上面的代码就是赋值操作。。。下面说下消息映射。。。

1 person->SerializeToString(&str); //序列化
2 typedef tutorial::Person T;
3 std::string type_name = T::descriptor()->full_name(); // 得到包名字
4 ::google::protobuf::Message* prototype = createMessage(type_name); //根据包名得到(抽象)消息
5 prototype->ParseFromString(str); //反序列化
6 tutorial::Person *new_obj = (tutorial::Person*)prototype;

  

posted on 2011-07-20 11:38  游戏开发:主席  阅读(950)  评论(0编辑  收藏  举报

导航