是否可以(递归地)使用 x-macros "introspect" 嵌套 C 结构?

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

我正在阅读这篇文章 (Struct iteration through (ab)use of the preprocessor),其中作者使用 x-macros 和 offsetof 将元数据添加到结构中,这将允许其成员轻松序列化,按名称访问,等等,但它仅针对原始结构元素实现。

是否也可以将其扩展为包含嵌套结构的结构? IE。允许简单 de/serialialization 之类的东西:

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

struct data {
   int number;
   struct some_struct something;
}; 

我注意到作者在开头是这样说的:

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).

是否可以使用 C 预处理器实现类似的功能?

(澄清)

为了更清楚,我想看看是否有一种方法可以让我:

a) 定义struct,和
b) 为文本 serialization/deserialization

创建元数据

如果可能一步到位。

你要的都在这里实现https://github.com/alexanderchuranov/Metaresc

这是一个示例应用程序:

#include <metaresc.h>

TYPEDEF_STRUCT (some_struct_t,
                int x,
                int y,
                int z,
                );

TYPEDEF_STRUCT (data_t,
                int number,
                (struct some_struct_t, something),
                );  

int main (int argc, char * argv[])
{
  data_t data = { 1, { 2, 3, 4 } };
  MR_PRINT ("data = ", (data_t, &data));
  return (EXIT_SUCCESS);
}

预期输出:

data = {
  .number = 1,
  .something = {
    .x = 2,
    .y = 3,
    .z = 4
  }
}