I have a macro that works as expected, but I want to make some changes to make it cleaner to use.
#define FuncCreate(func_name, func, ...) \
int func_name(lua_State *ms) { \
func(__VA_ARGS__); \
return 0; \
}
FuncCreate(appEvent, app_event, lua_touserdata(ms, 1), lua_tointeger(ms, 2), lua_tostring(ms, 3));
FuncCreate(GdEvent, gd_event, lua_touserdata(ms, 1), lua_touserdata(ms, 2), lua_tostring(ms, 3), lua_tointeger(ms, 4), lua_tostring(ms, 5));
I want to change the way I use arguments in the macro
I want to make use of key from data types e.g.
FuncCreate(appEvent, app_event, struct, int, const char*);
FuncCreate(GdEvent, gd_event, struct, int, const char*, int, const char*);
How can the macro take for example get the names
struct pass as argument to func the lua_touserdata
pass int as an argument to func or lua_tointeger
Would it be better to create a variadic function? How can I do this ? Remembering that I want to use the key name of the data types
You can use a _Generic to overload over type. Then you can overload a macro over variable number of arguments to pass each function to proper callback.
C programming is statically typed and does not have reflection capability. Using a variadic function would lose the information about types, which seems to be of interest here.
Overall, it would be better not to write such code. In C, you would type everything out, line by line. Writing such macro expansions to generate functions results in really hard to maintain and unreadable code that no one is ever able to fix or change later.