ProtoBuf练习(三)
任意类型
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练习(三)的更多相关文章
- Go - 如何编写 ProtoBuf 插件 (三) ?
目录 前言 演示代码 小结 推荐阅读 前言 上篇文章<Go - 如何编写 ProtoBuf 插件 (二) >,分享了基于 自定义选项 定义了 interceptor 插件,然后在 hell ...
- Sword protobuf学习三
#include <iostream> #include <sys/types.h> /* See NOTES */ #include <sys/socket.h> ...
- asp.net core 使用protobuf
在一些性能要求很高的应用中,使用protocol buffer序列化,优于Json.而且protocol buffer向后兼容的能力比较好. 由于Asp.net core 采用了全新的MiddleWa ...
- Protobuf实现Android Socket通讯开发教程
本节为您介绍Protobuf实现Android Socket通讯开发教程,因此,我们需要先了理一下protobuf 是什么? Protocol buffers是一种编码方法构造的一种有效而可扩展的格式 ...
- ProtocolBuffers (二) android与PC,C#与Java 利用protobuf 进行无障碍通讯【Socket】
protobuf 是什么? Protocol buffers是一种编码方法构造的一种有效而可扩展的格式的数据. 谷歌使用其内部几乎RPC协议和文件格式的所有协议缓冲区. 参考文档 http://c ...
- erlang抽象码与basho的protobuf
erlang抽象码与basho的protobuf(一)使用 erlang抽象码与basho的protobuf(二)代码生成原理之词法与语法分析 erlang抽象码与basho的protobuf(三)代 ...
- Corba、protocol buffer、SOA的区别 (转)
From: http://www.zhihu.com/question/20279489 Google的protocol buffers?这个跟corba.soa没啥关系,不同层次的概念,没法比.pr ...
- 【阿里云产品公测】大数据下精确快速搜索OpenSearch
[阿里云产品公测]大数据下精确快速搜索OpenSearch 作者:阿里云用户小柒2012 相信做过一两个项目的人都会遇到上级要求做一个类似百度或者谷歌的站内搜索功能.传统的sql查询只能使用like ...
- protocol buffer和当年corba ,和现在SOA有啥异同点
CORBA是对象管理集团(OMG)的一个标准,使得不同语言编写的,运行在不同计算机上的能够协同工作.标准包括分布式计算的通讯协议(GIOP和IIOP),可映射到多种语言的接口描述语言(IDL),对象请 ...
- .NET 中的序列化 & 反序列化
序列化:将对象的状态信息及类型信息,转换为一种易于传输或存储形式(流,即字节序列)的过程. 下图为序列化过程图示,图片来自微软官方文档: 反序列化:与序列化相反,将流转换为对象的过程. 常用的有二进制 ...
随机推荐
- maven建ssh项目的pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- php 数据处理--合并,拆分,追加,去重
1. 合并数组 array_merge()函数将数组合并到一起,返回一个联合的数组.所得到的数组以第一个输入数组参数开始,按后面数组参数出现的顺序依次迫加. 示例代码: <?php $arr = ...
- PyCharm 常用快捷键和设置
pycharm常用快捷键 1.编辑(Editing) Ctrl + Space 基本的代码完成(类.方法.属性)Ctrl + Alt + Space 快速导入任意类Ctrl + Shift + ...
- Hibernate学习---第十三节:hibernate过滤器和拦截器的实现
一.hibernate 过滤器 1.在持久化映射文件中配置过滤器,代码如下: <?xml version="1.0"?> <!DOCTYPE hibernate- ...
- stl_vector.h
stl_vector.h // Filename: stl_vector.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com // Blog: http ...
- 【leetcode刷题笔记】Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- php断点续传
http://www.cnblogs.com/xproer/archive/2012/10/26/2741264.html
- 基于无锁的C#并发队列实现
最近开始学习无锁编程,和传统的基于Lock的算法相比,无锁编程具有其独特的优点,Angel Lucifer的关于无锁编程一文对此有详细的描述. 无锁编程的目标是在不使用Lock的前提下保证并发过程中共 ...
- Linux mount指令
-o,是指option,可以指定username,password:当时我们就碰到一个坎,如何来避免输入用户名密码,其实本质并不是避免输入用户名米吗,而是某种可知的方式来进行权限控制:解决的方式就是采 ...
- CF1060B:Maximum Sum of Digits
我对贪心的理解:https://www.cnblogs.com/AKMer/p/9776293.html 题目传送门:http://codeforces.com/problemset/problem/ ...