Reputation: 8754
I'm checking out Flatbuffers for implementing a communication protocol. When a message is received, it may in my case contain a number of different tables. If I understand correctly, the way to achieve this in Flatbuffers is to use a "root" table that has each possible different table in a union.
In my case, I will already know the incoming type (type is part of the header) => I do not necessarily need to be able to place each type inside a single table. However, it does not seem to be possible to mark multiple tables as "root" types. This means that if I have defined the tables Foo
and Bar
that I can only get either a GetFoo()
or a GetBar()
method for deserialization, but not both.
I am assuming that it would also be possible to split the definitions across different schema files, but since they would share some subclasses I would also need a shared schema file for the common definitions. This seems to be a bit more complicated than necessary for simple cases.
Is there another way of being able to deserialize multiple different types with Flatbuffers?
Upvotes: 5
Views: 4860
Reputation: 6074
Yes, you can do this. Note that the generated GetMyType()
is just short for the templated GetRootAs<MyType>
which you can use with any type.
Upvotes: 7