Reputation: 1088
i referred following link to understand how Union is encoded/ decoded in flatbuffers.
https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html https://github.com/dvidelabs/flatcc#unions
i have defined my IDL as follows
table EthEndpoint
{
// ethernet fields
}
table WifiEndpoint
{
// wifi fields
}
union EndpointData
{
EthEndpoint,
WifiEndpoint
}
flatcc_builder_t builder;
flatcc_builder_init(&builder);
EthEndpoint_start(&builder)
// populate using EthEndpoint_*_add(&builder, ...)
EthEndpoint_ref_t eth_ep = EthEndpoint_end(&builder);
EndpointData_ref_t ep = EndpointData_as_EthEndpoint(eth_ep);
how to add ep to builder? There are no generated methods EndpointData_start/ EndpointData_end/ EndpointData_create.
size_t size;
void *buf = flatcc_builder_get_direct_buffer(&builder, &size);
// store the buffer to disk
flatcc_builder_clear(&builder);
how to add ep to builder? there is no EndpointData_start/ EndpointData_end/ EndpointData_create.
Upvotes: 0
Views: 232
Reputation: 6074
You can't have a union as root of a buffer. Add a table that wraps it, then use the generated code to set both type and value.
Upvotes: 1