mercredi 12 avril 2017

Is it possible to (recursively) "introspect" nested C structs using x-macros?

I was reading this article (Struct iteration through (ab)use of the preprocessor), where the author uses x-macros and offsetof to add metadata to structs which would allow their members to be easily serialized, accessed by name, etc. But it's only implemented for primitive struct elements.

Is it possible to expand this to structs containing nested struct, also? I.e. something which would allow simple de/serialialization of something like:

struct a {
   int x, y, z;
}; 

struct b {
   struct a ax;
   struct a ay;
   struct a az;
}; 

I've noticed the author states this at the beginning:

At this stage, the structs only consist of primitive elements (int, float, char, etc). Handling nested structs, unions, bitfields and pointers would require additional work (that may be the subject of a future post).

Is something like this possible using the C preprocessor?





Aucun commentaire:

Enregistrer un commentaire