1

I need to map my "native" C++ struct that has an array(std::array, but I presume for std::vector solution is same) to protobuf.

So let' say I have

struct MyStruct{
    // Color is some class I already know how to map to PB
    std::array<std::optional<Color>, 4> opt_colors;
}

I am not sure what is the best way.

My current best guess is:

message ColorPb{
// some fields here
}

message OptColorPb{
  int32 idx = 1; // idx in array
  ColorPb color = 2; // value
}
message MyStructPb{
   // in case of vector I would also have a size, but for array it is known
   repeated OptColorPb opt_colors = 1;
}
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
  • Why are you storing the index? – StoryTeller - Unslander Monica Jan 27 '21 at 17:07
  • @StoryTeller-UnslanderMonica so I know which ones are active, if indexes 0,3 are active I can not just have repeated with 2 entries since I need to know in which locations to restore them. – NoSenseEtAl Jan 27 '21 at 17:26
  • 1
    I would have thought you can just convert the entire array. And in entries without a `Color`, just don't set `color` (but still add the `OptColorPb` message wrapper). So yo'd always have four elements in the message, and the optional ones will simply not have `color`. Of course, the memory consumption would be affected (of interest for larger datasets). I never measured though... could be interesting to check. – StoryTeller - Unslander Monica Jan 27 '21 at 17:35
  • @StoryTeller-UnslanderMonica that also makes sense, although human readable string for large list of optionals that are not set is spammy... Still if you make it an answer I think it is a good option for people that have high occupancy ratio. – NoSenseEtAl Jan 28 '21 at 10:55

0 Answers0