ecipientes genéricos seguros para o tipo com macr
Estou tentando criar uma lista vinculada genérica de tipo seguro em C usando macros. Ele deve funcionar de maneira semelhante à forma como os modelos funcionam em C ++. Por exemplo
LIST(int) *list = LIST_CREATE(int);
A minha primeira tentativa foi para#define LIST(TYPE)
(a macro que usei acima) para definir umstruct _List_##TYPE {...}
. Isso, no entanto, não funcionou porque a estrutura seria redefinida toda vez que declarasse uma nova lista. Corrigi o problema fazendo o seguinte:
/* 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
Mas outro problema é que, quando tenho vários arquivos que usamDEFINE_LIST(int)
, por exemplo, e alguns deles se incluem, ainda haverá várias definições da mesma estrutura. Existe alguma maneira de fazerDEFINE_LIST
verificar se a estrutura já foi definida?
/* one.h */
DEFINE_LIST(int);
/* two.h */
#include "one.h"
DEFINE_LIST(int); /* Error: already defined in one.h */