Edit: Since the replies seem to be focusing on the void* to typed* conversion, I've changed the example to something a bit more concrete.
I have a bunch of functions that all follow the same pattern:
int prefix_name(void *p) {
my_type *t = (my_type *)p;
<possibly other common lines>
<function-specific code>
}
static int mod_name(lua_State *L) {
lua_pushlightuserdata(L, (void *)&KEY);
lua_gettable(L, LUA_REGISTRYINDEX);
typed *t = lua_touserdata(L, -1);
<function-specific code>
}
To reduce typing and typos, I created a simple macro:
#define API_FUNCTION(name) static int mod_ ## name(lua_State *L) {\
lua_pushlightuserdata(L, (void *)&KEY);\
lua_gettable(L, LUA_REGISTRYINDEX);\
typed *t = lua_touserdata(L, -1);
so I can then just write each function as
API_FUNCTION(name)
<function-specific code>
}
This works well, but because the opening brace is in the macro, this breaks the brace-matching function in my editor. Is there something I could change in the macro so I could write
API_FUNCTION(name) {
<function-specific code>
}
and have things work as intended?
One possible alternative might be to define the macro as
API_FUNCTION(name, ...) int prefix_ ## name(void *p) {\
my_type *t = (my_type*)p;\
<possibly other common lines>
__VA_ARGS__
}
and then call it as
API_FUNCTION(name,
<function-specific code>
)
which at first glance does appear to work, but I don't like the way it looks and it also makes the preprocessor output much larger.
Edit 2: based on Orel's code, I suppose something like this would work, though it's rather ugly and means an extra function call:
#define API_FUNCTION(name) static int mod_ ## name ## _internal(lua_State *L, typed *t);\
static int mod_ ## name(lua_State *L) {\
lua_pushlightuserdata(L, (void *)&KEY);\
lua_gettable(L, LUA_REGISTRYINDEX);\
typed *t = lua_touserdata(L, -1);\
return mod_ ## name ## _internal(L,t); \
} \
static int mod_ ## name ## _internal(lua_State *L, typed *t)