1

background

I'm trying to make automatic generator of Lua-C interface using C macros. The biggest problem was to make it general for varying number of arguments, which I resolved by using __VA_ARGS__ with help of this answer: Is it possible to iterate over arguments in variadic macros?

Simpler almost working solution

this solution almost works, but it produce some redudant commas (nottice ,,,, in output)

// helper macros for iteration over __VA_ARGS__
#define ARG1(WHAT,X,...) WHAT(X)ARG2(WHAT,__VA_ARGS__)
#define ARG2(WHAT,X,...) WHAT(X)ARG3(WHAT,__VA_ARGS__)
#define ARG3(WHAT,X,...) WHAT(X)ARG4(WHAT,__VA_ARGS__)
#define ARG4(WHAT,X,...) WHAT(X)ARG5(WHAT,__VA_ARGS__)
#define ARG5(WHAT,X,...) WHAT(X)ARG6(WHAT,__VA_ARGS__)
#define ARG6(WHAT,X,...) WHAT(X)//ARG2(__VA_ARGS__)

// macros dispatch propper type of Lua::get
#define LUA_GET_int(i)     Lua::getInt(L,i)
#define LUA_GET_long(i)    Lua::getInt(L,i)
#define LUA_GET_float(i)   (float)Lua::getDouble(L,i)
#define LUA_GET_double(i)  Lua::getDouble(L,i)
#define LUA_GET_string(i)  Lua::getString(L,i)

#define LUA_PUSH_int(a)    lua_pushnumber(L,a)
#define LUA_PUSH_float(a)  lua_pushnumber(L,a)
#define LUA_PUSH_double(a) lua_pushnumber(L,a)
#define LUA_PUSH_float(a)  lua_pushstring(L,a)

#define LUA_GET_(T)
#define LUA_GET(T) ,LUA_GET_##T(i++) // commas come from here
#define MAKE_LUA_FUNC(TR,fname,T1,...) int l_##fname(lua_State * L){ int i=0; LUA_PUSH_##TR( fname( LUA_GET_##T1(i++) ARG1(LUA_GET,__VA_ARGS__) ) ); return 1; }

// interface for function:
// double add3(float, int, double );
MAKE_LUA_FUNC( double, add3, float, int, double )
// output:
// 'int l_add3(lua_State * L){ int i=0; lua_pushnumber(L,add3((float)Lua::getDouble(L,i++) ,Lua::getInt(L,i++),Lua::getDouble(L,i++),,,, )); return 1; }'

Working but less-nice solution

I had to duplicate LUA_GET_ macros for case when it is first in argument list (without comma) and otherwise (with comma in front)

// begin of argument list => no commas
#define LUA_GET_int(i)     Lua::getInt(L,i)
#define LUA_GET_long(i)    Lua::getInt(L,i)
#define LUA_GET_float(i)   (float)Lua::getDouble(L,i)
#define LUA_GET_double(i)  Lua::getDouble(L,i)
#define LUA_GET_string(i)  Lua::getString(L,i)

// rest of argument list => with commas
#define LUA_GET__int(i)     ,Lua::getInt(L,i)
#define LUA_GET__long(i)    ,Lua::getInt(L,i)
#define LUA_GET__float(i)   ,(float)Lua::getDouble(L,i)
#define LUA_GET__double(i)  ,Lua::getDouble(L,i)
#define LUA_GET__string(i)  ,Lua::getString(L,i)

#define LUA_PUSH_int(a)    lua_pushnumber(L,a)
#define LUA_PUSH_float(a)  lua_pushnumber(L,a)
#define LUA_PUSH_double(a) lua_pushnumber(L,a)
#define LUA_PUSH_float(a)  lua_pushstring(L,a)

#define LUA_GET_(T)
#define LUA_GET__(T)
#define LUA_GET(T) LUA_GET__##T(i++)
#define MAKE_LUA_FUNC(TR,fname,T1,...) int l_##fname(lua_State * L){ int i=0; LUA_PUSH_##TR( fname( LUA_GET_##T1(i++) ARG1(LUA_GET,__VA_ARGS__) ) ); return 1; }

// MAKE_LUA_FUNC( double, add3, float, int, double )
// output:
// int l_add3(lua_State * L){ int i=0; lua_pushnumber(L,add3( (float)Lua::getDouble(L,i++) ,Lua::getInt(L,i++),Lua::getDouble(L,i++) )); return 1; }

Is it possible to make it simpler / nicer ?

NOTE - for debugging I found very useful this Seeing expanded C macros in particular https://stackoverflow.com/a/31460434/1291544

Prokop Hapala
  • 2,424
  • 2
  • 30
  • 59

1 Answers1

2

You need to count the number of arguments you have, and then call the corresponding ARG# macro.

#define ARGS_N(M,...) \
ARGS_N__(__VA_ARGS__, 6, 5, 4, 3, 2, 1)(M, __VA_ARGS__)

#define ARGS_N__(_1, _2, _3, _4, _5, _6, X, ...) ARGS_##X

#define ARGS_1(M, X)      M(X)
#define ARGS_2(M, X, ...) M(X)ARGS_1(M, __VA_ARGS__)
#define ARGS_3(M, X, ...) M(X)ARGS_2(M, __VA_ARGS__)
#define ARGS_4(M, X, ...) M(X)ARGS_3(M, __VA_ARGS__)
#define ARGS_5(M, X, ...) M(X)ARGS_4(M, __VA_ARGS__)
#define ARGS_6(M, X, ...) M(X)ARGS_5(M, __VA_ARGS__)

Now, change MAKE_LUA_FUNC to call ARGS_N instead of your ARG1.


The way the counting technique works is that ARGS_N invokes the helper ARGS_N__ with the variable arguments, and then pads out the invocation with additional arguments. ARGS_N__ does the counting by always utilizing the 7th argument. So, if ARGS_N is provided 4 variable arguments after the first one, ARGS_N__ will produce ARGS_4, because in that case, in the padding provided by ARGS_N, 4 would be the 7th argument.

ARGS_N__(__VA_ARGS__, 6, 5, 4, 3, 2, 1)(M, __VA_ARGS__)
         .                  .
        /|\                /|\
         |                  |
         If this has 4 arguments
                            |
                            This would be the 7th argument

This is the same technique that was shown in the answer you pointed to. However, that version was a bit more complicated than the version I am illustrating for you, so hopefully you will find this explanation helpful.

jxh
  • 69,070
  • 8
  • 110
  • 193