Skip to content

[ -- additional -- ] invoke function #57

@DominikMS

Description

@DominikMS

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);
        }

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions