types.hpp 11 KB

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