Consider a simple Protobuf code like this:
message MessageName
{
oneof some_options {
OptionA optionAName = 1;
OptionB optionBName = 2;
}
}
This will generate the following C++ code (or something like it, I've renamed some variables manually so it may not be 100% right):
typedef struct _namespace_name_MessageName {
pb_size_t which_some_options;
union {
namespace_name_OptionA optionAName;
namespace_name_OptionB optionBName;
} some_options;
} namespace_name_MessageName;
How do I read this message in C++? I would like to say "if it is OptionA, do this" and "if it is OptionB, do this". I haven't been able to find out how which_some_options works though, I can't find it in the documentation at all. I did read this but I can't find any of the methods mentioned there in the code that Protobuf generated. An example that shows how I should accomplish what I'm trying to accomplish would be greatly appreciated.
The original author discovered they were using NanoPB, and therefore the generated code did not match Google's C++ protobuf docs.
However, for those looking for a quick example on how to read oneof using C++ code, it should look something like this:
void ParseMessageName(MessageName msg)
{
switch (msg.some_options_case()) {
case MessageName::SomeOptionsCase::kOptionAName: {
// The msg contained "optionAName"
break;
}
case MessageName::SomeOptionsCase::kOptionBName: {
// The msg contained "optionBName"
break;
}
default: {
break;
}
}
}
The oneof section of the docs is here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With