基础数据类型

protobuf语言的基础字段类型相当于C++语言的基础类型

工程目录结构

$ ls proto/
TFixed.proto TInt.proto TScalar.proto TStr.proto

proto文件

$ cat TScalar.proto
syntax = "proto3"; //导入其他message
import "TInt.proto";
import "TFixed.proto";
import "TStr.proto"; //使用其他message作为字段类型,如果message使用了package则需要使用package.的方式引用
message TScalar {
scalar.TInt int_val = 1;
TFixed fixed_val = 2;
TStr str_val = 3;
}; $ cat TInt.proto
syntax = "proto3"; // 设置包名,在生成的C++源码中将变成namespace
package scalar; message TInt {
double ddouble_val = 1;
float float_val = 2;
int32 int32_val = 3;
uint32 uint32_val = 4;
sint32 sint32_val = 5;
bool bool_val = 6;
}; $ cat TFixed.proto
syntax = "proto3"; message TFixed {
fixed32 fixed32_val = 1;
fixed64 fixed64_val = 2;
sfixed32 sfixed32_val = 3;
sfixed64 sfixed64_val = 4;
}; $ cat TStr.proto
syntax = "proto3"; message TStr {
string string_val = 1;
bytes bytes_val = 2;
};

读写源文件

$ cat writer.cpp
#include <fstream>
#include <iostream>
#include "TScalar.pb.h" using namespace std; int main(int argc, char *argv[])
{
TScalar msg;
scalar::TInt* iMsg = new scalar::TInt();
TFixed* fMsg = new TFixed();
TStr* sMsg = new TStr();
// 使用protobuf自己的内存管理
msg.set_allocated_int_val(iMsg);
msg.set_allocated_fixed_val(fMsg);
msg.set_allocated_str_val(sMsg); iMsg->set_ddouble_val(11.01);
iMsg->set_float_val(11.01);
iMsg->set_int32_val(-1);
iMsg->set_uint32_val(1);
iMsg->set_sint32_val(-1);
iMsg->set_bool_val(true); fMsg->set_fixed32_val(1);
fMsg->set_fixed64_val(1);
fMsg->set_sfixed32_val(-1);
fMsg->set_sfixed64_val(-1); sMsg->set_string_val("中");
sMsg->set_bytes_val("文"); fstream output("./log", ios::out | ios::trunc | ios::binary);
cout << "Serialize start." << endl;
if (!msg.SerializeToOstream(&output))
{
cout << "Serialize failed." << endl;
return -1;
}
output.close();
cout << "Serialize end." << endl;
return 0;
} $ cat reader.cpp
#include <fstream>
#include <iostream>
#include "TScalar.pb.h" using namespace std; int main(int argc, char *argv[])
{
fstream input("./log", ios::in | ios::binary);
cout << "Deserialize start." << endl; TScalar msg;
//如果writer依次写入TInt、TFixed、TStr,然后在reader依次读取TInt、TFixed、TStr,这时只有第一个message能正常解析,后两条message解析失败,因为ParseFromIstream会一次性把input流内容读完,导致后面的message解析时读取的输入流已经为空,所以表面上看起来就好像protocol buffer只能序列化和反序列化一条message
if (!msg.ParseFromIstream(&input))
{
cout << "Deserialize failed." << endl;
return -1;
}
msg.int_val().PrintDebugString();
msg.fixed_val().PrintDebugString();
msg.str_val().PrintDebugString(); cout << "Deserialize end." << endl;
input.close();
return 0;
}

最新文章

  1. 传智播客--数据绑定--INotifyPropertyChanged(小白内容)
  2. MyBatis学习总结(六)——调用存储过程
  3. Easyui主要组件用法
  4. Android AdapterView.OnItemSelectedListener
  5. YII 常用路径总结
  6. [数据清洗]-使用 Pandas 清洗“脏”数据
  7. 今年暑假不AC【贪心】
  8. PS图层混合算法之六(差值,溶解, 排除)
  9. JVM上的响应式流 — Reactor简介
  10. mysql 5.7 配置文件说明
  11. SpringBoot实现跨域
  12. 在Eclipse中导入web项目时的问题总结
  13. PHP中对象的本质
  14. rocket mq知识点
  15. IntelliJ Idea Java 使用
  16. jQuery常用的取值或赋值的方法
  17. 【java编程】Java魔法类:Unsafe应用解析
  18. lunix salt 用法
  19. 罗马数字转整数 &#183; Roman to Integer
  20. How to reset XiaoMi bluetooth headphone Youth edition.

热门文章

  1. BZOJ 3357 [Usaco2004]等差数列:map优化dp
  2. jq js 的date()使用
  3. cout是右结合的
  4. hbase_学习_01_HBase环境搭建(单机)
  5. js的trim方法(转)
  6. codeforces 633A A. Ebony and Ivory(暴力)
  7. java静态方法(变量)、非静态方法(变量)区别
  8. 查看linux上所有用户
  9. Poj_1045
  10. 微服务学习一 微服务session 管理