LuaGlue
LuaGlue copied to clipboard
[ -- additional -- ] invoke function
Problem:
Double function named invokeFunction
Example:
g_luaState.invokeTabledFunction<int>("tab.func", 5);
g_luaState.invokeTabledFunction<int>("SandBoxed.func", 5);
g_luaState.invokeTabledFunction<int>("func", 5);
Todo:
- add to: void invokeFunction
- add to: void setGlobal
- add to: Type_ getGlobal
Code:
template<typename Ret_, typename... Args_>
Ret_ invokeFunction(const std::string &name, Args_&&... args)
{
const unsigned int Arg_Count_ = sizeof...(Args_);
// explode
std::vector<std::string> explode;
std::size_t lastpos = 0, pos = name.find(".");
if(pos == std::string::npos)
explode.push_back(name);
while(pos != std::string::npos)
{
explode.push_back(std::string(name, lastpos, pos - lastpos).c_str());
lastpos = ++pos;
if((pos = name.find(".", pos)) == std::string::npos)
explode.push_back(std::string(name, lastpos, pos - lastpos).c_str());
}
// find func
lua_getglobal(state_, explode[0].c_str());
if(explode.size() > 1)
{
for(auto it = explode.begin() + 1; it != explode.end(); it++)
{
lua_getfield(state_, -1, (*it).c_str());
if(lua_isnil(state_, -1)) // table not found
return Ret_();
}
}
applyTupleLuaFunc(this, state_, std::forward<Args_>(args)...);
lua_pcall(state_, Arg_Count_, 1, 0);
return stack<Ret_>::get(this, state_, -1);
}
Ah crud. I didn't think about that. Yeah, the namespace stuff isn't in yet, so I can just remove the one with a namespace.
I think something like that would work well with future support for namespaces in Class<>(). For instance: glue.Class<Some::NameSpace::Foo>("Some.Namespace.Foo").