types.hpp 9.6 KB

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