| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- /* Luwra
- * Minimal-overhead Lua wrapper for C++
- *
- * Copyright (C) 2015, Ole Krüger <ole@vprsm.de>
- */
- #ifndef LUWRA_TYPES_H_
- #define LUWRA_TYPES_H_
- #include "common.hpp"
- #include "compat.hpp"
- #include <utility>
- #include <tuple>
- #include <string>
- #include <type_traits>
- #include <limits>
- LUWRA_NS_BEGIN
- /* Lua types */
- using Integer = lua_Integer;
- using Number = lua_Number;
- using State = lua_State;
- using CFunction = lua_CFunction;
- /**
- * User type
- */
- template <typename T>
- struct Value {
- /**
- * Copy a user type value from the stack.
- * \param state Lua state
- * \param index Position of the value
- */
- static
- T read(State* state, int index) {
- return Value<T&>::read(state, index);
- }
- /**
- * Copy a user type value onto the stack.
- * \param state Lua state
- * \param value Value you want to push
- * \returns Number of values pushed
- */
- static
- size_t push(State* state, const T& value) {
- return Value<T&>::push(state, value);
- }
- };
- // Nil
- template <>
- struct Value<std::nullptr_t> {
- static inline
- std::nullptr_t read(State* state, int n) {
- luaL_checktype(state, n, LUA_TNIL);
- return nullptr;
- }
- static inline
- size_t push(State* state, std::nullptr_t) {
- lua_pushnil(state);
- return 1;
- }
- };
- /**
- * This does nothing.
- */
- static inline
- size_t push(State*) {
- return 0;
- }
- /**
- * Convenient wrapped for [Value<T>::push](@ref Value<T>::push).
- */
- template <typename T> static inline
- size_t push(State* state, T value) {
- return Value<T>::push(state, value);
- }
- /**
- * Allows you to push multiple values at once.
- */
- template <typename T1, typename T2, typename... TR>
- size_t push(State* state, T1 value, T2&& head, TR&&... rest) {
- return push(state, value) + push(state, std::forward<T2>(head), std::forward<TR>(rest)...);
- }
- /**
- * Convenient wrapper for [Value<T>::read](@ref Value<T>::read).
- */
- template <typename T> static inline
- T read(State* state, int index) {
- return Value<T>::read(state, index);
- }
- /**
- * Define a template specialization of `Value` for `type` with a `retrf(State*, int)` which
- * extracts it from the stack and a `pushf(State*, type)` which pushes the value on the stack again.
- * This assumes that only one value will be pushed onto the stack.
- */
- #define LUWRA_DEF_VALUE(type, retrf, pushf) \
- template <> \
- struct Value<type> { \
- static inline \
- type read(State* state, int n) { \
- return retrf(state, n); \
- } \
- \
- static inline \
- size_t push(State* state, type value) { \
- pushf(state, value); \
- return 1; \
- } \
- }
- #ifndef luaL_checkboolean
- /**
- * Check if the value at index `n` is a boolean and retrieve its value.
- */
- #define luaL_checkboolean(state, n) \
- (luaL_checktype(state, n, LUA_TBOOLEAN), lua_toboolean(state, n))
- #endif
- #ifndef luaL_checkcfunction
- /**
- * Check if the value at index `n` is a C function and retrieve it.
- */
- #define luaL_checkcfunction(state, n) \
- (luaL_checktype(state, n, LUA_TCFUNCTION), lua_tocfunction(state, n))
- #endif
- #ifndef luaL_pushstdstring
- /**
- * push a `std::string` as string onto the stack.
- */
- #define luaL_pushstdstring(state, stdstring) \
- (lua_pushstring(state, (stdstring).c_str()))
- #endif
- namespace internal {
- template <typename T>
- struct NumericTransportValue {
- static_assert(
- sizeof(T) == -1,
- "Parameter to NumericTransportValue is not a numeric base type"
- );
- };
- // Transport unit `Integer`
- template <>
- struct NumericTransportValue<Integer> {
- static inline
- Integer read(State* state, int index) {
- return luaL_checkinteger(state, index);
- }
- static inline
- size_t push(State* state, Integer value) {
- lua_pushinteger(state, value);
- return 1;
- }
- };
- // Transport unit `Number`
- template <>
- struct NumericTransportValue<Number> {
- static inline
- Number read(State* state, int index) {
- return luaL_checknumber(state, index);
- }
- static inline
- size_t push(State* state, Number value) {
- lua_pushnumber(state, value);
- return 1;
- }
- };
- // Base for `Value<I>` specializations which uses `B` as transport unit, where `I` is smaller
- // than `B`.
- template <typename I, typename B>
- struct NumericContainedValueBase {
- static constexpr
- bool qualifies =
- // TODO: Remove warning about comparsion between signed and unsigned integers
- std::numeric_limits<I>::max() <= std::numeric_limits<B>::max()
- && std::numeric_limits<I>::lowest() >= std::numeric_limits<B>::lowest();
- static inline
- I read(State* state, int index) {
- return
- static_cast<I>(NumericTransportValue<B>::read(state, index));
- }
- static inline
- size_t push(State* state, I value) {
- NumericTransportValue<B>::push(state, static_cast<B>(value));
- return 1;
- }
- };
- // Base for `Value<I>` specializations which uses `B` as transport unit, where `I` is bigger
- // than `B`.
- template <typename I, typename B>
- struct NumericTruncatingValueBase {
- static inline
- I read(State* state, int index) {
- return static_cast<I>(NumericTransportValue<B>::read(state, index));
- }
- static inline
- size_t push(State*, I) {
- static_assert(
- sizeof(I) == -1,
- "You must not use 'Value<I>::push' specializations which inherit from NumericTruncatingValueBase"
- );
- return -1;
- }
- };
- // Base for `Value<I>` specializations which uses `B` as transport unit
- template <typename I, typename B>
- using NumericValueBase =
- typename std::conditional<
- std::is_same<I, B>::value,
- NumericTransportValue<B>,
- typename std::conditional<
- NumericContainedValueBase<I, B>::qualifies,
- NumericContainedValueBase<I, B>,
- NumericTruncatingValueBase<I, B>
- >::type
- >::type;
- }
- /**
- * Define an integral type which will be transported via `base`.
- */
- #define LUWRA_DEF_NUMERIC(base, type) \
- template <> \
- struct Value<type>: internal::NumericValueBase<type, base> {};
- // Lua-dependent types
- LUWRA_DEF_NUMERIC(Number, float)
- LUWRA_DEF_NUMERIC(Number, double)
- LUWRA_DEF_NUMERIC(Number, long double)
- // Integral types
- LUWRA_DEF_NUMERIC(Integer, signed char)
- LUWRA_DEF_NUMERIC(Integer, unsigned char)
- LUWRA_DEF_NUMERIC(Integer, signed short)
- LUWRA_DEF_NUMERIC(Integer, unsigned short)
- LUWRA_DEF_NUMERIC(Integer, signed int)
- LUWRA_DEF_NUMERIC(Integer, unsigned int)
- LUWRA_DEF_NUMERIC(Integer, signed long int)
- LUWRA_DEF_NUMERIC(Integer, unsigned long int)
- LUWRA_DEF_NUMERIC(Integer, signed long long int)
- LUWRA_DEF_NUMERIC(Integer, unsigned long long int)
- // C/C++ types
- LUWRA_DEF_VALUE(bool, luaL_checkboolean, lua_pushboolean);
- LUWRA_DEF_VALUE(const char*, luaL_checkstring, lua_pushstring);
- LUWRA_DEF_VALUE(std::string, luaL_checkstring, luaL_pushstdstring);
- // Do not export these macros
- #undef LUWRA_DEF_VALUE
- #undef LUWRA_DEF_NUMERIC
- // Alias for string literals
- template <size_t n>
- struct Value<char[n]>: Value<const char*> {};
- // Alias for const string literals
- template <size_t n>
- struct Value<const char[n]>: Value<const char*> {};
- /**
- * C Functions may be pushed aswell.
- */
- template <>
- struct Value<CFunction> {
- static inline
- size_t push(State* state, CFunction fun) {
- lua_pushcfunction(state, fun);
- return 1;
- }
- };
- /**
- * An arbitrary value on an execution stack.
- * Note: this value is only available as long as it exists on its originating stack.
- */
- struct Arbitrary {
- /**
- * Originating Lua state
- */
- State* state;
- /**
- * Stack index
- */
- int index;
- };
- /**
- * See [Arbitrary](@ref Arbitrary).
- */
- template <>
- struct Value<Arbitrary> {
- static inline
- Arbitrary read(State* state, int index) {
- if (index < 0)
- index = lua_gettop(state) + (index + 1);
- return Arbitrary {state, index};
- }
- static inline
- size_t push(State* state, const Arbitrary& value) {
- lua_pushvalue(value.state, value.index);
- if (value.state != state)
- lua_xmove(value.state, state, 1);
- return 1;
- }
- };
- namespace internal {
- template <typename>
- struct StackPusher;
- template <size_t I>
- struct StackPusher<IndexSequence<I>> {
- template <typename... T> static inline
- size_t push(State* state, const std::tuple<T...>& package) {
- using R = typename std::tuple_element<I, std::tuple<T...>>::type;
- return Value<R>::push(state, std::get<I>(package));
- }
- };
- template <size_t I, size_t... Is>
- struct StackPusher<IndexSequence<I, Is...>> {
- template <typename... T> static inline
- size_t push(State* state, const std::tuple<T...>& package) {
- return
- StackPusher<IndexSequence<I>>::push(state, package)
- + StackPusher<IndexSequence<Is...>>::push(state, package);
- }
- };
- }
- /**
- * Allows you to use multiple return values.
- */
- template <typename... A>
- struct Value<std::tuple<A...>> {
- static inline
- size_t push(State* state, const std::tuple<A...>& value) {
- using Seq = internal::MakeIndexSequence<sizeof...(A)>;
- return internal::StackPusher<Seq>::push(state, value);
- }
- };
- /**
- * Fix specialization for const types.
- */
- template <typename T>
- struct Value<const T>: Value<T> {};
- /**
- * Fix specialization for volatile types.
- */
- template <typename T>
- struct Value<volatile T>: Value<T> {};
- namespace internal {
- struct PushableI {
- virtual
- size_t push(State* state) const = 0;
- virtual
- PushableI* copy() const = 0;
- virtual
- ~PushableI() {}
- };
- template <typename T>
- struct PushableT: virtual PushableI {
- T value;
- inline
- PushableT(T value): value(value) {}
- virtual
- size_t push(State* state) const {
- return Value<T>::push(state, value);
- }
- virtual
- PushableI* copy() const {
- return new PushableT<T>(value);
- }
- };
- }
- /**
- * A value which may be pushed onto the stack.
- */
- struct Pushable: virtual internal::PushableI {
- internal::PushableI* interface;
- template <typename T> inline
- Pushable(T value): interface(new internal::PushableT<T>(value)) {}
- inline
- Pushable(Pushable&& other): interface(other.interface) {
- other.interface = nullptr;
- }
- Pushable(const Pushable& other): interface(other.interface->copy()) {}
- virtual
- size_t push(State* state) const {
- return interface->push(state);
- }
- virtual
- internal::PushableI* copy() const {
- return new Pushable(*this);
- }
- virtual
- ~Pushable() {
- if (interface)
- delete interface;
- }
- };
- template <>
- struct Value<Pushable> {
- static inline
- size_t push(State* state, const Pushable& value) {
- return value.push(state);
- }
- };
- LUWRA_NS_END
- #endif
|