Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 6, 2017, 5:23:08 PM (7 years ago)
Author:
kohlia
Message:

Not working yet

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/ScriptableController_HS17/src/orxonox/scriptablecontroller/luatb.ipp

    r11519 r11549  
    33#include <iostream>
    44#include <type_traits>
    5 #include "scriptable_controller_api.h"
    65#include "luatb_typed_stack.h"
    76
     
    2019    {
    2120        // Store the 'this' pointer of the caller in the extraspace
    22 //        *static_cast<ThisType**>(lua_getextraspace(lua)) = _this;
     21        LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua] = _this;
    2322
    2423        // Make a function visible to lua that will call 'func' with the correct
     
    2827
    2928private:
     29    static std::map<lua_State*, ThisType*> stateToClassMap;
     30
    3031    // Represents a function that can made visible to lua with the correct
    3132    // signature. It will call the corresponding C++ function with the
     
    3435    static int toLuaSignature(lua_State *lua)
    3536    {
    36         // The number of arguments is the first item on the stack
    37         int argc = lua_tointeger(lua, lua_gettop(lua));
    38         lua_pop(lua, 1);
     37        // The index of the topmost item on the stack equals the size of
     38        // the stack, which also equals the number of arguments
     39        int argc = lua_gettop(lua);
    3940
    4041        // Check if the number of arguments match
    4142        if(argc != sizeof...(Args))
    4243        {
    43             std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments" << std::endl;
     44            std::cerr << "ERROR: LuaTB: Lua script called a function with wrong number of arguments (" << argc << " given, " << sizeof...(Args) << " expected)" << std::endl;
    4445            return LUA_ERRSYNTAX;
    4546        }
    46 
    47         // Retrieve 'this' pointer of caller
    48 //        ThisType *_this = *static_cast<ThisType**>(lua_getextraspace(lua));
    49 ThisType *_this = orxonox::ScriptableControllerAPI::this_;
     47        orxonox::orxout(orxonox::user_warning) << "what" << std::endl;
    5048
    5149        // Call getArgument for every argument seperately to convert each argument
    5250        // to the correct type and call the function afterwards
    53         ((*_this).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
     51        ((*LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap[lua]).*func)( (LuaTBTypedStack::getArgument<Args>(lua))... );
    5452
    5553        return 0;
    5654    }
    5755};
     56
     57// This needs to be here and not in a source file, because the compiler can't know
     58// the template types if it's in a separate module.
     59template<typename ThisType, typename Ret, typename... Args>
     60std::map<lua_State*, ThisType*> LuaTB<ThisType, Ret (ThisType::*)(Args...)>::stateToClassMap = std::map<lua_State*, ThisType*>();
Note: See TracChangeset for help on using the changeset viewer.