ee2 ee2 - 1 month ago 6
C++ Question

Set Enum values using reflection in Protocol buffers

I have an Enum

Update update
in a .proto file . I want to store it using reflections in a message.

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


???? - should be EnumValueDescriptor * (I do not know how to get it)

How can I do this?

I have tried
message_desc->FindEnumTypeByName(s)->FindValueByNumber(update)
for many different possibilities of s like:

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

Answer

All you need in the third argument is EnumValueDescriptor*.

Also as you can see you have passed FieldDescriptor in the second argument. So just use FieldDesciptor::enum_type() to get EnumDescriptor* and then use EnumDescriptor::Value() to get EnumValueDescriptor.

Comments