LuaCallWrapper.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /*
  2. * LuaCallWrapper.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "api/Registry.h"
  12. #include "LuaStack.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. namespace scripting
  15. {
  16. namespace detail
  17. {
  18. template<int ...>
  19. struct Seq {};
  20. template<int N, int ...S>
  21. struct Gens : Gens<N-1, N-1, S...> {};
  22. template<int ...S>
  23. struct Gens<0, S...>
  24. {
  25. typedef Seq<S...> type;
  26. };
  27. template <typename R, typename ... Args>
  28. class LuaArgumentsTuple
  29. {
  30. public:
  31. using TupleData = std::tuple<Args ...>;
  32. using Functor = R(*)(Args ...);
  33. TupleData args;
  34. Functor f;
  35. LuaArgumentsTuple(Functor _f)
  36. :f(_f),
  37. args()
  38. {
  39. }
  40. STRONG_INLINE int invoke(lua_State * L)
  41. {
  42. return callFunc(L, typename Gens<sizeof...(Args)>::type());
  43. }
  44. private:
  45. template<int ...N>
  46. int callFunc(lua_State * L, Seq<N...>)
  47. {
  48. LuaStack S(L);
  49. bool ok[sizeof...(Args)] = {(S.tryGet(N+1, std::get<N>(args)))...};
  50. if(std::count(std::begin(ok), std::end(ok), false) > 0)
  51. return S.retVoid();
  52. R ret = f(std::get<N>(args) ...);
  53. S.clear();
  54. S.push(ret);
  55. return 1;
  56. }
  57. };
  58. class LuaFunctionInvoker
  59. {
  60. public:
  61. template<typename R, typename ... Args>
  62. static STRONG_INLINE int invoke(lua_State * L, R(*f)(Args ...))
  63. {
  64. LuaArgumentsTuple<R, Args ...> args(f);
  65. return args.invoke(L);
  66. }
  67. };
  68. }
  69. template <typename F, F f>
  70. class LuaFunctionWrapper
  71. {
  72. public:
  73. static int invoke(lua_State * L)
  74. {
  75. return detail::LuaFunctionInvoker::invoke(L, f);
  76. }
  77. };
  78. //TODO: this should be the only one wrapper type
  79. //
  80. template <typename U, typename M, M m>
  81. class LuaMethodWrapper
  82. {
  83. };
  84. template <typename U, typename T, typename R, R(T:: * method)()const>
  85. class LuaMethodWrapper <U, R(T:: *)()const, method>
  86. {
  87. public:
  88. static int invoke(lua_State * L)
  89. {
  90. LuaStack S(L);
  91. const U * obj = nullptr;
  92. if(!S.tryGet(1,obj))
  93. return S.retVoid();
  94. static auto functor = std::mem_fn(method);
  95. S.clear();
  96. S.push(functor(obj));
  97. return S.retPushed();
  98. }
  99. };
  100. template <typename U, typename T, typename R, R(T:: * method)()>
  101. class LuaMethodWrapper <U, R(T:: *)(), method>
  102. {
  103. public:
  104. static int invoke(lua_State * L)
  105. {
  106. LuaStack S(L);
  107. U * obj = nullptr;
  108. if(!S.tryGet(1,obj))
  109. return S.retVoid();
  110. static auto functor = std::mem_fn(method);
  111. S.clear();
  112. S.push(functor(obj));
  113. return S.retPushed();
  114. }
  115. };
  116. template <typename U, typename T, void(T:: * method)()const>
  117. class LuaMethodWrapper <U, void(T:: *)()const, method>
  118. {
  119. public:
  120. static int invoke(lua_State * L)
  121. {
  122. LuaStack S(L);
  123. const U * obj = nullptr;
  124. if(!S.tryGet(1,obj))
  125. return S.retVoid();
  126. static auto functor = std::mem_fn(method);
  127. S.clear();
  128. functor(obj);
  129. return 0;
  130. }
  131. };
  132. template <typename U, typename T, void(T:: * method)()>
  133. class LuaMethodWrapper <U, void(T:: *)(), method>
  134. {
  135. public:
  136. static int invoke(lua_State * L)
  137. {
  138. LuaStack S(L);
  139. U * obj = nullptr;
  140. if(!S.tryGet(1,obj))
  141. return S.retVoid();
  142. static auto functor = std::mem_fn(method);
  143. S.clear();
  144. functor(obj);
  145. return 0;
  146. }
  147. };
  148. template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)const>
  149. class LuaMethodWrapper <U, R(T:: *)(P1)const, method>
  150. {
  151. public:
  152. static int invoke(lua_State * L)
  153. {
  154. LuaStack S(L);
  155. const U * obj = nullptr;
  156. if(!S.tryGet(1,obj))
  157. return S.retVoid();
  158. P1 p1;
  159. if(!S.tryGet(2, p1))
  160. return S.retVoid();
  161. static auto functor = std::mem_fn(method);
  162. S.clear();
  163. S.push(functor(obj, p1));
  164. return S.retPushed();
  165. }
  166. };
  167. template <typename U, typename T, typename R, typename P1, R(T:: * method)(P1)>
  168. class LuaMethodWrapper <U, R(T:: *)(P1), method>
  169. {
  170. public:
  171. static int invoke(lua_State * L)
  172. {
  173. LuaStack S(L);
  174. U * obj = nullptr;
  175. if(!S.tryGet(1,obj))
  176. return S.retVoid();
  177. P1 p1;
  178. if(!S.tryGet(2, p1))
  179. return S.retVoid();
  180. static auto functor = std::mem_fn(method);
  181. S.clear();
  182. S.push(functor(obj, p1));
  183. return S.retPushed();
  184. }
  185. };
  186. template <typename U, typename T, typename P1, void(T:: * method)(P1)const>
  187. class LuaMethodWrapper <U, void(T:: *)(P1)const, method>
  188. {
  189. public:
  190. static int invoke(lua_State * L)
  191. {
  192. LuaStack S(L);
  193. const U * obj = nullptr;
  194. if(!S.tryGet(1,obj))
  195. return S.retVoid();
  196. P1 p1;
  197. if(!S.tryGet(2, p1))
  198. return S.retVoid();
  199. static auto functor = std::mem_fn(method);
  200. S.clear();
  201. functor(obj, p1);
  202. return 0;
  203. }
  204. };
  205. template <typename U, typename T, typename P1, void(T:: * method)(P1)>
  206. class LuaMethodWrapper <U, void(T:: *)(P1), method>
  207. {
  208. public:
  209. static int invoke(lua_State * L)
  210. {
  211. LuaStack S(L);
  212. U * obj = nullptr;
  213. if(!S.tryGet(1,obj))
  214. return S.retVoid();
  215. P1 p1;
  216. if(!S.tryGet(2, p1))
  217. return S.retVoid();
  218. static auto functor = std::mem_fn(method);
  219. S.clear();
  220. functor(obj, p1);
  221. return 0;
  222. }
  223. };
  224. template <typename U, typename T, typename R, typename P1, typename P2, R(T:: * method)(P1, P2)const>
  225. class LuaMethodWrapper <U, R(T:: *)(P1, P2)const, method>
  226. {
  227. public:
  228. static int invoke(lua_State * L)
  229. {
  230. LuaStack S(L);
  231. const U * obj = nullptr;
  232. if(!S.tryGet(1, obj))
  233. return S.retVoid();
  234. P1 p1;
  235. if(!S.tryGet(2, p1))
  236. return S.retVoid();
  237. P2 p2;
  238. if(!S.tryGet(3, p2))
  239. return S.retVoid();
  240. static auto functor = std::mem_fn(method);
  241. S.clear();
  242. S.push(functor(obj, p1, p2));
  243. return S.retPushed();
  244. }
  245. };
  246. template <typename U, typename T, typename P1, typename P2, void(T:: * method)(P1, P2)const>
  247. class LuaMethodWrapper <U, void(T:: *)(P1, P2)const, method>
  248. {
  249. public:
  250. static int invoke(lua_State * L)
  251. {
  252. LuaStack S(L);
  253. const U * obj = nullptr;
  254. if(!S.tryGet(1, obj))
  255. return S.retVoid();
  256. P1 p1;
  257. if(!S.tryGet(2, p1))
  258. return S.retVoid();
  259. P2 p2;
  260. if(!S.tryGet(3, p2))
  261. return S.retVoid();
  262. static auto functor = std::mem_fn(method);
  263. S.clear();
  264. functor(obj, p1, p2);
  265. return 0;
  266. }
  267. };
  268. }
  269. VCMI_LIB_NAMESPACE_END