在协议缓冲区中使用反射设置枚举值

Set Enum values using reflection in Protocol buffers

我在 .proto 文件中有一个枚举 Update update。我想在消息中使用 reflections 存储它。

refl->SetEnum (msg, fd , ???   );

???? - 应该是 EnumValueDescriptor *(我不知道如何得到它)

我该怎么做?

我已经 message_desc->FindEnumTypeByName(s)->FindValueByNumber(update) 尝试了很多不同的 s 可能性,例如:

UpdateType
UpdateType::CONFIRM
CONFIRM
....
.... 

第三个参数只需要 EnumValueDescriptor*。

您还可以看到,您已经在第二个参数中传递了 FieldDescriptor。所以只需使用 FieldDesciptor::enum_type() 获取 EnumDescriptor* 然后使用 EnumDescriptor::Value() 获取 EnumValueDescriptor.