Contenedores genéricos de tipo seguro con macros
Estoy tratando de hacer una lista vinculada genérica de tipo seguro en C usando macros. Debería funcionar de manera similar a cómo funcionan las plantillas en C ++. Por ejemplo
LIST(int) *list = LIST_CREATE(int);
Mi primer intento fue para#define LIST(TYPE)
(la macro que utilicé anteriormente) para definir unastruct _List_##TYPE {...}
. Sin embargo, eso no funcionó porque la estructura se redefiniría cada vez que declarara una nueva lista. Solucioné el problema haciendo esto:
/* You would first have to use this macro, which will define
the `struct _List_##TYPE`... */
DEFINE_LIST(int);
int main(void)
{
/* ... And this macro would just be an alias for the struct, it
wouldn't actually define it. */
LIST(int) *list = LIST_CREATE(int);
return 0;
}
/* This is how the macros look like */
#define DEFINE_LIST(TYPE) \
struct _List_##TYPE \
{ \
... \
}
#define LIST(TYPE) \
struct _List_##TYPE
Pero otro problema es que cuando tengo varios archivos que usanDEFINE_LIST(int)
, por ejemplo, y algunos de ellos se incluyen entre sí, entonces aún habrá múltiples definiciones de la misma estructura. ¿Hay alguna manera de hacerDEFINE_LIST
comprobar si la estructura ya se ha definido?
/* one.h */
DEFINE_LIST(int);
/* two.h */
#include "one.h"
DEFINE_LIST(int); /* Error: already defined in one.h */