types.hpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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
  156. template <typename I, typename B>
  157. struct NumericValueBase {
  158. static inline
  159. I read(State* state, int index) {
  160. return static_cast<I>(NumericTransportValue<B>::read(state, index));
  161. }
  162. static inline
  163. size_t push(State* state, I value) {
  164. NumericTransportValue<B>::push(state, static_cast<B>(value));
  165. return 1;
  166. }
  167. };
  168. }
  169. /**
  170. * Define an integral type which will be transported via `base`.
  171. */
  172. #define LUWRA_DEF_NUMERIC(base, type) \
  173. template <> \
  174. struct Value<type>: internal::NumericValueBase<type, base> {};
  175. // Lua-dependent types
  176. LUWRA_DEF_NUMERIC(Number, float)
  177. LUWRA_DEF_NUMERIC(Number, double)
  178. LUWRA_DEF_NUMERIC(Number, long double)
  179. // Integral types
  180. LUWRA_DEF_NUMERIC(Integer, signed char)
  181. LUWRA_DEF_NUMERIC(Integer, unsigned char)
  182. LUWRA_DEF_NUMERIC(Integer, signed short)
  183. LUWRA_DEF_NUMERIC(Integer, unsigned short)
  184. LUWRA_DEF_NUMERIC(Integer, signed int)
  185. LUWRA_DEF_NUMERIC(Integer, unsigned int)
  186. LUWRA_DEF_NUMERIC(Integer, signed long int)
  187. LUWRA_DEF_NUMERIC(Integer, unsigned long int)
  188. LUWRA_DEF_NUMERIC(Integer, signed long long int)
  189. LUWRA_DEF_NUMERIC(Integer, unsigned long long int)
  190. // C/C++ types
  191. LUWRA_DEF_VALUE(bool, luaL_checkboolean, lua_pushboolean);
  192. LUWRA_DEF_VALUE(const char*, luaL_checkstring, lua_pushstring);
  193. LUWRA_DEF_VALUE(std::string, luaL_checkstring, luaL_pushstdstring);
  194. // Do not export these macros
  195. #undef LUWRA_DEF_VALUE
  196. #undef LUWRA_DEF_NUMERIC
  197. // Alias for string literals
  198. template <size_t n>
  199. struct Value<char[n]>: Value<const char*> {};
  200. // Alias for const string literals
  201. template <size_t n>
  202. struct Value<const char[n]>: Value<const char*> {};
  203. /**
  204. * C Functions may be pushed aswell.
  205. */
  206. template <>
  207. struct Value<CFunction> {
  208. static inline
  209. size_t push(State* state, CFunction fun) {
  210. lua_pushcfunction(state, fun);
  211. return 1;
  212. }
  213. };
  214. /**
  215. * An arbitrary value on an execution stack.
  216. * Note: this value is only available as long as it exists on its originating stack.
  217. */
  218. struct Arbitrary {
  219. /**
  220. * Originating Lua state
  221. */
  222. State* state;
  223. /**
  224. * Stack index
  225. */
  226. int index;
  227. };
  228. /**
  229. * See [Arbitrary](@ref Arbitrary).
  230. */
  231. template <>
  232. struct Value<Arbitrary> {
  233. static inline
  234. Arbitrary read(State* state, int index) {
  235. if (index < 0)
  236. index = lua_gettop(state) + (index + 1);
  237. return Arbitrary {state, index};
  238. }
  239. static inline
  240. size_t push(State* state, const Arbitrary& value) {
  241. lua_pushvalue(value.state, value.index);
  242. if (value.state != state)
  243. lua_xmove(value.state, state, 1);
  244. return 1;
  245. }
  246. };
  247. namespace internal {
  248. template <typename>
  249. struct StackPusher;
  250. template <size_t I>
  251. struct StackPusher<IndexSequence<I>> {
  252. template <typename... T> static inline
  253. size_t push(State* state, const std::tuple<T...>& package) {
  254. using R = typename std::tuple_element<I, std::tuple<T...>>::type;
  255. return Value<R>::push(state, std::get<I>(package));
  256. }
  257. };
  258. template <size_t I, size_t... Is>
  259. struct StackPusher<IndexSequence<I, Is...>> {
  260. template <typename... T> static inline
  261. size_t push(State* state, const std::tuple<T...>& package) {
  262. return
  263. StackPusher<IndexSequence<I>>::push(state, package)
  264. + StackPusher<IndexSequence<Is...>>::push(state, package);
  265. }
  266. };
  267. }
  268. /**
  269. * Allows you to use multiple return values.
  270. */
  271. template <typename... A>
  272. struct Value<std::tuple<A...>> {
  273. static inline
  274. size_t push(State* state, const std::tuple<A...>& value) {
  275. using Seq = internal::MakeIndexSequence<sizeof...(A)>;
  276. return internal::StackPusher<Seq>::push(state, value);
  277. }
  278. };
  279. /**
  280. * Fix specialization for const types.
  281. */
  282. template <typename T>
  283. struct Value<const T>: Value<T> {};
  284. /**
  285. * Fix specialization for volatile types.
  286. */
  287. template <typename T>
  288. struct Value<volatile T>: Value<T> {};
  289. namespace internal {
  290. struct PushableI {
  291. virtual
  292. size_t push(State* state) const = 0;
  293. virtual
  294. PushableI* copy() const = 0;
  295. virtual
  296. ~PushableI() {}
  297. };
  298. template <typename T>
  299. struct PushableT: virtual PushableI {
  300. T value;
  301. inline
  302. PushableT(T value): value(value) {}
  303. virtual
  304. size_t push(State* state) const {
  305. return Value<T>::push(state, value);
  306. }
  307. virtual
  308. PushableI* copy() const {
  309. return new PushableT<T>(value);
  310. }
  311. };
  312. }
  313. /**
  314. * A value which may be pushed onto the stack.
  315. */
  316. struct Pushable: virtual internal::PushableI {
  317. internal::PushableI* interface;
  318. template <typename T> inline
  319. Pushable(T value): interface(new internal::PushableT<T>(value)) {}
  320. inline
  321. Pushable(Pushable&& other): interface(other.interface) {
  322. other.interface = nullptr;
  323. }
  324. Pushable(const Pushable& other): interface(other.interface->copy()) {}
  325. virtual
  326. size_t push(State* state) const {
  327. return interface->push(state);
  328. }
  329. virtual
  330. internal::PushableI* copy() const {
  331. return new Pushable(*this);
  332. }
  333. virtual
  334. ~Pushable() {
  335. if (interface)
  336. delete interface;
  337. }
  338. };
  339. template <>
  340. struct Value<Pushable> {
  341. static inline
  342. size_t push(State* state, const Pushable& value) {
  343. return value.push(state);
  344. }
  345. };
  346. LUWRA_NS_END
  347. #endif