Protobuf: how should nested (custom) optional fields be handled in C ++?

Reading in the Basics of the protocol buffer: C ++ , nothing was found, which corresponds to the situation :; followed by .proto processing with --cpp_out ,

 message A { required int32 foo = 1; } message B { optional A data = 1; } 

no explicit looking accessory / setter is created to set a custom optional field (including the "nested types" section, which I'm too lazy to put here):

  // accessors ------------------------------------------------------- // optional .A = 1; inline bool has_a() const; inline void clear_a(); static const int kAFieldNumber = 1; inline const ::A& a() const; inline ::A* mutable_a(); inline ::A* release_a(); 

So how to set B :: A to some instance of A in C ++?


TEST FILES :. proto generated results:. h , .cc and some . java


Update: in Java, nested fields are set via Builder: see the link above, for example (look for setData).

+6
source share
1 answer

Solution: use mutable to modify some returned doodad.

 A a; A.set_foo(1); B b; B.mutable_A()->CopyFrom(a); 
+10
source

Source: https://habr.com/ru/post/917339/


All Articles