Deep-copying with ProtoBuf in C/C++ -


say had array of pointers, each of points structs may once again have pointers other structs in them; possible handle serializing using protobuf? if so, how?

any appreciated.

the way you're thinking problem reverse of how need think if you're considering protobufs. protobufs don't take existing data structure , serialize them. take serialization protocol , create data structures you populate.

with said, nested serialization pretty simple:

// nested.proto message inner {     required string value = 1; }  message outer {     required inner inner = 1; }  message pointers {     repeated outer outer = 1; } 

assuming you've correctly compiled this, can use protocol working outside in. is, start highest structure, pointers, , work way inner object:

pointers pointers; (int = 0; < 10; ++i) {     auto outer = pointers.add_outer();     auto inner = outer->mutable_inner();     inner->set_value(std::to_string(i)); }  std::stringstream stream; pointers.serializetoostream(&stream);  ...  pointers parsed_pointers; parsed_pointers.parsefromistream(&stream); (int = 0; < parsed_pointers.outer_size(); ++i) {     std::cout << parsed_pointers.outer(i).inner().value() << std::endl; }  // prints 0, 1, 2, ..., 9 on own lines 

it's possible start inner message, way pass ownership outer message not simple or obvious:

inner* inner = new inner(); inner->set_value("hello world"); outer outer; outer.set_allocated_inner(inner); 

calling method capture ownership of inner pointer shouldn't manually delete yourself.