I have a variadic function accepting any number of mixed parameters: the following code works as expected:
template <typename ... Args>
void call_snippet(lua_State *L, const std::string& name, Args... args) {
lua_rawgeti(L, LUA_REGISTRYINDEX, snippets[name]);
int nargs = 0;
for (auto &&x : {args...}) {
lua_pushinteger(L, x);
nargs++;
}
lua_pcall(L, nargs, LUA_MULTRET, 0);
}
but falls short from needs because it assumes all parameters are int
(or convertible to int
). I would need something along the lines:
template <typename ... Args>
void call_snippet(lua_State *L, const std::string& name, Args... args) {
lua_rawgeti(L, LUA_REGISTRYINDEX, snippets[name]);
int nargs = 0;
for (auto &&x : {args...}) {
switch (typeof(x)) {
case int:
lua_pushinteger(L, x);
nargs++;
break;
case float:
lua_pushnumber(L, x);
nargs++;
break;
case std:string:
lua_pushcstring(L, x.c_str());
nargs++;
break;
case char*:
lua_pushcstring(L, x);
nargs++;
break;
default:
//raise error
;
}
lua_pcall(L, nargs, LUA_MULTRET, 0);
}
How should I actually implement the above pseudocode?