/* * LuaCallWrapper.h, part of VCMI engine * * Authors: listed in file AUTHORS in main folder * * License: GNU General Public License v2.0 or later * Full text of license available in license.txt file, in main folder * */ #pragma once #include "api/Registry.h" #include "LuaStack.h" #include "LuaFunctor.h" namespace scripting { namespace detail { } //TODO: this should be the only one wrapper type // template class LuaMethodWrapper { }; template class LuaMethodWrapper { public: static int invoke(lua_State * L) { LuaStack S(L); const U * obj = nullptr; if(!S.tryGet(1,obj)) return S.retVoid(); static auto functor = std::mem_fn(method); S.clear(); S.push(functor(obj)); return S.retPushed(); } }; template class LuaMethodWrapper { public: static int invoke(lua_State * L) { LuaStack S(L); const U * obj = nullptr; if(!S.tryGet(1,obj)) return S.retVoid(); static auto functor = std::mem_fn(method); S.clear(); functor(obj); return 0; } }; template class LuaMethodWrapper { public: static int invoke(lua_State * L) { LuaStack S(L); const U * obj = nullptr; if(!S.tryGet(1,obj)) return S.retVoid(); P1 p1; if(!S.tryGet(2, p1)) return S.retVoid(); static auto functor = std::mem_fn(method); S.clear(); S.push(functor(obj, p1)); return S.retPushed(); } }; template class LuaMethodWrapper { public: static int invoke(lua_State * L) { LuaStack S(L); const U * obj = nullptr; if(!S.tryGet(1,obj)) return S.retVoid(); P1 p1; if(!S.tryGet(2, p1)) return S.retVoid(); static auto functor = std::mem_fn(method); S.clear(); functor(obj, p1); return 0; } }; template class LuaMethodWrapper { public: static int invoke(lua_State * L) { LuaStack S(L); const U * obj = nullptr; if(!S.tryGet(1, obj)) return S.retVoid(); P1 p1; if(!S.tryGet(2, p1)) return S.retVoid(); P2 p2; if(!S.tryGet(3, p2)) return S.retVoid(); static auto functor = std::mem_fn(method); S.clear(); S.push(functor(obj, p1, p2)); return S.retPushed(); } }; template class LuaMethodWrapper { public: static int invoke(lua_State * L) { LuaStack S(L); const U * obj = nullptr; if(!S.tryGet(1, obj)) return S.retVoid(); P1 p1; if(!S.tryGet(2, p1)) return S.retVoid(); P2 p2; if(!S.tryGet(3, p2)) return S.retVoid(); static auto functor = std::mem_fn(method); S.clear(); functor(obj, p1, p2); return 0; } }; //deprecated, should use LuaMethodWrapper instead, once implemented template class LuaCallWrapper { public: using Wrapped = typename std::remove_const::type; static std::function createFunctor(void (Wrapped::* method)()) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); functor(object); return S.retVoid(); }; return ret; } static std::function createFunctor(void (Wrapped::* method)() const) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); functor(object); return S.retVoid(); }; return ret; } template static std::function createFunctor(R (Wrapped::* method)()) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); S.clear(); S.push(functor(object)); return S.retPushed(); }; return ret; } template static std::function createFunctor(R (Wrapped::* method)() const) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); S.clear(); S.push(functor(object)); return S.retPushed(); }; return ret; } template static std::function createFunctor(void (Wrapped::* method)(P1)) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); P1 p1; if(S.tryGet(1, p1)) { functor(object, p1); } return S.retVoid(); }; return ret; } template static std::function createFunctor(void (Wrapped::* method)(P1) const) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); P1 p1; if(S.tryGet(1, p1)) { functor(object, p1); } return S.retVoid(); }; return ret; } template static std::function createFunctor(R (Wrapped::* method)(P1)) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); P1 p1; if(S.tryGet(1, p1)) { S.push(functor(object, p1)); return 1; } return S.retVoid(); }; return ret; } template static std::function createFunctor(R (Wrapped::* method)(P1) const) { auto functor = std::mem_fn(method); auto ret = [=](lua_State * L, T * object)->int { LuaStack S(L); P1 p1; if(S.tryGet(1, p1)) { S.push(functor(object, p1)); return 1; } return S.retVoid(); }; return ret; } }; }