gwenhywfar  5.11.1beta
Macros For Typesafe Tree Handling

The macros of this group facilitates typesafe use of trees.

Let's assume you have a structure type called MYSTRUCT and you want to manage lists of them. Let's further assume that you want the functions dealing with that struct have prefixes like MyStruct (as in MyStruct_new)

The header file would look like this:

/ * mystruct.h * /
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
typedef struct MYSTRUCT MYSTRUCT;
GWEN_TREE2_FUNCTION_DEFS(MYSTRUCT, MyStruct);
struct MYSTRUCT {
GWEN_TREE2_ELEMENT(MYSTRUCT);
int myData;
}
MYSTRUCT *MyStruct_new(int myData);
void MyStruct_free(MYSTRUCT *myStruct);
#endif
struct GWEN_TREE2_ELEMENT GWEN_TREE2_ELEMENT
Definition: tree2.h:155
#define GWEN_TREE2_FUNCTION_DEFS(t, pr)
Definition: tree2.h:345

This defines all necessary data and function prototypes needed for list management.

The code file would look quite similar to the following:

/ * mystruct.c * /
GWEN_TREE2_FUNCTIONS(MYSTRUCT, MyStruct)
MYSTRUCT *MyStruct_new(int myData) {
MYSTRUCT *pMyStruct;
pMyStruct=(MYSTRUCT*)malloc(sizeof(MYSTRUCT));
memset(pMyStruct, 0, sizeof(MYSTRUCT));
GWEN_TREE2_INIT(MYSTRUCT, pMyStruct)
pMyStruct->myData=myData;
return pMyStruct;
}
void MyStruct_free(MYSTRUCT *pMyStruct) {
if (pMyStruct) {
pMyStruct->myData=0;
GWEN_TREE2_FINI(MYSTRUCT, pMyStruct)
free(pMyStruct);
}
}
#define GWEN_TREE2_FUNCTIONS(t, pr)
Definition: tree2.h:354
#define GWEN_TREE2_FINI(t, element, pr)
Definition: tree2.h:457
#define GWEN_TREE2_INIT(t, element, pr)
Definition: tree2.h:445

Please note the three macros used in the code file:

Note: When writing these macro code lines, the original ISO C89 standard for the C language does not allow terminating the macro statement with a semicolon ';'. Any recent compiler will probably silently ignore such an extra ';', but you should be aware that this can cause problems once one of your users tries to compile this with a different compiler. Therefore these code lines should end directly with the closing parentheses.

The tree management code assumes that there is a function called (in this example) MyStruct_free() (or generally: TYPEPREFIX_free). This is used when destroying a list of MYSTRUCT elements. In this case all elements still enlisted are destroyed upon destruction of the list.