types.hpp 11 KB

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