types.hpp 8.5 KB

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