C++谷歌协议缓冲区:将二进制流分配给protobuf对象

C++ Google Protocol Buffers: assign binary stream to protobuf object

本文关键字:分配 protobuf 对象 二进制 谷歌 协议 缓冲区 C++      更新时间:2023-10-16

我有以下协议文件:

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);
}