知道 void * 分配类型的宏
Macro that knows void * allocation type
以这段代码为例:
typedef struct {
int value;
char another;
} foo;
typedef struct {
float yet_another;
long really_another;
} bar;
typedef struct {
char* name;
void* data;
} gen;
int main(int argc, char** argv) {
gen foo_gen, bar_gen;
foo_gen.name = "Foo";
foo_gen.data = (foo*) malloc(sizeof(foo));
bar_gen.name = "Bar";
bar_gen.data = (bar*) malloc(sizeof(bar));
((foo*) foo_gen->data)->value = 0;
((bar*) bar_gen->data)->yet_another = 1.0f;
return 0;
}
这段代码可以正常工作,所以我定义了 2 个宏来方便我的工作:
#define FOO_DATA(N, D) ((foo*) N->data)->D
#define BAR_DATA(N, D) ((bar*) N->data)->D
不过好像太重复了。我想让它变得更通用,让宏知道它应该转换哪种类型。我尝试使用 __auto_type
:
#define DATA(N, D) (__auto_type N->data)->D
但是没有用。 typeof
似乎也不适用于宏。我应该怎么做?
如果我和其他评论者理解正确,你希望能够做这样的事情:
struct a {
int field1;
} a;
struct b {
int field2;
} b;
void *self1 = &a;
void *self2 = &b;
int f1 = MAGIC(self1)->field1;
int f2 = MAGIC(self2)->field2;
对于 MAGIC
.
的某些定义
这在 C 中是不可能的。
扩展我的评论:您必须将您的公共字段分组到另一个结构中,并将其作为 self->data
中可能结构的第一个字段:
struct common {
int foo;
};
struct a {
struct common common;
int bar;
};
struct b {
struct common common;
int baz;
};
#define $(D) (((struct common*) self->data)->D)
以这段代码为例:
typedef struct {
int value;
char another;
} foo;
typedef struct {
float yet_another;
long really_another;
} bar;
typedef struct {
char* name;
void* data;
} gen;
int main(int argc, char** argv) {
gen foo_gen, bar_gen;
foo_gen.name = "Foo";
foo_gen.data = (foo*) malloc(sizeof(foo));
bar_gen.name = "Bar";
bar_gen.data = (bar*) malloc(sizeof(bar));
((foo*) foo_gen->data)->value = 0;
((bar*) bar_gen->data)->yet_another = 1.0f;
return 0;
}
这段代码可以正常工作,所以我定义了 2 个宏来方便我的工作:
#define FOO_DATA(N, D) ((foo*) N->data)->D
#define BAR_DATA(N, D) ((bar*) N->data)->D
不过好像太重复了。我想让它变得更通用,让宏知道它应该转换哪种类型。我尝试使用 __auto_type
:
#define DATA(N, D) (__auto_type N->data)->D
但是没有用。 typeof
似乎也不适用于宏。我应该怎么做?
如果我和其他评论者理解正确,你希望能够做这样的事情:
struct a {
int field1;
} a;
struct b {
int field2;
} b;
void *self1 = &a;
void *self2 = &b;
int f1 = MAGIC(self1)->field1;
int f2 = MAGIC(self2)->field2;
对于 MAGIC
.
这在 C 中是不可能的。
扩展我的评论:您必须将您的公共字段分组到另一个结构中,并将其作为 self->data
中可能结构的第一个字段:
struct common {
int foo;
};
struct a {
struct common common;
int bar;
};
struct b {
struct common common;
int baz;
};
#define $(D) (((struct common*) self->data)->D)