Tomasu / LuaGlue

C++11 Lua 5.2 Binding Library
zlib License
79 stars 22 forks source link

[ -- additional -- ] invoke function #57

Open DominikMS opened 10 years ago

DominikMS commented 10 years ago

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:

        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);
        }
Tomasu commented 10 years ago

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.

Tomasu commented 10 years ago

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").