【Protocol Buffer】Protocol Buffer入门教程(七):导入定义

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/dengjin20104042056/article/details/102467050

00. 目录

01. 消息格式

info.proto文件内容如下:

syntax = "proto3";//指定版本信息,不指定会报错

package infopack; //package声明符

message info //message为关键字,作用为定义一种消息类型
{
    string addr = 1;    //地址
    string group = 2;   //分组
}

addressbook.proto文件内容如下,addressbook.proto文件需要导入info.proto文件的内容:

syntax = "proto3";//指定版本信息,不指定会报错

import "info.proto"; //导入定义

package tutorial; //package声明符

message Person //message为关键字,作用为定义一种消息类型
{
    string name = 1;    //姓名
    int32 id = 2;       //id
    string email = 3; //邮件

    enum PhoneType //枚举消息类型
    {
        MOBILE = 0; //proto3版本中,首成员必须为0,成员不应有相同的值
        HOME = 1;
        WORK = 2;
    }

    message PhoneNumber
    {
        string number = 1;
        PhoneType type = 2;
    }

    repeated PhoneNumber phones = 4; //phones为数组

    //info定义在"info.proto"
    //类型格式:包名.信息名
    infopack.info tmp = 5;
}

message AddressBook
{
    repeated Person people = 1;
}

02. 参考程序

#include "addressbook.pb.h"
#include <iostream>
#include <fstream>
using namespace std;

void set_addressbook()
{
    tutorial::AddressBook obj;

    tutorial::Person *p1 = obj.add_people(); //新增加一个Person
    p1->set_name("tom");
    p1->set_id(1);
    p1->set_email("[email protected]");

    tutorial::Person::PhoneNumber *phone1 = p1->add_phones(); //增加一个phone
    phone1->set_number("110");
    phone1->set_type(tutorial::Person::MOBILE);

    tutorial::Person::PhoneNumber *phone2 = p1->add_phones(); //增加一个phone
    phone2->set_number("120");
    phone2->set_type(tutorial::Person::HOME);

    //info addr和group的使用
    infopack::info *p_info = p1->mutable_tmp(); //取出info的对象指针
    p_info->set_addr("China");  //地址
    p_info->set_group("A");     //组

    fstream output("pb.itcast", ios::out | ios::trunc | ios::binary);

    bool flag = obj.SerializeToOstream(&output);//序列化
    if (!flag)
    {
        cerr << "Failed to write file." << endl;
        return;
    }

    output.close();//关闭文件
}

void get_addressbook()
{
    tutorial::AddressBook obj;
    fstream input("./pb.itcast", ios::in | ios::binary);
    obj.ParseFromIstream(&input);  //反序列化
    input.close(); //关闭文件

    for (int i = 0; i < obj.people_size(); i++)
    {
        const tutorial::Person& person = obj.people(i);//取第i个people
        cout << "第" << i + 1 << "个信息\n";
        cout << "name = " << person.name() << endl;
        cout << "id = " << person.id() << endl;
        cout << "email = " << person.email() << endl;

        for (int j = 0; j < person.phones_size(); j++)
        {
            const tutorial::Person::PhoneNumber& phone_number = person.phones(j);

            switch (phone_number.type())
            {
            case tutorial::Person::MOBILE:
                cout << "  Mobile phone #: ";
                break;
            case tutorial::Person::HOME:
                cout << "  Home phone #: ";
                break;
            case tutorial::Person::WORK:
                cout << "  Work phone #: ";
                break;
            }

            cout << phone_number.number() << endl;
        }

        //info addr和group的使用
        infopack::info info = person.tmp(); //取出info的对象
        cout << "addr = " << info.addr() << endl;
        cout << "group = " << info.group() << endl;

        cout << endl;
    }
}

int main()
{
    // 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;

    set_addressbook(); //序列化
    get_addressbook(); //反序列化

    // Optional:  Delete all global objects allocated by libprotobuf.
    google::protobuf::ShutdownProtobufLibrary();

    return 0;
}

03. 编译测试

deng@itcast:/mnt/hgfs/LinuxHome/day03$ protoc addressbook.proto info.proto --cpp_out=./
deng@itcast:/mnt/hgfs/LinuxHome/day03$ ./a.out 
第1个信息
name = tom
id = 1
email = [email protected]
  Mobile phone #: 110
  Home phone #: 120

deng@itcast:/mnt/hgfs/LinuxHome/day03$ 

在这里插入图片描述

04. 附录

测试程序下载:

猜你喜欢

转载自blog.csdn.net/dengjin20104042056/article/details/102467050