LuaStack.h 7.3 KB

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