4 ms·
Wow, I just finished doing something similar. The use of x-macros is definitely a little cleaner / canonical than my approach, which instead "recurses" through
by asteadman 11y ago
Wow, I just finished doing something similar. The use of x-macros is definitely a little cleaner / canonical than my approach, which instead "recurses" through a VA_MACRO. For an excellent guide on recursion in macros, see this article on how to abuse the C pre-processor: https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,....
In the end I end up defining structs like this:
SERIALIZER_STRUCT( structname, int8_t, field1, int16_t, field2, ... );
Initializing them like this:
structname instance = {.field1 = 1, .field2=2};
and serializing them like this:
SERIALIZER_SERIALIZE( structname, &instance, bigbuff_ptr );
As for just using a packed structure, there are definitely caveats with some compilers (ie: compilers for embedded systems may not handle various edge-cases for misaligned access, particularly when dereferencing pointers to structs.)