LuaStack.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * LuaStack.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 "../../lib/GameConstants.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. class JsonNode;
  15. class int3;
  16. namespace scripting
  17. {
  18. namespace detail
  19. {
  20. template<typename T>
  21. struct IsRegularClass
  22. {
  23. static constexpr auto value = std::is_class<T>::value && !std::is_base_of<IdTag, T>::value;
  24. };
  25. template<typename T>
  26. struct IsIdClass
  27. {
  28. static constexpr auto value = std::is_class<T>::value && std::is_base_of<IdTag, T>::value;
  29. };
  30. }
  31. class LuaStack
  32. {
  33. public:
  34. LuaStack(lua_State * L_);
  35. void balance();
  36. void clear();
  37. void pushByIndex(lua_Integer index);
  38. void pushNil();
  39. void pushInteger(lua_Integer value);
  40. void push(bool value);
  41. void push(const char * value);
  42. void push(const std::string & value);
  43. void push(const JsonNode & value);
  44. template<typename T>
  45. void push(const boost::optional<T> & value)
  46. {
  47. if(value.is_initialized())
  48. push(value.get());
  49. else
  50. pushNil();
  51. }
  52. template<typename T, typename std::enable_if< std::is_integral<T>::value && !std::is_same<T, bool>::value, int>::type = 0>
  53. void push(const T value)
  54. {
  55. pushInteger(static_cast<lua_Integer>(value));
  56. }
  57. template<typename T, typename std::enable_if< std::is_enum<T>::value, int>::type = 0>
  58. void push(const T value)
  59. {
  60. pushInteger(static_cast<lua_Integer>(value));
  61. }
  62. void push(const int3 & value);
  63. template<typename T, typename std::enable_if< detail::IsIdClass<T>::value, int>::type = 0>
  64. void push(const T & value)
  65. {
  66. pushInteger(static_cast<lua_Integer>(value.toEnum()));
  67. }
  68. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value, int>::type = 0>
  69. void push(T * value)
  70. {
  71. using UData = T *;
  72. static auto KEY = api::TypeRegistry::get()->getKey<UData>();
  73. if(!value)
  74. {
  75. pushNil();
  76. return;
  77. }
  78. void * raw = lua_newuserdata(L, sizeof(UData));
  79. if(!raw)
  80. {
  81. pushNil();
  82. return;
  83. }
  84. UData * ptr = static_cast<UData *>(raw);
  85. *ptr = value;
  86. luaL_getmetatable(L, KEY);
  87. lua_setmetatable(L, -2);
  88. }
  89. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value, int>::type = 0>
  90. void push(std::shared_ptr<T> value)
  91. {
  92. using UData = std::shared_ptr<T>;
  93. static auto KEY = api::TypeRegistry::get()->getKey<UData>();
  94. if(!value)
  95. {
  96. pushNil();
  97. return;
  98. }
  99. void * raw = lua_newuserdata(L, sizeof(UData));
  100. if(!raw)
  101. {
  102. pushNil();
  103. return;
  104. }
  105. new(raw) UData(value);
  106. luaL_getmetatable(L, KEY);
  107. lua_setmetatable(L, -2);
  108. }
  109. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value, int>::type = 0>
  110. void push(std::unique_ptr<T> && value)
  111. {
  112. using UData = std::unique_ptr<T>;
  113. static auto KEY = api::TypeRegistry::get()->getKey<UData>();
  114. if(!value)
  115. {
  116. pushNil();
  117. return;
  118. }
  119. void * raw = lua_newuserdata(L, sizeof(UData));
  120. if(!raw)
  121. {
  122. pushNil();
  123. return;
  124. }
  125. new(raw) UData(std::move(value));
  126. luaL_getmetatable(L, KEY);
  127. lua_setmetatable(L, -2);
  128. }
  129. bool tryGetInteger(int position, lua_Integer & value);
  130. bool tryGet(int position, bool & value);
  131. template<typename T, typename std::enable_if< std::is_integral<T>::value && !std::is_same<T, bool>::value, int>::type = 0>
  132. bool tryGet(int position, T & value)
  133. {
  134. lua_Integer temp;
  135. if(tryGetInteger(position, temp))
  136. {
  137. value = static_cast<T>(temp);
  138. return true;
  139. }
  140. else
  141. {
  142. return false;
  143. }
  144. }
  145. template<typename T, typename std::enable_if<detail::IsIdClass<T>::value, int>::type = 0>
  146. bool tryGet(int position, T & value)
  147. {
  148. lua_Integer temp;
  149. if(tryGetInteger(position, temp))
  150. {
  151. value = T(temp);
  152. return true;
  153. }
  154. else
  155. {
  156. return false;
  157. }
  158. }
  159. template<typename T, typename std::enable_if< std::is_enum<T>::value, int>::type = 0>
  160. bool tryGet(int position, T & value)
  161. {
  162. lua_Integer temp;
  163. if(tryGetInteger(position, temp))
  164. {
  165. value = static_cast<T>(temp);
  166. return true;
  167. }
  168. else
  169. {
  170. return false;
  171. }
  172. }
  173. bool tryGet(int position, int3 & value);
  174. bool tryGet(int position, double & value);
  175. bool tryGet(int position, std::string & value);
  176. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value && std::is_const<T>::value, int>::type = 0>
  177. STRONG_INLINE bool tryGet(int position, T * & value)
  178. {
  179. using NCValue = typename std::remove_const<T>::type;
  180. using UData = NCValue *;
  181. using CUData = T *;
  182. return tryGetCUData<T *, UData, CUData>(position, value);
  183. }
  184. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value && !std::is_const<T>::value, int>::type = 0>
  185. STRONG_INLINE bool tryGet(int position, T * & value)
  186. {
  187. return tryGetUData<T *>(position, value);
  188. }
  189. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value && std::is_const<T>::value, int>::type = 0>
  190. STRONG_INLINE bool tryGet(int position, std::shared_ptr<T> & value)
  191. {
  192. using NCValue = typename std::remove_const<T>::type;
  193. using UData = std::shared_ptr<NCValue>;
  194. using CUData = std::shared_ptr<T>;
  195. return tryGetCUData<std::shared_ptr<T>, UData, CUData>(position, value);
  196. }
  197. template<typename T, typename std::enable_if<detail::IsRegularClass<T>::value && !std::is_const<T>::value, int>::type = 0>
  198. STRONG_INLINE bool tryGet(int position, std::shared_ptr<T> & value)
  199. {
  200. return tryGetUData<std::shared_ptr<T>>(position, value);
  201. }
  202. template<typename U>
  203. bool tryGetUData(int position, U & value)
  204. {
  205. static auto KEY = api::TypeRegistry::get()->getKey<U>();
  206. void * raw = lua_touserdata(L, position);
  207. if(!raw)
  208. return false;
  209. if(lua_getmetatable(L, position) == 0)
  210. return false;
  211. lua_getfield(L, LUA_REGISTRYINDEX, KEY);
  212. if(lua_rawequal(L, -1, -2) == 1)
  213. {
  214. value = *(static_cast<U *>(raw));
  215. lua_pop(L, 2);
  216. return true;
  217. }
  218. lua_pop(L, 2);
  219. return false;
  220. }
  221. template<typename T, typename U, typename CU>
  222. bool tryGetCUData(int position, T & value)
  223. {
  224. static auto KEY = api::TypeRegistry::get()->getKey<U>();
  225. static auto C_KEY = api::TypeRegistry::get()->getKey<CU>();
  226. void * raw = lua_touserdata(L, position);
  227. if(!raw)
  228. return false;
  229. if(lua_getmetatable(L, position) == 0)
  230. return false;
  231. //top is metatable
  232. lua_getfield(L, LUA_REGISTRYINDEX, KEY);
  233. if(lua_rawequal(L, -1, -2) == 1)
  234. {
  235. value = *(static_cast<U *>(raw));
  236. lua_pop(L, 2);
  237. return true;
  238. }
  239. lua_pop(L, 1);
  240. //top is metatable
  241. lua_getfield(L, LUA_REGISTRYINDEX, C_KEY);
  242. if(lua_rawequal(L, -1, -2) == 1)
  243. {
  244. value = *(static_cast<CU *>(raw));
  245. lua_pop(L, 2);
  246. return true;
  247. }
  248. lua_pop(L, 2);
  249. return false;
  250. }
  251. bool tryGet(int position, JsonNode & value);
  252. int retNil();
  253. int retVoid();
  254. STRONG_INLINE
  255. int retPushed()
  256. {
  257. return lua_gettop(L);
  258. }
  259. inline bool isFunction(int position)
  260. {
  261. return lua_isfunction(L, position);
  262. }
  263. inline bool isNumber(int position)
  264. {
  265. return lua_isnumber(L, position);
  266. }
  267. static int quickRetBool(lua_State * L, bool value)
  268. {
  269. lua_settop(L, 0);
  270. lua_pushboolean(L, value);
  271. return 1;
  272. }
  273. template<typename T>
  274. static int quickRetInt(lua_State * L, const T & value)
  275. {
  276. lua_settop(L, 0);
  277. lua_pushinteger(L, static_cast<int32_t>(value));
  278. return 1;
  279. }
  280. static int quickRetStr(lua_State * L, const std::string & value)
  281. {
  282. lua_settop(L, 0);
  283. lua_pushlstring(L, value.c_str(), value.size());
  284. return 1;
  285. }
  286. private:
  287. lua_State * L;
  288. int initialTop;
  289. };
  290. }
  291. VCMI_LIB_NAMESPACE_END