[英]Changing an element of an existing protobuf message in C++
I was just wondering, why anybody hasn't gone through the problem I recently have in connection with google protobufs, but after extensive googling, reading the documentation of google's manual page and searching in Stackoverflow-DB, I did not found a solution. 我只是想知道,为什么没有人解决我最近遇到的与Google protobufs有关的问题,但是经过广泛的搜索,阅读了Google手册页的文档并在Stackoverflow-DB中进行搜索之后,我没有找到解决方案。
I am using proto2-c++-API on an Ubuntu 14.04.3 LTS, compiling with gcc/g++ over cmake-files. 我在Ubuntu 14.04.3 LTS上使用proto2-c ++-API,并通过gmake / g ++在cmake文件上进行了编译。
I have an application that reads binary (serialized) google protocol buffer messages from a file. 我有一个从文件读取二进制(序列化)谷歌协议缓冲区消息的应用程序。 The programm's purpose then is to send the messages (without deserialization) to another application, which proceeds with processing the actual data.
然后,程序的目的是将消息(不进行反序列化)发送到另一个应用程序,该应用程序继续处理实际数据。
I now would like to modify some of the messages, read from the file, so I can test the functionality of the second application. 现在,我想修改一些消息,从文件中读取消息,以便测试第二个应用程序的功能。 Unfortunately my message includes a lot of nested messages, so after deserializing I have to call something like
不幸的是,我的消息中包含很多嵌套消息,因此在反序列化之后,我必须调用类似
message().a().b().c()....x().value();
to be able to work with the actual data. 以便能够处理实际数据。
My question now is, how I can change the value of x
without creating another message of type message
where I also have to create all sub-messages ( a,b,c...
) and allocate these with the respective predecessor like in the following pseudo-code?! 我现在的问题是,如何在不创建另一个message类型的
message
情况下更改x
的值,其中我还必须创建所有子消息( a,b,c...
)并将它们分配给各自的前任对象,例如以下伪代码?
a = new a();
b = new b();
c = new c();
...
v = new v();
w = new w();
x = new x();
x.set_value();
w.set_allocated_x_value(x);
v.set_allocated_w_value(w);
...
a.set_allocated_b_value(b);
message.set_allocated_a_value(a);
...
/* forward message to second application */
...
delete x;
delete w;
...
delete a;
Obviously it is not possible to call set_value
directly on the message
-objects, respectively its sub-objects like message().a().b().c()....x().set_value();
显然,不可能直接在
message
对象或其子对象(例如message().a().b().c()....x().set_value();
上调用set_value
message().a().b().c()....x().set_value();
, as one would violate the const requirements of the auto-generated protobuf-messages, where it is not allowed to call a setter-method on a const object: error: passing xxx as 'this' argument of xxx discards qualifiers ,因为它会违反自动生成的protobuf消息的const要求,在该情况下,不允许在const对象上调用setter方法: 错误:将xxx作为xxx的“ this”参数传递会舍弃限定符
I would appreciate any creative solution to avoid implementing the recursive new-set_allocated-delete
code, posted above. 我希望能有任何创造性的解决方案来避免实现上面发布的递归
new-set_allocated-delete
代码。
Thanks in advance 提前致谢
The key to this is to use the mutable_x()
accessors, so in your example you would do something like this: 这样做的关键是使用
mutable_x()
访问器,因此在您的示例中,您将执行以下操作:
message.mutable_a()->mutable_b()->mutable_c()->set_value(42);
The set_allocated_*
methods are actually not really recommended unless you really know what you're doing, because they give you special control over memory management that you ordinarily should not need unless you're specifically trying to optimize a particular piece of code. 实际上,除非您真的知道自己在做什么,否则实际上不建议使用
set_allocated_*
方法,因为它们可以使您对通常不需要的内存管理有特殊的控制,除非您专门尝试优化特定的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.