types.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <limits>
  15. LUWRA_NS_BEGIN
  16. /* Lua types */
  17. using Integer = lua_Integer;
  18. using Number = lua_Number;
  19. using State = lua_State;
  20. using CFunction = lua_CFunction;
  21. /**
  22. * User type
  23. */
  24. template <typename T>
  25. struct Value {
  26. /**
  27. * Copy a user type value from the stack.
  28. * \param state Lua state
  29. * \param index Position of the value
  30. */
  31. static
  32. T read(State* state, int index) {
  33. return Value<T&>::read(state, index);
  34. }
  35. /**
  36. * Copy a user type value onto the stack.
  37. * \param state Lua state
  38. * \param value Value you want to push
  39. * \returns Number of values pushed
  40. */
  41. static
  42. size_t push(State* state, const T& value) {
  43. return Value<T&>::push(state, value);
  44. }
  45. };
  46. // Nil
  47. template <>
  48. struct Value<std::nullptr_t> {
  49. static inline
  50. std::nullptr_t read(State* state, int n) {
  51. luaL_checktype(state, n, LUA_TNIL);
  52. return nullptr;
  53. }
  54. static inline
  55. size_t push(State* state, std::nullptr_t) {
  56. lua_pushnil(state);
  57. return 1;
  58. }
  59. };
  60. /**
  61. * Convenient wrapped for [Value<T>::push](@ref Value<T>::push).
  62. */
  63. template <typename T> static inline
  64. size_t push(State* state, T value) {
  65. return Value<T>::push(state, value);
  66. }
  67. /**
  68. * Allows you to push multiple values at once.
  69. */
  70. template <typename T1, typename T2, typename... TR>
  71. size_t push(State* state, T1 value, T2&& head, TR&&... rest) {
  72. return push(state, value) + push(state, std::forward<T2>(head), std::forward<TR>(rest)...);
  73. }
  74. /**
  75. * Convenient wrapper for [Value<T>::read](@ref Value<T>::read).
  76. */
  77. template <typename T> static inline
  78. T read(State* state, int index) {
  79. return Value<T>::read(state, index);
  80. }
  81. /**
  82. * Define a template specialization of `Value` for `type` with a `retrf(State*, int)` which
  83. * extracts it from the stack and a `pushf(State*, type)` which pushes the value on the stack again.
  84. * This assumes that only one value will be pushed onto the stack.
  85. */
  86. #define LUWRA_DEF_VALUE(type, retrf, pushf) \
  87. template <> \
  88. struct Value<type> { \
  89. static inline \
  90. type read(State* state, int n) { \
  91. return retrf(state, n); \
  92. } \
  93. \
  94. static inline \
  95. size_t push(State* state, type value) { \
  96. pushf(state, value); \
  97. return 1; \
  98. } \
  99. }
  100. #ifndef luaL_checkboolean
  101. /**
  102. * Check if the value at index `n` is a boolean and retrieve its value.
  103. */
  104. #define luaL_checkboolean(state, n) \
  105. (luaL_checktype(state, n, LUA_TBOOLEAN), lua_toboolean(state, n))
  106. #endif
  107. #ifndef luaL_checkcfunction
  108. /**
  109. * Check if the value at index `n` is a C function and retrieve it.
  110. */
  111. #define luaL_checkcfunction(state, n) \
  112. (luaL_checktype(state, n, LUA_TCFUNCTION), lua_tocfunction(state, n))
  113. #endif
  114. #ifndef luaL_pushstdstring
  115. /**
  116. * push a `std::string` as string onto the stack.
  117. */
  118. #define luaL_pushstdstring(state, stdstring) \
  119. (lua_pushstring(state, (stdstring).c_str()))
  120. #endif
  121. namespace internal {
  122. template <typename T>
  123. struct NumericTransportValue {
  124. static_assert(
  125. sizeof(T) == -1,
  126. "Parameter to NumericTransportValue is not a numeric base type"
  127. );
  128. };
  129. // Transport unit `Integer`
  130. template <>
  131. struct NumericTransportValue<Integer> {
  132. static inline
  133. Integer read(State* state, int index) {
  134. return luaL_checkinteger(state, index);
  135. }
  136. static inline
  137. size_t push(State* state, Integer value) {
  138. lua_pushinteger(state, value);
  139. return 1;
  140. }
  141. };
  142. // Transport unit `Number`
  143. template <>
  144. struct NumericTransportValue<Number> {
  145. static inline
  146. Number read(State* state, int index) {
  147. return luaL_checknumber(state, index);
  148. }
  149. static inline
  150. size_t push(State* state, Number value) {
  151. lua_pushnumber(state, value);
  152. return 1;
  153. }
  154. };
  155. // Base for `Value<I>` specializations which uses `B` as transport unit, where `I` is smaller
  156. // than `B`.
  157. template <typename I, typename B>
  158. struct NumericContainedValueBase {
  159. static constexpr
  160. bool qualifies =
  161. // TODO: Remove warning about comparsion between signed and unsigned integers
  162. std::numeric_limits<I>::max() <= std::numeric_limits<B>::max()
  163. && std::numeric_limits<I>::lowest() >= std::numeric_limits<B>::lowest();
  164. static inline
  165. I read(State* state, int index) {
  166. return
  167. static_cast<I>(NumericTransportValue<B>::read(state, index));
  168. }
  169. static inline
  170. size_t push(State* state, I value) {
  171. NumericTransportValue<B>::push(state, static_cast<B>(value));
  172. return 1;
  173. }
  174. };
  175. // Base for `Value<I>` specializations which uses `B` as transport unit, where `I` is bigger
  176. // than `B`.
  177. template <typename I, typename B>
  178. struct NumericTruncatingValueBase {
  179. static inline
  180. I read(State* state, int index) {
  181. return static_cast<I>(NumericTransportValue<B>::read(state, index));
  182. }
  183. static inline
  184. size_t push(State*, I) {
  185. static_assert(
  186. sizeof(I) == -1,
  187. "You must not use 'Value<I>::push' specializations which inherit from NumericTruncatingValueBase"
  188. );
  189. return -1;
  190. }
  191. };
  192. // Base for `Value<I>` specializations which uses `B` as transport unit
  193. template <typename I, typename B>
  194. using NumericValueBase =
  195. typename std::conditional<
  196. std::is_same<I, B>::value,
  197. NumericTransportValue<B>,
  198. typename std::conditional<
  199. NumericContainedValueBase<I, B>::qualifies,
  200. NumericContainedValueBase<I, B>,
  201. NumericTruncatingValueBase<I, B>
  202. >::type
  203. >::type;
  204. }
  205. /**
  206. * Define an integral type which will be transported via `base`.
  207. */
  208. #define LUWRA_DEF_NUMERIC(base, type) \
  209. template <> \
  210. struct Value<type>: internal::NumericValueBase<type, base> {};
  211. // Lua-dependent types
  212. LUWRA_DEF_NUMERIC(Number, float)
  213. LUWRA_DEF_NUMERIC(Number, double)
  214. LUWRA_DEF_NUMERIC(Number, long double)
  215. // Integral types
  216. LUWRA_DEF_NUMERIC(Integer, signed char)
  217. LUWRA_DEF_NUMERIC(Integer, unsigned char)
  218. LUWRA_DEF_NUMERIC(Integer, signed short)
  219. LUWRA_DEF_NUMERIC(Integer, unsigned short)
  220. LUWRA_DEF_NUMERIC(Integer, signed int)
  221. LUWRA_DEF_NUMERIC(Integer, unsigned int)
  222. LUWRA_DEF_NUMERIC(Integer, signed long int)
  223. LUWRA_DEF_NUMERIC(Integer, unsigned long int)
  224. LUWRA_DEF_NUMERIC(Integer, signed long long int)
  225. LUWRA_DEF_NUMERIC(Integer, unsigned long long int)
  226. // C/C++ types
  227. LUWRA_DEF_VALUE(bool, luaL_checkboolean, lua_pushboolean);
  228. LUWRA_DEF_VALUE(const char*, luaL_checkstring, lua_pushstring);
  229. LUWRA_DEF_VALUE(std::string, luaL_checkstring, luaL_pushstdstring);
  230. // Do not export these macros
  231. #undef LUWRA_DEF_VALUE
  232. #undef LUWRA_DEF_NUMERIC
  233. // Alias for string literals
  234. template <size_t n>
  235. struct Value<char[n]>: Value<const char*> {};
  236. // Alias for const string literals
  237. template <size_t n>
  238. struct Value<const char[n]>: Value<const char*> {};
  239. /**
  240. * C Functions may be pushed aswell.
  241. */
  242. template <>
  243. struct Value<CFunction> {
  244. static inline
  245. size_t push(State* state, CFunction fun) {
  246. lua_pushcfunction(state, fun);
  247. return 1;
  248. }
  249. };
  250. /**
  251. * An arbitrary value on an execution stack.
  252. * Note: this value is only available as long as it exists on its originating stack.
  253. */
  254. struct Arbitrary {
  255. /**
  256. * Originating Lua state
  257. */
  258. State* state;
  259. /**
  260. * Stack index
  261. */
  262. int index;
  263. };
  264. /**
  265. * See [Arbitrary](@ref Arbitrary).
  266. */
  267. template <>
  268. struct Value<Arbitrary> {
  269. static inline
  270. Arbitrary read(State* state, int index) {
  271. if (index < 0)
  272. index = lua_gettop(state) + (index + 1);
  273. return Arbitrary {state, index};
  274. }
  275. static inline
  276. size_t push(State* state, const Arbitrary& value) {
  277. lua_pushvalue(value.state, value.index);
  278. if (value.state != state)
  279. lua_xmove(value.state, state, 1);
  280. return 1;
  281. }
  282. };
  283. namespace internal {
  284. template <typename>
  285. struct StackPusher;
  286. template <size_t I>
  287. struct StackPusher<IndexSequence<I>> {
  288. template <typename... T> static inline
  289. size_t push(State* state, const std::tuple<T...>& package) {
  290. using R = typename std::tuple_element<I, std::tuple<T...>>::type;
  291. return Value<R>::push(state, std::get<I>(package));
  292. }
  293. };
  294. template <size_t I, size_t... Is>
  295. struct StackPusher<IndexSequence<I, Is...>> {
  296. template <typename... T> static inline
  297. size_t push(State* state, const std::tuple<T...>& package) {
  298. return
  299. StackPusher<IndexSequence<I>>::push(state, package)
  300. + StackPusher<IndexSequence<Is...>>::push(state, package);
  301. }
  302. };
  303. }
  304. /**
  305. * Allows you to use multiple return values.
  306. */
  307. template <typename... A>
  308. struct Value<std::tuple<A...>> {
  309. static inline
  310. size_t push(State* state, const std::tuple<A...>& value) {
  311. using Seq = internal::MakeIndexSequence<sizeof...(A)>;
  312. return internal::StackPusher<Seq>::push(state, value);
  313. }
  314. };
  315. /**
  316. * Fix specialization for const types.
  317. */
  318. template <typename T>
  319. struct Value<const T>: Value<T> {};
  320. /**
  321. * Fix specialization for volatile types.
  322. */
  323. template <typename T>
  324. struct Value<volatile T>: Value<T> {};
  325. namespace internal {
  326. struct PushableI {
  327. virtual
  328. size_t push(State* state) const = 0;
  329. virtual
  330. PushableI* copy() const = 0;
  331. virtual
  332. ~PushableI() {}
  333. };
  334. template <typename T>
  335. struct PushableT: virtual PushableI {
  336. T value;
  337. inline
  338. PushableT(T value): value(value) {}
  339. virtual
  340. size_t push(State* state) const {
  341. return Value<T>::push(state, value);
  342. }
  343. virtual
  344. PushableI* copy() const {
  345. return new PushableT<T>(value);
  346. }
  347. };
  348. }
  349. /**
  350. * A value which may be pushed onto the stack.
  351. */
  352. struct Pushable: virtual internal::PushableI {
  353. internal::PushableI* interface;
  354. template <typename T> inline
  355. Pushable(T value): interface(new internal::PushableT<T>(value)) {}
  356. inline
  357. Pushable(Pushable&& other): interface(other.interface) {
  358. other.interface = nullptr;
  359. }
  360. Pushable(const Pushable& other): interface(other.interface->copy()) {}
  361. virtual
  362. size_t push(State* state) const {
  363. return interface->push(state);
  364. }
  365. virtual
  366. internal::PushableI* copy() const {
  367. return new Pushable(*this);
  368. }
  369. virtual
  370. ~Pushable() {
  371. if (interface)
  372. delete interface;
  373. }
  374. };
  375. template <>
  376. struct Value<Pushable> {
  377. static inline
  378. size_t push(State* state, const Pushable& value) {
  379. return value.push(state);
  380. }
  381. };
  382. LUWRA_NS_END
  383. #endif