The way you think about this problem is the opposite of the way you need to think if you are considering protobuffs. Protobufs do not use the existing data structure and do not serialize them. They take a serialization protocol and create data structures for you that you populate.
With this in mind, nested serialization is pretty simple:
// nested.proto
message Inner {
required string value = 1;
}
message Outer {
required Inner inner = 1;
}
message Pointers {
repeated Outer outer = 1;
}
, , , . , Pointers Inner:
Pointers pointers;
for (int i = 0; i < 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);
for (int i = 0; i < parsed_pointers.outer_size(); ++i) {
std::cout << parsed_pointers.outer(i).inner().value() << std::endl;
}
Inner, Outer :
Inner* inner = new Inner();
inner->set_value("Hello world");
Outer outer;
outer.set_allocated_inner(inner);
Inner, delete .