How to define structures include C language bit-fields with FlatBuffers schema files? #334
-
|
Hello guys, does the FlatBuffer support C language bit-fields? Can the FlatBuffer tools generate it during compilation? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
|
The short answer is that a flatbuffer schema allows enums, and if you use the Bitfields in standard C are to my knowledge extremely rare (I don't even recall the syntax). Usefully enums are used, and preferably defined symbols because they work better as const expressions, e.g. in switch statements. Thus, FlatCC makes no attempt to directly support bitfields. FlatCC does support enums, and enums can be powers of two. These are not exactly like C enums. Each enum value is defined as a numeric constant (again, this works better with switch statements), and they are type safe, e.g. If you use the It is still possible to add flags together to set multiple flags at once in an enum value. At least in C. How portable that is across all languages, I am not sure. I'm curious about your use case for bit fields? Not flags / enums, but the C syntax for bit fields. |
Beta Was this translation helpful? Give feedback.
-
|
See also: https://flatbuffers.dev/schema/ |
Beta Was this translation helpful? Give feedback.
-
|
Hello @mikkelfj,
Recently we decided to use FlatBuffers in our project and we have many places with bit-field assignments, value checks (multi-bit as well single bit fields to be assigned, checked) for convenience across the project and I try to find easy way to keep all places unchanged (with C language bit-field usage) in whole my project. Thanks you for such detailed description, however I suspect my project's sources require some updates with getter/setter scenario instead of bit-fields assignments as well as value checks. |
Beta Was this translation helpful? Give feedback.
-
|
Thanks, one option of course, is to reconsider if bit fields actually add any value, secondary, what the performance constraints are, because translation will likely require a test per bit. Depending on how many different field types you have, you might get away with manually writing getters/setters as you suggest. Adding support to flatcc is not trivial because bit-fields are not portable and must be translated bit by bit, and because there is no inherent guarantee that only the listed values are stored in a buffer, but you might not care about that. There are generated functions to map enums, including flags, to json names using a syntax like "axe | hammer | bow". In principle this could be adopted to generate getters and setters for bit fields. But I am not keen on that approach. If it is important enough to you, you could consider a PR if a design can be made that works reasonably well. However, e.g. structs cannot both have an int type and a bit field type, and changing the struct type is likely complicated. So the best option would be generated a typedef for bit fields and getter setter inline functions. |
Beta Was this translation helpful? Give feedback.
The short answer is that a flatbuffer schema allows enums, and if you use the
bit_flagsattribute on enums, they behave like bitfields by assigning values1 << 0,1 << 1,1 << 2etc. This is documented in the official FlatBuffers guide on writing a schema.Bitfields in standard C are to my knowledge extremely rare (I don't even recall the syntax). Usefully enums are used, and preferably defined symbols because they work better as const expressions, e.g. in switch statements.
Thus, FlatCC makes no attempt to directly support bitfields.
FlatCC does support enums, and enums can be powers of two. These are not exactly like C enums.
Each enum value is defined as a numeric constant (again, this…