任意类型

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练习(三)的更多相关文章

  1. Go - 如何编写 ProtoBuf 插件 (三) ?

    目录 前言 演示代码 小结 推荐阅读 前言 上篇文章<Go - 如何编写 ProtoBuf 插件 (二) >,分享了基于 自定义选项 定义了 interceptor 插件,然后在 hell ...

  2. Sword protobuf学习三

    #include <iostream> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> ...

  3. asp.net core 使用protobuf

    在一些性能要求很高的应用中,使用protocol buffer序列化,优于Json.而且protocol buffer向后兼容的能力比较好. 由于Asp.net core 采用了全新的MiddleWa ...

  4. Protobuf实现Android Socket通讯开发教程

    本节为您介绍Protobuf实现Android Socket通讯开发教程,因此,我们需要先了理一下protobuf 是什么? Protocol buffers是一种编码方法构造的一种有效而可扩展的格式 ...

  5. ProtocolBuffers (二) android与PC,C#与Java 利用protobuf 进行无障碍通讯【Socket】

    protobuf 是什么?   Protocol buffers是一种编码方法构造的一种有效而可扩展的格式的数据. 谷歌使用其内部几乎RPC协议和文件格式的所有协议缓冲区. 参考文档 http://c ...

  6. erlang抽象码与basho的protobuf

    erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...

  7. Corba、protocol buffer、SOA的区别 (转)

    From: http://www.zhihu.com/question/20279489 Google的protocol buffers?这个跟corba.soa没啥关系,不同层次的概念,没法比.pr ...

  8. 【阿里云产品公测】大数据下精确快速搜索OpenSearch

    [阿里云产品公测]大数据下精确快速搜索OpenSearch 作者:阿里云用户小柒2012 相信做过一两个项目的人都会遇到上级要求做一个类似百度或者谷歌的站内搜索功能.传统的sql查询只能使用like ...

  9. protocol buffer和当年corba ,和现在SOA有啥异同点

    CORBA是对象管理集团(OMG)的一个标准,使得不同语言编写的,运行在不同计算机上的能够协同工作.标准包括分布式计算的通讯协议(GIOP和IIOP),可映射到多种语言的接口描述语言(IDL),对象请 ...

  10. .NET 中的序列化 & 反序列化

    序列化:将对象的状态信息及类型信息,转换为一种易于传输或存储形式(流,即字节序列)的过程. 下图为序列化过程图示,图片来自微软官方文档: 反序列化:与序列化相反,将流转换为对象的过程. 常用的有二进制 ...

随机推荐

  1. Html 表单表格 form table

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 深入理解JVM - 早期(编译期)优化

    Java“编译期”是一段“不确定”的操作过程:可能是指一个前端编译器(编译器的前端)把*.java文件转变为*.class文件的过程:可能是指虚拟机的后端运行期编译器(JIT编译器,Just In T ...

  3. cpu架构

    转自 http://blog.csdn.net/wyzxg/article/details/5027738 CPU架构 Architecture ,结构.架构,这个词用于 CPU 的时候是指 CPU ...

  4. while( c= getchar(c) &&c!='\n')为什么错误

    运算顺序有关,详见 运算符优先级 代码1: #include<iostream> using namespace std; int main() { char c; int m=0; wh ...

  5. 普林斯顿算法(1.3)并查集(union-find算法)——本质就是一个数 下面的子树代表了连在一起的点

    转自:https://libhappy.com/2016/03/algs-1.3/ 假设在互联网中有两台计算机需要互相通信,那么该怎么确定它们之间是否已经连接起来还是需要架设新的线路连接这两台计算机. ...

  6. Python习题-统计日志中访问次数超过限制的IP

    #1.1分钟之内ip访问次数超过200次的,就给他的ip加入黑名单#需求分析: #1.读日志,1分钟读一次 #2.获取这1分钟之内所有访问的ip #3.判断ip出现的次数,如果出现200次,那么就加入 ...

  7. SQL Server 2008可以安装在win7 64位的系统上吗?

    可以安装的.SQL 支持32和64位.安装时它自动选择的.下载时注意是完整安装包. SQLFULL_CHS 2008.iso大小:3.28G 已经过百度安全检测,放心下载  

  8. 2017-2018-1 20179203 《Linux内核原理与分析》第三周作业

    攥写人:李鹏举 学号:20179203 ( 原创作品转载请注明出处) ( 学习课程:<Linux内核分析>MOOC课程http://mooc.study.163.com/course/US ...

  9. boost asio中io_service类的几种使用

    io_service类 你应该已经发现大部分使用Boost.Asio编写的代码都会使用几个io_service的实例.io_service是这个库里面最重要的类:它负责和操作系统打交道,等待所有异步操 ...

  10. 【转】 Pro Android学习笔记(五九):Preferences(3):EditText和Ringtone Preference

    目录(?)[-] EditText Preferences xml文件 设备的存贮文件 Ringtone Preferences EditText Preferences xml文件 在res/xml ...