That's a very awesome trick, thanks! I should really pick up a book about #defines...
For those interested, heres my code now (structs have been typedef'ed, as well as some other minor tweaks):
Code:
#define STR(x) #x // insert x as a string
#define CAT(x,y) x ## y // concatenate x and y in-file as if typed
#define STRUCT_REMOVE(x) int CAT(remove,x) ( scene *s, x *object ) { \
/* remove a (x) from a scene */\
/* prevent scene from being modified by other threads */\
writeScene(s);\
\
/*find the index in the array of our pointer */\
for (unsigned int i = 0; i < s->CAT(x,Count); ++i)\
{\
if (s->CAT(x,s)[i] == object)\
{\
/* prevent fragmentation:
// move last item in the array to this now vacant spot
// incase object _is_ the last item, this is a waste of time but it
// doesn't mess anything up so I won't add a branch for dealing with it.*/\
s->CAT(x,s)[i] = s->CAT(x,s)[s->CAT(x,Count)-1];\
s->CAT(x,s)[s->CAT(x,Count)-1] = NULL;\
s->CAT(x,Count)--;\
\
/* other threads are free to go at it */\
doneWritingScene(s);\
\
/* only free surface material if it's only being used by (x) */\
if(object->mat->users==1){\
free(object->mat);\
}\
free(object);\
printf("Removed a "STR(x)"\n!");\
return 1;\
}\
}\
doneWritingScene(s);\
return -1;\
}