码迷,mamicode.com
首页 > 其他好文 > 详细

ProtoBuf练习(三)

时间:2018-05-25 13:38:40      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:port   bugs   目录结构   space   tar   log   failed   googl   ret   

任意类型

protobuf语言的任意字段类型相当于Boost库的boost::any类型数据,google.protobuf.Any是对protobuf语言的message进行封装,所以需要使用message来封装任意类型的数据,而不能像boost::any一样直接使用基础数据类型

工程目录结构

$ ls proto/
ErrorStatus.proto

proto文件

$ cat proto/ErrorStatus.proto
syntax = "proto3";

import "google/protobuf/any.proto";

message NetworkErrorDetails
{
    sint32 err_no = 1;
}

message IoErrorDetails
{
    sint32 err_no = 1;
}

message ErrorStatus {
  string message = 1;
  repeated google.protobuf.Any details = 2;
}

读写源文件

$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "ErrorStatus.pb.h"

using namespace std;

int main(int argc, char *argv[])
{
    fstream input("./log", ios::in | ios::binary);
    cout << "Deserialize start." << endl;

    ErrorStatus status;
    if (!status.ParseFromIstream(&input))
    {
        cout << "Deserialize failed." << endl;
        return -1;
    }
    cout << status.message() << endl;
    for (const google::protobuf::Any& detail : status.details())
    {
        // 检测Any类型具体对应的消息类型
        if (detail.Is<IoErrorDetails>())
        {
            cout << "IoErrorStatus:" << endl;
            IoErrorDetails io_error;
            if (detail.UnpackTo(&io_error))
                io_error.PrintDebugString();
            else
                cout << "Parse fails." << endl;
        }

        if (detail.Is<NetworkErrorDetails>())
        {
            cout << "NetworkErrorDetails:" << endl;
            NetworkErrorDetails network_error;
            if (detail.UnpackTo(&network_error))
                network_error.PrintDebugString();
            else
                cout << "Parse fails." << endl;
        }

    }

    cout << "Deserialize end." << endl;
    input.close();
    return 0;
}

$ cat writer.cpp
#include <fstream>
#include <iostream>
#include <string>
#include "ErrorStatus.pb.h"

using namespace std;

int main(int argc, char *argv[])
{
    IoErrorDetails details;
    details.set_err_no(1);

    ErrorStatus status;
    // 自动生成对象,并打包消息
    status.add_details()->PackFrom(details);
    status.set_message("File read operation");

    fstream output("./log", ios::out | ios::trunc | ios::binary);
    cout << "Serialize start." << endl;
    if (!status.SerializeToOstream(&output))
    {
        cout << "Serialize failed." << endl;
         return -1;
    }
    output.close();
    cout << "Serialize end." << endl;
    return 0;
}

ProtoBuf练习(三)

标签:port   bugs   目录结构   space   tar   log   failed   googl   ret   

原文地址:https://www.cnblogs.com/silvermagic/p/9087613.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!