Search code examples
parameterslualuabind

luabind - variable number of parameters


How can I bind a function with luabind that accept a variable number of parameters ? Basically, I want to write my own print() function.

I know that the object class in luabind as a parameter can accept any data type, the best would be to received a dynamic table of luabind::object as a parameter.


Solution

  • I have made a mix between pure lua C API and luabind:

    int myPrint(lua_State* L)
    {
        int argCount = lua_gettop(L);
    
        for(int i = 1; i <= argCount; i++)
        {
            luabind::object obj(luabind::from_stack(L, i));
    
            switch(luabind::type(obj))
            {
                case LUA_TSTRING:
                    cout << luabind::object_cast<std::string>(obj);
                    break;
                case LUA_TNUMBER:
                    cout << luabind::object_cast<double>(obj);
                    break;
                case LUA_TBOOLEAN:
                    cout << boolalpha << luabind::object_cast<bool>(obj);
                    break;
                case LUA_TNIL:
                    cout << "#Nil#";
                    break;
                default:
                    cout << "#Unknown type '" << luabind::type(obj) << "'#";
                    break;
            }
        }
    
        cout << endl;
    
        return 0;
    }