Hi there,
I have a .fbs schema like below:
struct A {
a: float32;
}
struct B {
b: float32;
}
union ABUnionType {
A,
B
}
table MyConfig {
instance: ABUnionType;
}
root_type MyConfig;
--------------------------------
I'd like to use C++ full reflection to inspect / update one field (say the "a" in struct A, from a concrete flatbuffer with struct A stored in the instance). From the bfbs file, I did something as follows (trying to following the ReflectionTest pattern):
const reflection::Schema& schema = *reflection::GetSchema(bfbsfile.c_str());
const reflection::Object& root_table = *schema.root_table();
auto objects = schema.objects();
auto fields = root_table.fields();
const reflection::Field& instance_field_ptr = *fields->LookupByKey("instance");
assert(instance_field_ptr.type()->base_type() == reflection::Union);
// concrete instance from flatbuffer
const flatbuffers::Table& root = *flatbuffers::GetAnyRoot(my_flat_buffer);
const flatbuffers::Table* instanceTable = flatbuffers::GetFieldT(root, instance_field_ptr);
auto field_a = instance_struct_fields->LookupByKey("a");
const reflection::Object& unionType =
flatbuffers::GetUnionType(schema, root_table, instance_field_ptr, root);
auto field_a_ptr = unionType.fields()->LookupByKey("a");
assert(field_a_ptr);
--------------------------------
Now I'm stuck at this point, doing:
auto instance = flatbuffers::GetFieldF<float>(*instanceTable, *field_a_ptr);
will crash with EXC_BAD_ACCESS, as I guess instanceTable is describing the union, rather than the struct.
Any mechanism to get an "instanceStruct" of type "flatbuffers::Table*" pointing to the concrete struct A, so I can do something like:
flatbuffers::Table* instanceStruct = ???;
auto instance = flatbuffers::GetFieldF<float>(*instanceStruct, *field_a_ptr);
to its field "a" or even update its value?
Thanks.