types.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /* Luwra
  2. * Minimal-overhead Lua wrapper for C++
  3. *
  4. * Copyright (C) 2015, Ole Krüger <ole@vprsm.de>
  5. */
  6. #ifndef LUWRA_TYPES_H_
  7. #define LUWRA_TYPES_H_
  8. #include "common.hpp"
  9. #include "compat.hpp"
  10. #include <utility>
  11. #include <tuple>
  12. #include <string>
  13. #include <type_traits>
  14. #include <memory>
  15. #include <limits>
  16. #include <vector>
  17. #include <list>
  18. #include <initializer_list>
  19. #include <map>
  20. LUWRA_NS_BEGIN
  21. /* Lua types */
  22. using Integer = lua_Integer;
  23. using Number = lua_Number;
  24. using State = lua_State;
  25. using CFunction = lua_CFunction;
  26. /**
  27. * User type
  28. */
  29. template <typename T>
  30. struct Value;
  31. // Nil
  32. template <>
  33. struct Value<std::nullptr_t> {
  34. static inline
  35. std::nullptr_t read(State* state, int n) {
  36. luaL_checktype(state, n, LUA_TNIL);
  37. return nullptr;
  38. }
  39. static inline
  40. size_t push(State* state, std::nullptr_t) {
  41. lua_pushnil(state);
  42. return 1;
  43. }
  44. };
  45. /**
  46. * Convenient wrapped for [Value<T>::push](@ref Value<T>::push).
  47. */
  48. template <typename T> static inline
  49. size_t push(State* state, T&& value) {
  50. using U = typename std::remove_cv<typename std::remove_reference<T>::type>::type;
  51. return Value<U>::push(state, std::forward<T>(value));
  52. }
  53. /**
  54. * Allows you to push multiple values at once.
  55. */
  56. template <typename T1, typename T2, typename... TR>
  57. size_t push(State* state, T1&& value, T2&& head, TR&&... rest) {
  58. return
  59. push(state, std::forward<T1>(value)) +
  60. push(state, std::forward<T2>(head), std::forward<TR>(rest)...);
  61. }
  62. /**
  63. * Convenient wrapper for [Value<T>::read](@ref Value<T>::read).
  64. */
  65. template <typename T> static inline
  66. T read(State* state, int index) {
  67. return Value<T>::read(state, index);
  68. }
  69. /**
  70. * Define a template specialization of `Value` for `type` with a `retrf(State*, int)` which
  71. * extracts it from the stack and a `pushf(State*, type)` which pushes the value on the stack again.
  72. * This assumes that only one value will be pushed onto the stack.
  73. */
  74. #define LUWRA_DEF_VALUE(type, retrf, pushf) \
  75. template <> \
  76. struct Value<type> { \
  77. static inline \
  78. type read(State* state, int n) { \
  79. return static_cast<type>(retrf(state, n)); \
  80. } \
  81. \
  82. static inline \
  83. size_t push(State* state, type value) { \
  84. pushf(state, value); \
  85. return 1; \
  86. } \
  87. }
  88. #ifndef luaL_checkboolean
  89. /**
  90. * Check if the value at index `n` is a boolean and retrieve its value.
  91. */
  92. #define luaL_checkboolean(state, n) \
  93. (luaL_checktype(state, n, LUA_TBOOLEAN), lua_toboolean(state, n))
  94. #endif
  95. #ifndef luaL_checkcfunction
  96. /**
  97. * Check if the value at index `n` is a C function and retrieve it.
  98. */
  99. #define luaL_checkcfunction(state, n) \
  100. (luaL_checktype(state, n, LUA_TCFUNCTION), lua_tocfunction(state, n))
  101. #endif
  102. #ifndef luaL_pushstdstring
  103. /**
  104. * push a `std::string` as string onto the stack.
  105. */
  106. #define luaL_pushstdstring(state, stdstring) \
  107. (lua_pushstring(state, (stdstring).c_str()))
  108. #endif
  109. namespace internal {
  110. template <typename T>
  111. struct NumericTransportValue {
  112. static_assert(
  113. sizeof(T) == -1,
  114. "Parameter to NumericTransportValue is not a numeric base type"
  115. );
  116. };
  117. // Transport unit `Integer`
  118. template <>
  119. struct NumericTransportValue<Integer> {
  120. static inline
  121. Integer read(State* state, int index) {
  122. return luaL_checkinteger(state, index);
  123. }
  124. static inline
  125. size_t push(State* state, Integer value) {
  126. lua_pushinteger(state, value);
  127. return 1;
  128. }
  129. };
  130. // Transport unit `Number`
  131. template <>
  132. struct NumericTransportValue<Number> {
  133. static inline
  134. Number read(State* state, int index) {
  135. return luaL_checknumber(state, index);
  136. }
  137. static inline
  138. size_t push(State* state, Number value) {
  139. lua_pushnumber(state, value);
  140. return 1;
  141. }
  142. };
  143. // Base for `Value<I>` specializations which uses `B` as transport unit
  144. template <typename I, typename B>
  145. struct NumericValueBase {
  146. static inline
  147. I read(State* state, int index) {
  148. return static_cast<I>(NumericTransportValue<B>::read(state, index));
  149. }
  150. static inline
  151. size_t push(State* state, I value) {
  152. NumericTransportValue<B>::push(state, static_cast<B>(value));
  153. return 1;
  154. }
  155. };
  156. }
  157. /**
  158. * Define an integral type which will be transported via `base`.
  159. */
  160. #define LUWRA_DEF_NUMERIC(base, type) \
  161. template <> \
  162. struct Value<type>: internal::NumericValueBase<type, base> {};
  163. // Lua-dependent types
  164. LUWRA_DEF_NUMERIC(Number, float)
  165. LUWRA_DEF_NUMERIC(Number, double)
  166. LUWRA_DEF_NUMERIC(Number, long double)
  167. // Integral types
  168. LUWRA_DEF_NUMERIC(Integer, signed char)
  169. LUWRA_DEF_NUMERIC(Integer, unsigned char)
  170. LUWRA_DEF_NUMERIC(Integer, signed short)
  171. LUWRA_DEF_NUMERIC(Integer, unsigned short)
  172. LUWRA_DEF_NUMERIC(Integer, signed int)
  173. LUWRA_DEF_NUMERIC(Integer, unsigned int)
  174. LUWRA_DEF_NUMERIC(Integer, signed long int)
  175. LUWRA_DEF_NUMERIC(Integer, unsigned long int)
  176. LUWRA_DEF_NUMERIC(Integer, signed long long int)
  177. LUWRA_DEF_NUMERIC(Integer, unsigned long long int)
  178. // C/C++ types
  179. LUWRA_DEF_VALUE(const char*, luaL_checkstring, lua_pushstring);
  180. LUWRA_DEF_VALUE(std::string, luaL_checkstring, luaL_pushstdstring);
  181. // Do not export these macros
  182. #undef LUWRA_DEF_VALUE
  183. #undef LUWRA_DEF_NUMERIC
  184. template <>
  185. struct Value<bool> {
  186. static inline
  187. bool read(State* state, int n) {
  188. return luaL_checkboolean(state, n) == 1;
  189. }
  190. static inline
  191. size_t push(State* state, bool value) {
  192. lua_pushboolean(state, static_cast<int>(value));
  193. return 1;
  194. }
  195. };
  196. // Alias for string literals
  197. template <size_t n>
  198. struct Value<char[n]>: Value<const char*> {};
  199. // Alias for const string literals
  200. template <size_t n>
  201. struct Value<const char[n]>: Value<const char*> {};
  202. /**
  203. * C Functions may be pushed aswell.
  204. */
  205. template <>
  206. struct Value<CFunction> {
  207. static inline
  208. size_t push(State* state, CFunction fun) {
  209. lua_pushcfunction(state, fun);
  210. return 1;
  211. }
  212. };
  213. namespace internal {
  214. // Create reference the value pointed to by `index`. Does not remove the referenced value.
  215. static inline
  216. int referenceValue(State* state, int index) {
  217. lua_pushvalue(state, index);
  218. return luaL_ref(state, LUA_REGISTRYINDEX);
  219. }
  220. // Implementation of a reference which takes care of the lifetime of a Lua reference
  221. struct ReferenceImpl {
  222. State* const state;
  223. const int ref;
  224. bool autoUnref = true;
  225. // Reference a value at an index.
  226. inline
  227. ReferenceImpl(State* state, int indexOrRef, bool isIndex = true):
  228. state(state),
  229. ref(isIndex ? referenceValue(state, indexOrRef) : indexOrRef),
  230. autoUnref(isIndex)
  231. {}
  232. // Reference the value on top of stack.
  233. inline
  234. ReferenceImpl(State* state):
  235. state(state),
  236. ref(luaL_ref(state, LUA_REGISTRYINDEX))
  237. {}
  238. // A (smart) pointer to an instance may be copied and moved, but the instance itself must
  239. // not be copied or moved. This allows us to have only one instance of `ReferenceImpl` per
  240. // Lua reference.
  241. ReferenceImpl(const ReferenceImpl& other) = delete;
  242. ReferenceImpl(ReferenceImpl&& other) = delete;
  243. inline
  244. ~ReferenceImpl() {
  245. if (ref >= 0 && autoUnref) luaL_unref(state, LUA_REGISTRYINDEX, ref);
  246. }
  247. // Small shortcut to make the `push`-implementations for `Table` and `Reference` consistent,
  248. // since both use this struct internally.
  249. inline
  250. size_t push(State* targetState) {
  251. lua_rawgeti(state, LUA_REGISTRYINDEX, ref);
  252. if (state != targetState)
  253. lua_xmove(state, targetState, 1);
  254. return 1;
  255. }
  256. inline
  257. size_t push() {
  258. lua_rawgeti(state, LUA_REGISTRYINDEX, ref);
  259. return 1;
  260. }
  261. };
  262. using SharedReferenceImpl = std::shared_ptr<internal::ReferenceImpl>;
  263. }
  264. /**
  265. * Reference to an arbitrary value.
  266. */
  267. struct Reference {
  268. const internal::SharedReferenceImpl impl;
  269. /**
  270. * Create a reference to the value at the given index.
  271. */
  272. inline
  273. Reference(State* state, int indexOrRef, bool isIndex = true):
  274. impl(std::make_shared<internal::ReferenceImpl>(state, indexOrRef, isIndex))
  275. {}
  276. /**
  277. * Create a reference to the value at the top of the stack.
  278. */
  279. inline
  280. Reference(State* state):
  281. impl(std::make_shared<internal::ReferenceImpl>(state))
  282. {}
  283. /**
  284. * Read the referenced value.
  285. */
  286. template <typename T> inline
  287. T read() const {
  288. size_t pushed = impl->push();
  289. T ret = Value<T>::read(impl->state, -1);
  290. lua_pop(impl->state, pushed);
  291. return ret;
  292. }
  293. /**
  294. * Shortcut for `read<T>()`.
  295. */
  296. template <typename T> inline
  297. operator T() const {
  298. return read<T>();
  299. }
  300. };
  301. /**
  302. * See [Reference](@ref Reference).
  303. */
  304. template <>
  305. struct Value<Reference> {
  306. static inline
  307. Reference read(State* state, int index) {
  308. return {state, index, true};
  309. }
  310. static inline
  311. size_t push(State* state, const Reference& value) {
  312. return value.impl->push(state);
  313. }
  314. };
  315. namespace internal {
  316. template <typename>
  317. struct StackPusher;
  318. template <size_t I>
  319. struct StackPusher<IndexSequence<I>> {
  320. template <typename... T> static inline
  321. size_t push(State* state, const std::tuple<T...>& package) {
  322. return luwra::push(state, std::get<I>(package));
  323. }
  324. };
  325. template <size_t I, size_t... Is>
  326. struct StackPusher<IndexSequence<I, Is...>> {
  327. template <typename... T> static inline
  328. size_t push(State* state, const std::tuple<T...>& package) {
  329. return
  330. StackPusher<IndexSequence<I>>::push(state, package) +
  331. StackPusher<IndexSequence<Is...>>::push(state, package);
  332. }
  333. };
  334. }
  335. /**
  336. * Allows you to use multiple return values.
  337. */
  338. template <typename... A>
  339. struct Value<std::tuple<A...>> {
  340. static inline
  341. size_t push(State* state, const std::tuple<A...>& value) {
  342. using Seq = internal::MakeIndexSequence<sizeof...(A)>;
  343. return internal::StackPusher<Seq>::push(state, value);
  344. }
  345. };
  346. /**
  347. * Fix specialization for const types.
  348. */
  349. template <typename T>
  350. struct Value<const T>: Value<T> {};
  351. /**
  352. * Fix specialization for volatile types.
  353. */
  354. template <typename T>
  355. struct Value<volatile T>: Value<T> {};
  356. namespace internal {
  357. struct PushableI {
  358. virtual
  359. size_t push(State* state) const = 0;
  360. virtual
  361. PushableI* copy() const = 0;
  362. virtual
  363. ~PushableI() {}
  364. };
  365. template <typename T>
  366. struct PushableT: virtual PushableI {
  367. T value;
  368. inline
  369. PushableT(T value): value(value) {}
  370. virtual
  371. size_t push(State* state) const {
  372. return luwra::push(state, value);
  373. }
  374. virtual
  375. PushableI* copy() const {
  376. return new PushableT<T>(value);
  377. }
  378. };
  379. }
  380. /**
  381. * A value which may be pushed onto the stack.
  382. */
  383. struct Pushable: virtual internal::PushableI {
  384. internal::PushableI* interface;
  385. template <typename T> inline
  386. Pushable(T value): interface(new internal::PushableT<T>(value)) {}
  387. inline
  388. Pushable(Pushable&& other): interface(other.interface) {
  389. other.interface = nullptr;
  390. }
  391. Pushable(const Pushable& other): interface(other.interface->copy()) {}
  392. virtual
  393. size_t push(State* state) const {
  394. return interface->push(state);
  395. }
  396. virtual
  397. internal::PushableI* copy() const {
  398. return new Pushable(*this);
  399. }
  400. virtual
  401. ~Pushable() {
  402. if (interface)
  403. delete interface;
  404. }
  405. bool operator <(const Pushable& other) const {
  406. return interface < other.interface;
  407. }
  408. };
  409. template <>
  410. struct Value<Pushable> {
  411. static inline
  412. size_t push(State* state, const Pushable& value) {
  413. return value.push(state);
  414. }
  415. };
  416. template <typename T>
  417. struct Value<std::vector<T>> {
  418. static inline
  419. size_t push(State* state, const std::vector<T>& vec) {
  420. lua_createtable(state, vec.size(), 0);
  421. int size = static_cast<int>(vec.size());
  422. for (int i = 0; i < size; i++) {
  423. size_t pushedValues = luwra::push(state, vec[i]);
  424. if (pushedValues > 1)
  425. lua_pop(state, static_cast<int>(pushedValues - 1));
  426. lua_rawseti(state, -2, i + 1);
  427. }
  428. return 1;
  429. }
  430. };
  431. template <typename T>
  432. struct Value<std::list<T>> {
  433. static inline
  434. size_t push(State* state, const std::list<T>& lst) {
  435. lua_createtable(state, lst.size(), 0);
  436. int i = 0;
  437. for (const T& item: lst) {
  438. size_t pushedValues = luwra::push(state, item);
  439. if (pushedValues > 1)
  440. lua_pop(state, static_cast<int>(pushedValues - 1));
  441. lua_rawseti(state, -2, ++i);
  442. }
  443. return 1;
  444. }
  445. };
  446. template <typename K, typename V>
  447. struct Value<std::map<K, V>> {
  448. static inline
  449. size_t push(State* state, const std::map<K, V>& map) {
  450. lua_createtable(state, 0, map.size());
  451. for (const auto& entry: map) {
  452. size_t pushedKeys = luwra::push(state, entry.first);
  453. if (pushedKeys > 1)
  454. lua_pop(state, static_cast<int>(pushedKeys - 1));
  455. size_t pushedValues = luwra::push(state, entry.second);
  456. if (pushedValues > 1)
  457. lua_pop(state, static_cast<int>(pushedValues - 1));
  458. lua_rawset(state, -3);
  459. }
  460. return 1;
  461. }
  462. };
  463. LUWRA_NS_END
  464. #endif