简体   繁体   中英

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.

I am using proto2-c++-API on an Ubuntu 14.04.3 LTS, compiling with gcc/g++ over cmake-files.

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?!

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(); , 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

I would appreciate any creative solution to avoid implementing the recursive new-set_allocated-delete code, posted above.

Thanks in advance

The key to this is to use the mutable_x() accessors, so in your example you would do something like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM