LuaWrapper.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * LuaWrapper.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 "LuaCallWrapper.h"
  12. /*
  13. * Original code is LunaWrapper by nornagon.
  14. * https://lua-users.org/wiki/LunaWrapper
  15. * Published under the BSD 2-clause license
  16. * https://opensource.org/licenses/BSD-2-Clause
  17. *
  18. */
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. namespace scripting
  21. {
  22. namespace detail
  23. {
  24. struct CustomRegType
  25. {
  26. const char * name;
  27. lua_CFunction functor;
  28. bool isStatic;
  29. };
  30. template <typename P, typename U>
  31. struct Dispatcher
  32. {
  33. using ProxyType = P;
  34. using UDataType = U;
  35. static void setIndexTable(lua_State * L)
  36. {
  37. lua_pushstring(L, "__index");
  38. lua_newtable(L);
  39. for(auto & reg : ProxyType::REGISTER_CUSTOM)
  40. {
  41. if(!reg.isStatic)
  42. {
  43. lua_pushstring(L, reg.name);
  44. lua_pushcclosure(L, reg.functor, 0);
  45. lua_rawset(L, -3);
  46. }
  47. }
  48. lua_rawset(L, -3);
  49. }
  50. static void pushStaticTable(lua_State * L)
  51. {
  52. lua_newtable(L);
  53. lua_newtable(L);
  54. lua_pushstring(L, "__index");
  55. {
  56. lua_newtable(L);
  57. for(auto & reg : ProxyType::REGISTER_CUSTOM)
  58. {
  59. if(reg.isStatic)
  60. {
  61. lua_pushstring(L, reg.name);
  62. lua_pushcclosure(L, reg.functor, 0);
  63. lua_rawset(L, -3);
  64. }
  65. }
  66. }
  67. lua_rawset(L, -3);
  68. lua_pushstring(L, "__newindex");
  69. lua_pushnil(L);
  70. lua_rawset(L, -3);
  71. lua_setmetatable(L, -2);
  72. }
  73. static int destructor(lua_State * L)
  74. {
  75. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  76. void * objPtr = luaL_checkudata(L, 1, KEY);
  77. if(objPtr)
  78. {
  79. auto obj = static_cast<UDataType *>(objPtr);
  80. obj->reset();
  81. }
  82. lua_settop(L, 0);
  83. return 0;
  84. }
  85. };
  86. }
  87. class RegistarBase : public api::Registar
  88. {
  89. public:
  90. protected:
  91. virtual void adjustMetatable(lua_State * L) const
  92. {
  93. }
  94. virtual void adjustStaticTable(lua_State * L) const
  95. {
  96. }
  97. };
  98. template<class T, class Proxy = T>
  99. class OpaqueWrapper : public RegistarBase
  100. {
  101. public:
  102. using ObjectType = typename std::remove_cv<T>::type;
  103. using UDataType = ObjectType *;
  104. using CUDataType = const ObjectType *;
  105. using CustomRegType = detail::CustomRegType;
  106. void pushMetatable(lua_State * L) const override final
  107. {
  108. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  109. static auto S_KEY = api::TypeRegistry::get()->getKey<CUDataType>();
  110. LuaStack S(L);
  111. if(luaL_newmetatable(L, KEY) != 0)
  112. adjustMetatable(L);
  113. S.balance();
  114. if(luaL_newmetatable(L, S_KEY) != 0)
  115. adjustMetatable(L);
  116. S.balance();
  117. detail::Dispatcher<Proxy, UDataType>::pushStaticTable(L);
  118. adjustStaticTable(L);
  119. }
  120. protected:
  121. void adjustMetatable(lua_State * L) const override
  122. {
  123. detail::Dispatcher<Proxy, UDataType>::setIndexTable(L);
  124. }
  125. };
  126. template<class T, class Proxy = T>
  127. class SharedWrapper : public RegistarBase
  128. {
  129. public:
  130. using ObjectType = typename std::remove_cv<T>::type;
  131. using UDataType = std::shared_ptr<T>;
  132. using CustomRegType = detail::CustomRegType;
  133. static int constructor(lua_State * L)
  134. {
  135. LuaStack S(L);
  136. S.clear();//we do not accept any parameters in constructor
  137. auto obj = std::make_shared<T>();
  138. S.push(obj);
  139. return 1;
  140. }
  141. void pushMetatable(lua_State * L) const override final
  142. {
  143. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  144. LuaStack S(L);
  145. if(luaL_newmetatable(L, KEY) != 0)
  146. {
  147. adjustMetatable(L);
  148. S.push("__gc");
  149. lua_pushcfunction(L, &(detail::Dispatcher<Proxy, UDataType>::destructor));
  150. lua_rawset(L, -3);
  151. }
  152. S.balance();
  153. detail::Dispatcher<Proxy, UDataType>::pushStaticTable(L);
  154. adjustStaticTable(L);
  155. }
  156. protected:
  157. void adjustMetatable(lua_State * L) const override
  158. {
  159. detail::Dispatcher<Proxy, UDataType>::setIndexTable(L);
  160. }
  161. };
  162. template<class T, class Proxy = T>
  163. class UniqueOpaqueWrapper : public api::Registar
  164. {
  165. public:
  166. using ObjectType = typename std::remove_cv<T>::type;
  167. using UDataType = std::unique_ptr<T>;
  168. using CustomRegType = detail::CustomRegType;
  169. void pushMetatable(lua_State * L) const override final
  170. {
  171. static auto KEY = api::TypeRegistry::get()->getKey<UDataType>();
  172. LuaStack S(L);
  173. if(luaL_newmetatable(L, KEY) != 0)
  174. {
  175. // detail::Dispatcher<Proxy, UDataType>::setIndexTable(L);
  176. S.push("__gc");
  177. lua_pushcfunction(L, &(detail::Dispatcher<Proxy, UDataType>::destructor));
  178. lua_rawset(L, -3);
  179. }
  180. S.balance();
  181. detail::Dispatcher<Proxy, UDataType>::pushStaticTable(L);
  182. }
  183. };
  184. }
  185. VCMI_LIB_NAMESPACE_END