zoukankan      html  css  js  c++  java
  • 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;

      

  • 相关阅读:
    [Linux] Chmod 改变权限
    [linux命令]基本命令
    [Linux命令] 查看目录大小du
    [Linux命令]格式化mkfs
    在VMWare下的Linux切换
    .net的MSMQ异步调用
    CASSINI源代码分析
    [Wix] RadioButton与ListItem的属性要改掉了
    如何快速生成Insert数据插入语句?
    撕纸
  • 原文地址:https://www.cnblogs.com/GameDeveloper/p/2111466.html
Copyright © 2011-2022 走看看