其中一个类型

protobuf语言的oneof字段类型相当于C++语言的联合数据类型

工程目录结构

$ ls proto/
sample_oneof.proto

proto文件

$ cat proto/sample_oneof.proto
syntax = "proto3"; message sample_oneof
{
oneof oneof_name
{
int32 foo_int = 1;
string foo_string = 2;
}
}

读写源文件

$ cat reader.cpp
#include <fstream>
#include <iostream>
#include "sample_oneof.pb.h" using namespace std; int main(int argc, char *argv[])
{
fstream input("./log", ios::in | ios::binary);
cout << "Deserialize start." << endl; sample_oneof s;
if (!s.ParseFromIstream(&input))
{
cout << "Deserialize failed." << endl;
return -1;
} switch (s.oneof_name_case())
{
case sample_oneof::OneofNameCase::kFooInt:
cout << "Int:" << s.foo_int() << endl;
break;
case sample_oneof::OneofNameCase::kFooString:
cout << "String:" << s.foo_string() << endl;
break;
default:
cout << "ONEOF_NAME_NOT_SET" << endl;
break;
} cout << "Deserialize end." << endl;
input.close();
return 0;
} $ cat writer.cpp
#include <fstream>
#include <iostream>
#include "sample_oneof.pb.h" using namespace std; int main(int argc, char *argv[])
{
sample_oneof s;
s.set_foo_string("sample union"); fstream output("./log", ios::out | ios::trunc | ios::binary);
cout << "Serialize start." << endl;
if (!s.SerializeToOstream(&output))
{
cout << "Serialize failed." << endl;
return -1;
}
output.close();
cout << "Serialize end." << endl;
return 0;
}

ProtoBuf练习(四)的更多相关文章

  1. Sword protobuf学习四

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

  2. erlang抽象码与basho的protobuf

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

  3. Go 中的 gRPC 入门详解

    目录 Go GRPC 入门 1,安装包 2,gRPC 服务端 3,gRPC 客户端 4,编译运行 5,其它 GRPC Protobuf buffer 字段类型 字段规则 Protobuf gRPC 四 ...

  4. go微服务系列(四) - http api中引入protobuf

    1. protobuf相关依赖安装 2. 改造之前的client 2.1 新建proto文件 2.2 运行protoc命令生成go文件 2.3 然后把原来的map修改成具体的类型就可以了 3. 处理j ...

  5. unity探索者之socket传输protobuf字节流(四)

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/7027659.html 上篇已经把socket的传输说的差不多了,这篇主要是说说断线 ...

  6. Protobuf使用规范分享

    一.Protobuf 的优点 Protobuf 有如 XML,不过它更小.更快.也更简单.它以高效的二进制方式存储,比 XML 小 3 到 10 倍,快 20 到 100 倍.你可以定义自己的数据结构 ...

  7. protobuf中文教程(第一篇)

    声明:本文大部分内容翻译自官方英文文档,其中可能穿插着加入自己的语言用以辅助理解,本文禁止转载. 一.什么是protocol buffers Protocol buffers是一个灵活的.高效的.自动 ...

  8. Google的Protobuf协议分析

    protobuf和thrift类似,也是一个序列化的协议实现,简称PB(下文出现的PB代表protobuf). Github:https://github.com/google/protobuf 上图 ...

  9. Android高手速成--第四部分 开发工具及测试工具

    第四部分 开发工具及测试工具 主要介绍和Android开发工具和测试工具相关的开源项目. 一.开发效率工具 Json2Java根据JSon数据自动生成对应的Java实体类,还支持Parcel.Gson ...

随机推荐

  1. 修改myEclipse2014web项目名称

    重命名项目名称后 右键点击你的项目,然后选择属性---->然后点击myeclipse—>Project Facets—> web 选项,修改web context-root名称为你要 ...

  2. 一些有意思的面试题(持续更新) .C语言编程技巧札记

    一些有意思的面试题(持续更新) http://blog.csdn.net/wangyuling1234567890/article/details/38565239 C语言编程技巧札记 http:// ...

  3. adb命令(二)

    1.获取手机型号指令 adb shell cat /system/build.prop | findstr "ro.product.model" 2.获取手机处理器信息 adb s ...

  4. 【leetcode】Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. tbody scroll

    http://jsfiddle.net/hashem/CrSpu/555/

  6. 记一次编译tensorflow-gpu爬过的坑

    废话不多说,先说最终成功的版本:系统=>centos7 ,cuda=>10.0 ,cudnn=>7.5 ,nccl=>源码编译, tensorflow=>最新版本源码编译 ...

  7. 数据库JSON字段设计思路

    任务的阶段信息直接存储为JSON格式,这种格式避免了表关联,避免建表,应用层处理也简单的多了. 1. JSON内容为信息性质,而不具备非统计功能:简单讲就是展示,不能用于深度处理: 2. JSON内容 ...

  8. 洛谷【P1090】合并果子&&洛谷【P1334】瑞瑞的木板

    浅谈堆:https://www.cnblogs.com/AKMer/p/10284629.html 合并果子题目传送门:https://www.luogu.org/problemnew/show/P1 ...

  9. selenium webdriver frame操作,跳进跳出

    如果有两个平级的frame,跳进一个以后操作完成再操作第二个,这种情况要先跳出来,再跳进另外一个frame 跳出语句:browser.switch_to_default_content() #codi ...

  10. poj3414Pots(倒水BFS)

    Pots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13231   Accepted: 5553   Special J ...