C++谷歌协议缓冲区:将二进制流分配给protobuf对象
C++ Google Protocol Buffers: assign binary stream to protobuf object
我有以下协议文件:
message DataChunk{
required bool isHash=1;
required int64 hash=2;
required string data=3;
}
message responseBody{
repeated DataChunk dataChunk=1;
}
我有以下C++函数:
void eamorr(string data){ //data is a protocol buffer stream converted to a string
responseBody rb;
rb=some_function_of(data); //what to do here?
}
字符串"data"是使用创建的
...
std::ostringstream stream;
rb.SerializeToOstream(&stream);
string protobufStream = stream.str();
...
我的问题是:如何将字符串转换为protoc对象,以便访问成员元素?请记住,我对C++还很陌生。
您可以使用
rb.ParseFromString(data)
创建数据对象时,为什么不制作:
responseBody rb; //this is your proto object;
rb.SerializeToString(&data);
然后进行反序列化:
void eamorr(string data){
responseBody rb;
rb.ParseFromString(data);
}
相关文章:
- 将数组的地址分配给变量并删除
- vector.resize()中的分配错误
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- Win32编译器选项和内存分配
- 函数中堆分配的效果与缺少堆分配的情况
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 获取字符串的长度并将其分配给数组
- 将地址分配给本地指针后,公共对象的变量将消失
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 有没有一种方法可以使用placement new将堆叠对象分配给分配的内存
- 我在二维向量中是否正确分配了内存
- 正在尝试重载二进制搜索树分配运算符
- GlobalAlloc而不是其他分配方法
- 自定义先决条件对移动分配运算符有效吗
- 我可以重新分配/覆盖std::字符串吗
- 在c++中使用动态分配的问题
- 如何在C++中分配嵌套的protobuf而不进行复制
- 如何在 protobuf 中使用反射将预分配的消息设置为字段C++
- 分配消息崩溃在google protobuf