| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 | 
							- /*
 
-  * 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 <type_traits>
 
- VCMI_LIB_NAMESPACE_BEGIN
 
- namespace scripting
 
- {
 
- namespace detail
 
- {
 
- template<int ...>
 
- struct Seq {};
 
- template<int N, int ...S>
 
- struct Gens : Gens<N-1, N-1, S...> {};
 
- template<int ...S>
 
- struct Gens<0, S...>
 
- {
 
- 	using type = Seq<S...>;
 
- };
 
- template <typename R, typename ... Args>
 
- class LuaArgumentsTuple
 
- {
 
- public:
 
- 	using TupleData = std::tuple<Args ...>;
 
- 	using Functor = R(*)(Args ...);
 
- 	TupleData args;
 
- 	Functor f;
 
- 	LuaArgumentsTuple(Functor _f)
 
- 		:f(_f),
 
- 		args()
 
- 	{
 
- 	}
 
- 	STRONG_INLINE int invoke(lua_State * L)
 
- 	{
 
- 		return callFunc(L, typename Gens<sizeof...(Args)>::type());
 
- 	}
 
- private:
 
- 	template<int ...N>
 
- 	int callFunc(lua_State * L, Seq<N...>)
 
- 	{
 
- 		LuaStack S(L);
 
- 		bool ok[sizeof...(Args)] = {(S.tryGet(N+1, std::get<N>(args)))...};
 
- 		if(std::count(std::begin(ok), std::end(ok), false) > 0)
 
- 			return S.retVoid();
 
- 		R ret = f(std::get<N>(args) ...);
 
- 		S.clear();
 
- 		S.push(ret);
 
- 		return 1;
 
- 	}
 
- };
 
- class LuaFunctionInvoker
 
- {
 
- public:
 
- 	template<typename R, typename ... Args>
 
- 	static STRONG_INLINE int invoke(lua_State * L, R(*f)(Args ...))
 
- 	{
 
- 		LuaArgumentsTuple<R, Args ...> args(f);
 
- 		return args.invoke(L);
 
- 	}
 
- };
 
- }
 
- template <typename F, F f>
 
- class LuaFunctionWrapper
 
- {
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		return detail::LuaFunctionInvoker::invoke(L, f);
 
- 	}
 
- };
 
- //TODO: this should be the only one wrapper type
 
- //
 
- template <typename U, typename M, M m>
 
- class LuaMethodWrapper
 
- {
 
- };
 
- template <typename U, typename T, typename R, R(T:: * method)()const>
 
- class LuaMethodWrapper <U, R(T:: *)()const, method>
 
- {
 
- 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 <typename U, typename T, typename R, R(T:: * method)()>
 
- class LuaMethodWrapper <U, R(T:: *)(), method>
 
- {
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		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 <typename U, typename T, void(T:: * method)()const>
 
- class LuaMethodWrapper <U, void(T:: *)()const, method>
 
- {
 
- 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 <typename U, typename T, void(T:: * method)()>
 
- class LuaMethodWrapper <U, void(T:: *)(), method>
 
- {
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		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 <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)const>
 
- class LuaMethodWrapper <U, R(T:: *)(P1)const, method>
 
- {
 
- 	using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		const U * obj = nullptr;
 
- 		if(!S.tryGet(1,obj))
 
- 			return S.retVoid();
 
- 		PM1 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 <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)>
 
- class LuaMethodWrapper <U, R(T:: *)(P1), method>
 
- {
 
- 	using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		U * obj = nullptr;
 
- 		if(!S.tryGet(1,obj))
 
- 			return S.retVoid();
 
- 		PM1 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 <typename U, typename T, typename P1, void(T:: * method)(P1)const>
 
- class LuaMethodWrapper <U, void(T:: *)(P1)const, method>
 
- {
 
- 	using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		const U * obj = nullptr;
 
- 		if(!S.tryGet(1,obj))
 
- 			return S.retVoid();
 
- 		PM1 p1;
 
- 		if(!S.tryGet(2, p1))
 
- 			return S.retVoid();
 
- 		static auto functor = std::mem_fn(method);
 
- 		S.clear();
 
- 		functor(obj, p1);
 
- 		return 0;
 
- 	}
 
- };
 
- template <typename U, typename T, typename P1, void(T:: * method)(P1)>
 
- class LuaMethodWrapper <U, void(T:: *)(P1), method>
 
- {
 
- 	using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		U * obj = nullptr;
 
- 		if(!S.tryGet(1,obj))
 
- 			return S.retVoid();
 
- 		PM1 p1;
 
- 		if(!S.tryGet(2, p1))
 
- 			return S.retVoid();
 
- 		static auto functor = std::mem_fn(method);
 
- 		S.clear();
 
- 		functor(obj, p1);
 
- 		return 0;
 
- 	}
 
- };
 
- template <typename U, typename T, typename R, typename P1, typename P2, R(T:: * method)(P1, P2)const>
 
- class LuaMethodWrapper <U, R(T:: *)(P1, P2)const, method>
 
- {
 
- 	using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
 
- 	using PM2 = std::remove_cv_t<std::remove_reference_t<P2>>;
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		const U * obj = nullptr;
 
- 		if(!S.tryGet(1, obj))
 
- 			return S.retVoid();
 
- 		PM1 p1;
 
- 		if(!S.tryGet(2, p1))
 
- 			return S.retVoid();
 
- 		PM2 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 <typename U, typename T, typename P1, typename P2, void(T:: * method)(P1, P2)const>
 
- class LuaMethodWrapper <U, void(T:: *)(P1, P2)const, method>
 
- {
 
- 	using PM1 = std::remove_cv_t<std::remove_reference_t<P1>>;
 
- 	using PM2 = std::remove_cv_t<std::remove_reference_t<P2>>;
 
- public:
 
- 	static int invoke(lua_State * L)
 
- 	{
 
- 		LuaStack S(L);
 
- 		const U * obj = nullptr;
 
- 		if(!S.tryGet(1, obj))
 
- 			return S.retVoid();
 
- 		PM1 p1;
 
- 		if(!S.tryGet(2, p1))
 
- 			return S.retVoid();
 
- 		PM2 p2;
 
- 		if(!S.tryGet(3, p2))
 
- 			return S.retVoid();
 
- 		static auto functor = std::mem_fn(method);
 
- 		S.clear();
 
- 		functor(obj, p1, p2);
 
- 		return 0;
 
- 	}
 
- };
 
- }
 
- VCMI_LIB_NAMESPACE_END
 
 
  |