I have a flatbuffer defined as following:
struct Config {
...
}
table Commands {
...
}
table Request {
config : Config
command : [Commands]
data : [ubyte]
}
After I receive Request and process it, I want to send to other device without the data
field, i.e. only config and command.
With knowledge of C struct, I can send the first bytes of Request that size is sizeof(Request) - sizeof(data).
Is it applicable for flatbuffer?
If the answer is no, what if the data
field is always the last one to be add in Request builder?
Will data
field to be put at the end of Request so I can skip it?
What you're asking for specifically is not supported, FlatBuffers are a bit more complicated than structs, see https://flatbuffers.dev/flatbuffers_internals.html
The best supported way to accomplish this is to stick config
and command
in their own table, and then to refer to that table in the root as nested_flatbuffer
(see docs), giving you an independent buffer you can send onwards.
You can also hack it, as data
, if you serialize it first, does indeed end up at the end of the buffer, and thus truncating the buffer at the first byte of data
would give you a mostly functional FlatBuffer. It is however not a correct FlatBuffer so the verifier would fail on it, an access of data
could crash with an out of bound access.