types.hpp 12 KB

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