2013-11-25 17:56:27 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2016-02-27 15:43:53 +08:00
|
|
|
// Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
|
2013-11-25 17:56:27 +08:00
|
|
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
|
|
// this software and associated documentation files (the "Software"), to deal in
|
|
|
|
// the Software without restriction, including without limitation the rights to
|
|
|
|
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
|
|
// the Software, and to permit persons to whom the Software is furnished to do so,
|
|
|
|
// subject to the following conditions:
|
|
|
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
|
|
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
|
|
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
|
|
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef SOL_STACK_HPP
|
|
|
|
#define SOL_STACK_HPP
|
|
|
|
|
2014-01-23 01:51:09 +08:00
|
|
|
#include "error.hpp"
|
2013-11-25 17:56:27 +08:00
|
|
|
#include "reference.hpp"
|
2016-03-12 00:34:44 +08:00
|
|
|
#include "userdata.hpp"
|
2013-12-03 03:22:51 +08:00
|
|
|
#include "tuple.hpp"
|
2013-12-13 07:35:42 +08:00
|
|
|
#include "traits.hpp"
|
2014-09-19 20:22:21 +08:00
|
|
|
#include "usertype_traits.hpp"
|
2016-03-05 14:43:45 +08:00
|
|
|
#include "inheritance.hpp"
|
2016-02-11 00:36:00 +08:00
|
|
|
#include "overload.hpp"
|
2013-11-25 17:56:27 +08:00
|
|
|
#include <utility>
|
2013-12-09 12:05:03 +08:00
|
|
|
#include <array>
|
2013-12-10 06:01:52 +08:00
|
|
|
#include <cstring>
|
2014-06-01 02:29:14 +08:00
|
|
|
#include <functional>
|
2013-11-25 17:56:27 +08:00
|
|
|
|
|
|
|
namespace sol {
|
2014-06-07 10:54:45 +08:00
|
|
|
namespace stack {
|
2016-02-14 09:14:31 +08:00
|
|
|
template<typename T, bool global = false, typename = void>
|
|
|
|
struct field_getter;
|
|
|
|
template<typename T, bool global = false, typename = void>
|
|
|
|
struct field_setter;
|
2015-03-02 10:14:42 +08:00
|
|
|
template<typename T, typename = void>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter;
|
2015-03-02 10:14:42 +08:00
|
|
|
template<typename T, typename = void>
|
2016-02-13 01:47:05 +08:00
|
|
|
struct popper;
|
|
|
|
template<typename T, typename = void>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct pusher;
|
2015-03-02 10:14:42 +08:00
|
|
|
template<typename T, type = lua_type_of<T>::value, typename = void>
|
|
|
|
struct checker;
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline int push(lua_State* L, T&& t, Args&&... args) {
|
2016-02-24 12:39:46 +08:00
|
|
|
return pusher<meta::Unqualified<T>>{}.push(L, std::forward<T>(t), std::forward<Args>(args)...);
|
2015-03-02 10:14:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// overload allows to use a pusher of a specific type, but pass in any kind of args
|
|
|
|
template<typename T, typename Arg, typename... Args>
|
|
|
|
inline int push(lua_State* L, Arg&& arg, Args&&... args) {
|
2016-02-24 12:39:46 +08:00
|
|
|
return pusher<meta::Unqualified<T>>{}.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
2015-03-02 10:14:42 +08:00
|
|
|
}
|
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
inline int multi_push(lua_State*) {
|
2015-03-02 10:14:42 +08:00
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
2016-03-12 00:34:44 +08:00
|
|
|
inline int multi_push(lua_State* L, T&& t, Args&&... args) {
|
2015-03-02 10:14:42 +08:00
|
|
|
int pushcount = push(L, std::forward<T>(t));
|
2016-02-14 09:14:31 +08:00
|
|
|
void(sol::detail::swallow{(pushcount += sol::stack::push(L, std::forward<Args>(args)), 0)... });
|
2015-03-02 10:14:42 +08:00
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
2015-07-19 22:26:11 +08:00
|
|
|
template<typename T>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline decltype(auto) get(lua_State* L, int index = -1) {
|
2016-02-24 12:39:46 +08:00
|
|
|
return getter<meta::Unqualified<T>>{}.get(L, index);
|
2015-03-02 10:14:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline decltype(auto) pop(lua_State* L) {
|
2016-02-24 12:39:46 +08:00
|
|
|
return popper<meta::Unqualified<T>>{}.pop(L);
|
2015-03-02 10:14:42 +08:00
|
|
|
}
|
|
|
|
|
2015-06-18 22:53:21 +08:00
|
|
|
template <typename T, typename Handler>
|
|
|
|
bool check(lua_State* L, int index, Handler&& handler) {
|
2016-02-24 12:39:46 +08:00
|
|
|
typedef meta::Unqualified<T> Tu;
|
2015-06-18 22:53:21 +08:00
|
|
|
checker<Tu> c;
|
2015-07-10 06:18:34 +08:00
|
|
|
// VC++ has a bad warning here: shut it up
|
|
|
|
(void)c;
|
2015-06-18 22:53:21 +08:00
|
|
|
return c.check(L, index, std::forward<Handler>(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
2016-03-12 00:34:44 +08:00
|
|
|
bool check(lua_State* L, int index = -1) {
|
|
|
|
auto handler = no_panic;
|
2015-06-18 22:53:21 +08:00
|
|
|
return check<T>(L, index, handler);
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool global = false, typename Key>
|
|
|
|
void get_field(lua_State* L, Key&& key) {
|
2016-02-24 12:39:46 +08:00
|
|
|
field_getter<meta::Unqualified<Key>, global>{}.get(L, std::forward<Key>(key));
|
2016-02-14 09:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key>
|
|
|
|
void get_field(lua_State* L, Key&& key, int tableindex) {
|
2016-02-24 12:39:46 +08:00
|
|
|
field_getter<meta::Unqualified<Key>, global>{}.get(L, std::forward<Key>(key), tableindex);
|
2016-02-14 09:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key, typename Value>
|
|
|
|
void set_field(lua_State* L, Key&& key, Value&& value) {
|
2016-02-24 12:39:46 +08:00
|
|
|
field_setter<meta::Unqualified<Key>, global>{}.set(L, std::forward<Key>(key), std::forward<Value>(value));
|
2016-02-14 09:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key, typename Value>
|
|
|
|
void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
2016-02-24 12:39:46 +08:00
|
|
|
field_setter<meta::Unqualified<Key>, global>{}.set(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
|
2016-02-14 09:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace stack_detail {
|
2015-06-18 22:53:21 +08:00
|
|
|
const bool default_check_arguments =
|
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
|
|
|
true;
|
|
|
|
#else
|
|
|
|
false;
|
|
|
|
#endif
|
|
|
|
|
2016-02-17 10:22:07 +08:00
|
|
|
template<bool releasemem = false, typename TCont>
|
|
|
|
static int push_upvalues(lua_State* L, TCont&& cont) {
|
|
|
|
int n = 0;
|
|
|
|
for(auto& c : cont) {
|
|
|
|
if(releasemem) {
|
2016-03-12 00:34:44 +08:00
|
|
|
stack::push<light_userdata_value>(L, c.release());
|
2016-02-17 10:22:07 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-03-12 00:34:44 +08:00
|
|
|
stack::push<light_userdata_value>(L, c.get());
|
2016-02-17 10:22:07 +08:00
|
|
|
}
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2016-02-02 00:56:44 +08:00
|
|
|
template <typename T>
|
|
|
|
struct userdata_pusher {
|
|
|
|
template <typename Key, typename... Args>
|
|
|
|
static void push (lua_State* L, Key&& metatablekey, Args&&... args) {
|
2016-03-12 00:34:44 +08:00
|
|
|
// Basically, we store all user-data like this:
|
2016-02-09 16:38:11 +08:00
|
|
|
// If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new
|
2016-02-02 00:56:44 +08:00
|
|
|
// data in the first sizeof(T*) bytes, and then however many bytes it takes to
|
2016-02-09 16:38:11 +08:00
|
|
|
// do the actual object. Things that are std::ref or plain T* are stored as
|
2016-02-02 00:56:44 +08:00
|
|
|
// just the sizeof(T*), and nothing else.
|
|
|
|
T** pdatum = static_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
|
|
|
T** referencepointer = pdatum;
|
|
|
|
T*& referencereference = *pdatum;
|
|
|
|
T* allocationtarget = reinterpret_cast<T*>(pdatum + 1);
|
|
|
|
referencereference = allocationtarget;
|
|
|
|
std::allocator<T> alloc{};
|
|
|
|
alloc.construct(allocationtarget, std::forward<Args>(args)...);
|
2016-02-17 10:22:07 +08:00
|
|
|
luaL_getmetatable(L, &metatablekey[0]);
|
2016-02-02 00:56:44 +08:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
};
|
2016-01-30 17:21:33 +08:00
|
|
|
|
2016-02-02 00:56:44 +08:00
|
|
|
template <typename T>
|
|
|
|
struct userdata_pusher<T*> {
|
|
|
|
template <typename Key, typename... Args>
|
|
|
|
static void push (lua_State* L, Key&& metatablekey, Args&&... args) {
|
|
|
|
T** pdatum = static_cast<T**>(lua_newuserdata(L, sizeof(T*)));
|
|
|
|
std::allocator<T*> alloc{};
|
|
|
|
alloc.construct(pdatum, std::forward<Args>(args)...);
|
2016-02-17 10:22:07 +08:00
|
|
|
luaL_getmetatable(L, &metatablekey[0]);
|
2016-02-02 00:56:44 +08:00
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
};
|
2016-01-30 17:21:33 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T, std::size_t... I>
|
|
|
|
inline int push_tuple(std::index_sequence<I...>, lua_State* L, T&& tuplen) {
|
|
|
|
int pushcount = 0;
|
2016-02-25 02:59:17 +08:00
|
|
|
void(detail::swallow{(pushcount += sol::stack::push(L, std::get<I>(tuplen)), '\0')... });
|
2016-02-13 01:47:05 +08:00
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
2015-06-18 22:53:21 +08:00
|
|
|
template <typename T, typename Key, typename... Args>
|
|
|
|
inline int push_confirmed_userdata(lua_State* L, Key&& metatablekey, Args&&... args) {
|
2016-02-02 00:56:44 +08:00
|
|
|
userdata_pusher<T>{}.push(L, std::forward<Key>(metatablekey), std::forward<Args>(args)...);
|
2015-06-18 22:53:21 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Key>
|
|
|
|
inline int push_userdata_pointer(lua_State* L, Key&& metatablekey) {
|
|
|
|
return push_confirmed_userdata<T>(L, std::forward<Key>(metatablekey));
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:39:46 +08:00
|
|
|
template <typename T, typename Key, typename Arg, meta::EnableIf<std::is_same<T, meta::Unqualified<Arg>>> = 0>
|
2015-06-18 22:53:21 +08:00
|
|
|
inline int push_userdata_pointer(lua_State* L, Key&& metatablekey, Arg&& arg) {
|
|
|
|
if (arg == nullptr)
|
|
|
|
return push(L, nil);
|
|
|
|
return push_confirmed_userdata<T>(L, std::forward<Key>(metatablekey), std::forward<Arg>(arg));
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:39:46 +08:00
|
|
|
template <typename T, typename Key, typename Arg, meta::DisableIf<std::is_same<T, meta::Unqualified<Arg>>> = 0>
|
2015-06-18 22:53:21 +08:00
|
|
|
inline int push_userdata_pointer(lua_State* L, Key&& metatablekey, Arg&& arg) {
|
|
|
|
return push_confirmed_userdata<T>(L, std::forward<Key>(metatablekey), std::forward<Arg>(arg));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename Key, typename Arg0, typename Arg1, typename... Args>
|
|
|
|
inline int push_userdata_pointer(lua_State* L, Key&& metatablekey, Arg0&& arg0, Arg1&& arg1, Args&&... args) {
|
|
|
|
return push_confirmed_userdata<T>(L, std::forward<Key>(metatablekey), std::forward<Arg0>(arg0), std::forward<Arg1>(arg1), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:39:46 +08:00
|
|
|
template<typename T, typename Key, typename... Args, meta::DisableIf<std::is_pointer<T>> = 0>
|
2015-06-18 22:53:21 +08:00
|
|
|
inline int push_userdata(lua_State* L, Key&& metatablekey, Args&&... args) {
|
|
|
|
return push_confirmed_userdata<T>(L, std::forward<Key>(metatablekey), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:39:46 +08:00
|
|
|
template<typename T, typename Key, typename... Args, meta::EnableIf<std::is_pointer<T>> = 0>
|
2015-06-18 22:53:21 +08:00
|
|
|
inline int push_userdata(lua_State* L, Key&& metatablekey, Args&&... args) {
|
|
|
|
return push_userdata_pointer<T>(L, std::forward<Key>(metatablekey), std::forward<Args>(args)...);
|
|
|
|
}
|
2016-02-14 09:14:31 +08:00
|
|
|
} // stack_detail
|
2015-06-18 22:53:21 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<typename T, typename>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter {
|
2016-02-22 08:26:58 +08:00
|
|
|
static T& get(lua_State* L, int index = -1) {
|
|
|
|
return getter<T&>{}.get(L, index);
|
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<std::is_floating_point<T>::value>> {
|
|
|
|
static T get(lua_State* L, int index = -1) {
|
|
|
|
return static_cast<T>(lua_tonumber(L, index));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2014-05-09 22:48:55 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct getter<T, std::enable_if_t<meta::And<std::is_integral<T>, std::is_signed<T>>::value>> {
|
2016-02-13 01:47:05 +08:00
|
|
|
static T get(lua_State* L, int index = -1) {
|
2015-03-02 10:14:42 +08:00
|
|
|
return static_cast<T>(lua_tointeger(L, index));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2014-05-09 22:48:55 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct getter<T, std::enable_if_t<meta::And<std::is_integral<T>, std::is_unsigned<T>>::value>> {
|
2016-02-13 01:47:05 +08:00
|
|
|
static T get(lua_State* L, int index = -1) {
|
|
|
|
return static_cast<T>(lua_tointeger(L, index));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2014-06-01 02:29:14 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
|
|
|
struct getter<T, std::enable_if_t<std::is_base_of<reference, T>::value>> {
|
|
|
|
static T get(lua_State* L, int index = -1) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return T(L, index);
|
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2016-03-05 15:42:19 +08:00
|
|
|
template<>
|
2016-03-12 00:34:44 +08:00
|
|
|
struct getter<userdata_value> {
|
|
|
|
static userdata_value get(lua_State* L, int index = -1) {
|
2016-03-05 15:42:19 +08:00
|
|
|
return{ lua_touserdata(L, index) };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
2016-03-12 00:34:44 +08:00
|
|
|
struct getter<light_userdata_value> {
|
|
|
|
static light_userdata_value get(lua_State* L, int index = -1) {
|
2016-03-05 15:42:19 +08:00
|
|
|
return{ lua_touserdata(L, index) };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<void*> {
|
|
|
|
static void* get(lua_State* L, int index = -1) {
|
|
|
|
return lua_touserdata(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<typename T>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<T*> {
|
2016-03-05 14:43:45 +08:00
|
|
|
static T* get_no_nil(lua_State* L, int index = -1) {
|
|
|
|
void** pudata = static_cast<void**>(lua_touserdata(L, index));
|
2016-03-12 00:34:44 +08:00
|
|
|
void* udata = *pudata;
|
|
|
|
return get_no_nil_from(L, udata, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
static T* get_no_nil_from(lua_State* L, void* udata, int index = -1) {
|
2016-03-05 14:43:45 +08:00
|
|
|
#ifndef SOL_NO_EXCEPTIONS
|
2016-03-12 00:34:44 +08:00
|
|
|
if (luaL_getmetafield(L, index, &detail::base_class_check_key[0]) != 0) {
|
|
|
|
void* basecastdata = stack::get<light_userdata_value>(L);
|
|
|
|
detail::throw_cast basecast = (detail::throw_cast)basecastdata;
|
2016-03-05 14:43:45 +08:00
|
|
|
// use the casting function to properly adjust the pointer for the desired T
|
2016-03-12 00:34:44 +08:00
|
|
|
udata = detail::catch_cast<T>(udata, basecast);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2016-03-05 14:43:45 +08:00
|
|
|
#elif !defined(SOL_NO_RTTI)
|
2016-03-12 00:34:44 +08:00
|
|
|
if (luaL_getmetafield(L, index, &detail::base_class_cast_key[0]) != 0) {
|
|
|
|
void* basecastdata = stack::get<light_userdata_value>(L);
|
|
|
|
detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata;
|
2016-03-05 14:43:45 +08:00
|
|
|
// use the casting function to properly adjust the pointer for the desired T
|
|
|
|
udata = ic(udata, typeid(T));
|
2016-03-12 00:34:44 +08:00
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2016-03-05 14:43:45 +08:00
|
|
|
#else
|
2016-03-12 00:34:44 +08:00
|
|
|
// Lol, you motherfucker
|
|
|
|
if (luaL_getmetafield(L, index, &detail::base_class_cast_key[0]) != 0) {
|
|
|
|
void* basecastdata = stack::get<light_userdata_value>(L);
|
|
|
|
detail::inheritance_cast_function ic = (detail::inheritance_cast_function)basecastdata;
|
|
|
|
// use the casting function to properly adjust the pointer for the desired T
|
|
|
|
udata = ic(udata, detail::id_for<T>::value);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
}
|
2016-03-05 14:43:45 +08:00
|
|
|
#endif // No Runtime Type Information || Exceptions
|
|
|
|
T* obj = static_cast<T*>(udata);
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
static T* get(lua_State* L, int index = -1) {
|
2015-05-27 14:23:37 +08:00
|
|
|
type t = type_of(L, index);
|
|
|
|
if (t == type::nil)
|
|
|
|
return nullptr;
|
2016-03-12 00:34:44 +08:00
|
|
|
return get_no_nil(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<non_null<T*>> {
|
|
|
|
static T* get(lua_State* L, int index = -1) {
|
|
|
|
return getter<T*>::get_no_nil(L, index);
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-09-18 12:23:46 +08:00
|
|
|
template<typename T>
|
|
|
|
struct getter<T&> {
|
|
|
|
static T& get(lua_State* L, int index = -1) {
|
2016-03-05 14:43:45 +08:00
|
|
|
return *getter<T*>::get_no_nil(L, index);
|
2014-09-18 12:23:46 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct getter<std::reference_wrapper<T>> {
|
|
|
|
static T& get(lua_State* L, int index = -1) {
|
|
|
|
return getter<T&>{}.get(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<type> {
|
2014-06-09 14:06:51 +08:00
|
|
|
static type get(lua_State *L, int index){
|
2014-06-07 10:54:45 +08:00
|
|
|
return static_cast<type>(lua_type(L, index));
|
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<bool> {
|
|
|
|
static bool get(lua_State* L, int index) {
|
|
|
|
return lua_toboolean(L, index) != 0;
|
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<std::string> {
|
|
|
|
static std::string get(lua_State* L, int index = -1) {
|
2016-02-13 01:47:05 +08:00
|
|
|
std::size_t len;
|
2014-06-07 10:54:45 +08:00
|
|
|
auto str = lua_tolstring(L, index, &len);
|
2014-09-30 10:24:32 +08:00
|
|
|
return { str, len };
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
|
|
|
};
|
2013-12-12 00:56:34 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<const char*> {
|
2014-06-09 14:06:51 +08:00
|
|
|
static const char* get(lua_State* L, int index = -1) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return lua_tostring(L, index);
|
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<nil_t> {
|
2016-03-03 09:45:52 +08:00
|
|
|
static nil_t get(lua_State*, int = -1) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return nil_t{ };
|
|
|
|
}
|
|
|
|
};
|
2014-04-27 06:23:56 +08:00
|
|
|
|
2016-02-17 10:22:07 +08:00
|
|
|
template<>
|
|
|
|
struct getter<lua_CFunction> {
|
|
|
|
static lua_CFunction get(lua_State* L, int index = -1) {
|
|
|
|
return lua_tocfunction(L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct getter<c_closure> {
|
|
|
|
static c_closure get(lua_State* L, int index = -1) {
|
|
|
|
return c_closure(lua_tocfunction(L, index), -1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename... Args>
|
|
|
|
struct getter<std::tuple<Args...>> {
|
|
|
|
template <std::size_t... I>
|
|
|
|
static decltype(auto) apply(std::index_sequence<I...>, lua_State* L, int index = -1) {
|
|
|
|
return std::tuple<decltype(stack::get<Args>(L, index + I))...>(stack::get<Args>(L, index + I)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
static decltype(auto) get(lua_State* L, int index = -1) {
|
|
|
|
return apply(std::index_sequence_for<Args...>(), L, index);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-25 02:59:17 +08:00
|
|
|
template <typename T, type expected, typename>
|
|
|
|
struct checker {
|
|
|
|
template <typename Handler>
|
|
|
|
static bool check (lua_State* L, int index, const Handler& handler) {
|
|
|
|
const type indextype = type_of(L, index);
|
|
|
|
bool success = expected == indextype;
|
|
|
|
if (!success) {
|
|
|
|
// expected type, actual type
|
|
|
|
handler(L, index, expected, indextype);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename C>
|
|
|
|
struct checker<T*, type::userdata, C> {
|
|
|
|
template <typename Handler>
|
|
|
|
static bool check (lua_State* L, int index, const Handler& handler) {
|
|
|
|
const type indextype = type_of(L, index);
|
|
|
|
// Allow nil to be transformed to nullptr
|
|
|
|
if (indextype == type::nil) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-12 06:47:15 +08:00
|
|
|
return checker<T, type::userdata, C>{}.check<T*>(L, indextype, index, handler);
|
2016-02-25 02:59:17 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-03-12 00:34:44 +08:00
|
|
|
template <typename T, typename C>
|
2016-02-25 02:59:17 +08:00
|
|
|
struct checker<T, type::userdata, C> {
|
2016-03-12 06:47:15 +08:00
|
|
|
template <typename U = T, typename Handler>
|
2016-02-25 02:59:17 +08:00
|
|
|
static bool check (lua_State* L, type indextype, int index, const Handler& handler) {
|
|
|
|
if (indextype != type::userdata) {
|
|
|
|
handler(L, index, type::userdata, indextype);
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-12 00:34:44 +08:00
|
|
|
if (meta::Or<std::is_same<T, light_userdata_value>, std::is_same<T, userdata_value>>::value)
|
|
|
|
return true;
|
2016-02-25 02:59:17 +08:00
|
|
|
if (lua_getmetatable(L, index) == 0) {
|
|
|
|
handler(L, index, type::userdata, indextype);
|
|
|
|
return false;
|
|
|
|
}
|
2016-03-12 06:47:15 +08:00
|
|
|
luaL_getmetatable(L, &usertype_traits<U>::metatable[0]);
|
2016-02-25 02:59:17 +08:00
|
|
|
const type expectedmetatabletype = get<type>(L);
|
2016-03-12 06:47:15 +08:00
|
|
|
if (expectedmetatabletype != type::nil) {
|
|
|
|
if (lua_rawequal(L, -1, -2) == 1) {
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lua_pop(L, 1);
|
2016-03-05 14:43:45 +08:00
|
|
|
#ifndef SOL_NO_EXCEPTIONS
|
2016-03-12 06:47:15 +08:00
|
|
|
lua_getfield(L, -1, &detail::base_class_check_key[0]);
|
|
|
|
void* basecastdata = stack::get<light_userdata_value>(L);
|
|
|
|
detail::throw_cast basecast = (detail::throw_cast)basecastdata;
|
|
|
|
bool success = detail::catch_check<T>(basecast);
|
2016-03-05 14:43:45 +08:00
|
|
|
#elif !defined(SOL_NO_RTTI)
|
2016-03-12 06:47:15 +08:00
|
|
|
lua_getfield(L, -1, &detail::base_class_check_key[0]);
|
|
|
|
if (stack::get<type>(L) == type::nil) {
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return false;
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
2016-03-12 06:47:15 +08:00
|
|
|
void* basecastdata = stack::get<light_userdata_value>(L);
|
|
|
|
detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata;
|
|
|
|
bool success = ic(typeid(T));
|
2016-03-12 00:34:44 +08:00
|
|
|
#else
|
|
|
|
// Topkek
|
2016-03-12 06:47:15 +08:00
|
|
|
lua_getfield(L, -1, &detail::base_class_check_key[0]);
|
|
|
|
if (stack::get<type>(L) == type::nil) {
|
|
|
|
lua_pop(L, 2);
|
|
|
|
return false;
|
2016-03-12 00:34:44 +08:00
|
|
|
}
|
2016-03-12 06:47:15 +08:00
|
|
|
void* basecastdata = stack::get<light_userdata_value>(L);
|
|
|
|
detail::inheritance_check_function ic = (detail::inheritance_check_function)basecastdata;
|
|
|
|
bool success = ic(detail::id_for<T>::value);
|
2016-03-05 14:43:45 +08:00
|
|
|
#endif // No Runtime Type Information || Exceptions
|
2016-02-25 02:59:17 +08:00
|
|
|
lua_pop(L, 2);
|
2016-03-12 06:47:15 +08:00
|
|
|
if (!success) {
|
|
|
|
handler(L, index, type::userdata, indextype);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2016-02-25 02:59:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Handler>
|
|
|
|
static bool check (lua_State* L, int index, const Handler& handler) {
|
|
|
|
const type indextype = type_of(L, index);
|
|
|
|
return check(L, indextype, index, handler);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template <typename T, typename>
|
|
|
|
struct popper {
|
|
|
|
inline decltype(auto) pop(lua_State* L) {
|
|
|
|
decltype(auto) r = get<T>(L);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Args>
|
|
|
|
struct popper<std::tuple<Args...>> {
|
|
|
|
inline decltype(auto) pop(lua_State* L) {
|
2016-02-14 09:14:31 +08:00
|
|
|
decltype(auto) r = get<std::tuple<Args...>>(L, lua_gettop(L) - sizeof...(Args) + 1);
|
|
|
|
lua_pop(L, static_cast<int>(sizeof...(Args)));
|
2016-02-13 01:47:05 +08:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename T, typename>
|
2014-06-01 13:03:27 +08:00
|
|
|
struct pusher {
|
2016-02-13 01:47:05 +08:00
|
|
|
static int push(lua_State* L, T& t) {
|
2016-02-14 09:14:31 +08:00
|
|
|
return stack_detail::push_userdata<T>(L, usertype_traits<T>::metatable, t);
|
2016-02-13 01:47:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int push(lua_State* L, T&& t) {
|
2016-02-14 09:14:31 +08:00
|
|
|
return stack_detail::push_userdata<T>(L, usertype_traits<T>::metatable, std::move(t));
|
2016-02-13 01:47:05 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<std::is_floating_point<T>::value>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& value) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushnumber(L, value);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2016-02-22 08:26:58 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2014-06-01 08:02:54 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct pusher<T, std::enable_if_t<meta::And<std::is_integral<T>, std::is_signed<T>>::value>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& value) {
|
2016-02-19 11:17:52 +08:00
|
|
|
lua_pushinteger(L, static_cast<lua_Integer>(value));
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2013-11-26 17:05:04 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct pusher<T, std::enable_if_t<meta::And<std::is_integral<T>, std::is_unsigned<T>>::value>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& value) {
|
2016-02-05 09:16:53 +08:00
|
|
|
typedef std::make_signed_t<T> signed_int;
|
2015-03-02 10:14:42 +08:00
|
|
|
return stack::push(L, static_cast<signed_int>(value));
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
2016-03-12 00:34:44 +08:00
|
|
|
struct pusher<T, std::enable_if_t<meta::And<meta::has_begin_end<T>, meta::Not<meta::has_key_value_pair<T>>, meta::Not<std::is_base_of<reference, T>>>::value>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& cont) {
|
2015-09-07 22:43:39 +08:00
|
|
|
lua_createtable(L, static_cast<int>(cont.size()), 0);
|
2016-03-12 00:34:44 +08:00
|
|
|
int tableindex = lua_gettop(L);
|
2014-06-08 04:33:39 +08:00
|
|
|
unsigned index = 1;
|
|
|
|
for(auto&& i : cont) {
|
2016-03-12 00:34:44 +08:00
|
|
|
set_field(L, index++, i, tableindex);
|
2014-06-08 04:33:39 +08:00
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-08 04:33:39 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2014-06-08 04:33:39 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
2016-03-12 00:34:44 +08:00
|
|
|
struct pusher<T, std::enable_if_t<meta::And<meta::has_begin_end<T>, meta::has_key_value_pair<T>, meta::Not<std::is_base_of<reference, T>>>::value>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& cont) {
|
2015-09-07 22:43:39 +08:00
|
|
|
lua_createtable(L, static_cast<int>(cont.size()), 0);
|
2016-03-12 00:34:44 +08:00
|
|
|
int tableindex = lua_gettop(L);
|
2014-06-08 04:33:39 +08:00
|
|
|
for(auto&& pair : cont) {
|
2016-03-12 00:34:44 +08:00
|
|
|
set_field(L, pair.first, pair.second, tableindex);
|
2014-06-08 04:33:39 +08:00
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-08 04:33:39 +08:00
|
|
|
}
|
2016-02-13 01:47:05 +08:00
|
|
|
};
|
2014-06-08 04:33:39 +08:00
|
|
|
|
2016-02-13 01:47:05 +08:00
|
|
|
template<typename T>
|
|
|
|
struct pusher<T, std::enable_if_t<std::is_base_of<reference, T>::value>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State*, T& ref) {
|
2015-10-21 09:38:28 +08:00
|
|
|
return ref.push();
|
2014-06-09 17:30:00 +08:00
|
|
|
}
|
2016-03-12 00:34:44 +08:00
|
|
|
|
|
|
|
static int push(lua_State*, T&& ref) {
|
|
|
|
return ref.push();
|
|
|
|
}
|
2014-06-09 17:30:00 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct pusher<T*> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, T* obj) {
|
2016-02-14 09:14:31 +08:00
|
|
|
return stack_detail::push_userdata<T*>(L, usertype_traits<T*>::metatable, obj);
|
2014-06-01 14:10:36 +08:00
|
|
|
}
|
2014-06-01 13:03:27 +08:00
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-09-18 12:23:46 +08:00
|
|
|
template<typename T>
|
|
|
|
struct pusher<std::reference_wrapper<T>> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const std::reference_wrapper<T>& t) {
|
|
|
|
return stack::push(L, std::addressof(t.get()));
|
2014-09-18 12:23:46 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<bool> {
|
2016-02-22 08:26:58 +08:00
|
|
|
static int push(lua_State* L, bool b) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushboolean(L, b);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
2013-12-08 10:16:23 +08:00
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<nil_t> {
|
2016-02-22 08:26:58 +08:00
|
|
|
static int push(lua_State* L, nil_t) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushnil(L);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-22 08:26:58 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<std::remove_pointer_t<lua_CFunction>> {
|
|
|
|
static int push(lua_State* L, lua_CFunction func, int n = 0) {
|
|
|
|
lua_pushcclosure(L, func, n);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<lua_CFunction> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, lua_CFunction func, int n = 0) {
|
2014-06-07 12:24:48 +08:00
|
|
|
lua_pushcclosure(L, func, n);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-17 10:22:07 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<c_closure> {
|
|
|
|
static int push(lua_State* L, c_closure closure) {
|
|
|
|
lua_pushcclosure(L, closure.c_function, closure.upvalues);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<void*> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, void* userdata) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushlightuserdata(L, userdata);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-07 12:24:48 +08:00
|
|
|
template<>
|
2016-03-12 00:34:44 +08:00
|
|
|
struct pusher<light_userdata_value> {
|
|
|
|
static int push(lua_State* L, light_userdata_value userdata) {
|
2014-06-07 10:54:45 +08:00
|
|
|
lua_pushlightuserdata(L, userdata);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-07 12:24:48 +08:00
|
|
|
template<>
|
2016-03-12 00:34:44 +08:00
|
|
|
struct pusher<userdata_value> {
|
|
|
|
static int push(lua_State* L, userdata_value data) {
|
2016-02-13 01:47:05 +08:00
|
|
|
void** ud = static_cast<void**>(lua_newuserdata(L, sizeof(void*)));
|
2016-02-22 08:26:58 +08:00
|
|
|
*ud = data.value;
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-07 12:24:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<const char*> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const char* str) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushlstring(L, str, std::char_traits<char>::length(str));
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
|
|
|
template<size_t N>
|
2014-06-01 13:03:27 +08:00
|
|
|
struct pusher<char[N]> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const char (&str)[N]) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushlstring(L, str, N - 1);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<>
|
|
|
|
struct pusher<std::string> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const std::string& str) {
|
2014-06-01 13:03:27 +08:00
|
|
|
lua_pushlstring(L, str.c_str(), str.size());
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
2013-12-09 12:05:03 +08:00
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template<typename... Args>
|
|
|
|
struct pusher<std::tuple<Args...>> {
|
|
|
|
template <typename Tuple>
|
|
|
|
static int push(lua_State* L, Tuple&& tuplen) {
|
2016-02-14 09:14:31 +08:00
|
|
|
return stack_detail::push_tuple(std::index_sequence_for<Args...>(), L, std::forward<Tuple>(tuplen));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, bool, typename>
|
|
|
|
struct field_getter {
|
|
|
|
template <typename Key>
|
|
|
|
void get(lua_State* L, Key&& key, int tableindex = -2) {
|
|
|
|
push( L, std::forward<Key>( key ) );
|
|
|
|
lua_gettable( L, tableindex );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename... Args, bool b, typename C>
|
|
|
|
struct field_getter<std::tuple<Args...>, b, C> {
|
|
|
|
template <std::size_t I0, std::size_t... I, typename Key>
|
|
|
|
void apply(std::index_sequence<I0, I...>, lua_State* L, Key&& key, int tableindex) {
|
|
|
|
get_field<b>(L, std::get<I0>(key), tableindex);
|
2016-02-25 02:59:17 +08:00
|
|
|
void(detail::swallow{ (get_field(L, std::get<I>(key)), 0)... });
|
2016-02-14 09:14:31 +08:00
|
|
|
reference saved(L, -1);
|
2016-02-22 08:26:58 +08:00
|
|
|
lua_pop(L, static_cast<int>(sizeof...(I) + 1));
|
|
|
|
saved.push();
|
2016-02-14 09:14:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Key>
|
|
|
|
void get(lua_State* L, Key&& key, int tableindex = -2) {
|
|
|
|
apply(std::index_sequence_for<Args...>(), L, std::forward<Key>(key), tableindex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct field_getter<T, true, std::enable_if_t<meta::is_c_str<T>::value>> {
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename Key>
|
|
|
|
void get(lua_State* L, Key&& key, int = -1) {
|
|
|
|
lua_getglobal(L, &key[0]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct field_getter<T, false, std::enable_if_t<meta::is_c_str<T>::value>> {
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename Key>
|
|
|
|
void get(lua_State* L, Key&& key, int tableindex = -1) {
|
|
|
|
lua_getfield(L, tableindex, &key[0]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-17 10:22:07 +08:00
|
|
|
#if SOL_LUA_VERSION >= 503
|
|
|
|
template <typename T>
|
|
|
|
struct field_getter<T, false, std::enable_if_t<std::is_integral<T>::value>> {
|
|
|
|
template <typename Key>
|
|
|
|
void get(lua_State* L, Key&& key, int tableindex = -1) {
|
|
|
|
lua_geti(L, tableindex, static_cast<lua_Integer>(key));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif // Lua 5.3.x
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename T, bool, typename>
|
|
|
|
struct field_setter {
|
|
|
|
template <typename Key, typename Value>
|
|
|
|
void set(lua_State* L, Key&& key, Value&& value, int tableindex = -3) {
|
|
|
|
push(L, std::forward<Key>(key));
|
|
|
|
push(L, std::forward<Value>(value));
|
|
|
|
lua_settable(L, tableindex);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct field_setter<T, true, std::enable_if_t<meta::is_c_str<T>::value>> {
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename Key, typename Value>
|
|
|
|
void set(lua_State* L, Key&& key, Value&& value, int = -2) {
|
|
|
|
push(L, std::forward<Value>(value));
|
2016-02-22 08:26:58 +08:00
|
|
|
lua_setglobal(L, &key[0]);
|
2016-02-14 09:14:31 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-02-24 12:39:46 +08:00
|
|
|
struct field_setter<T, false, std::enable_if_t<meta::is_c_str<T>::value>> {
|
2016-02-14 09:14:31 +08:00
|
|
|
template <typename Key, typename Value>
|
|
|
|
void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) {
|
|
|
|
push(L, std::forward<Value>(value));
|
|
|
|
lua_setfield(L, tableindex, &key[0]);
|
2015-03-02 10:14:42 +08:00
|
|
|
}
|
|
|
|
};
|
2014-06-07 10:54:45 +08:00
|
|
|
|
2016-02-17 10:22:07 +08:00
|
|
|
#if SOL_LUA_VERSION >= 503
|
|
|
|
template <typename T>
|
|
|
|
struct field_setter<T, false, std::enable_if_t<std::is_integral<T>::value>> {
|
|
|
|
template <typename Key, typename Value>
|
|
|
|
void set(lua_State* L, Key&& key, Value&& value, int tableindex = -2) {
|
|
|
|
push(L, std::forward<Value>(value));
|
|
|
|
lua_seti(L, tableindex, static_cast<lua_Integer>(key));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif // Lua 5.3.x
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
namespace stack_detail {
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename T>
|
|
|
|
inline int push_as_upvalues(lua_State* L, T& item) {
|
2016-02-05 09:16:53 +08:00
|
|
|
typedef std::decay_t<T> TValue;
|
2013-12-12 00:18:13 +08:00
|
|
|
const static std::size_t itemsize = sizeof(TValue);
|
|
|
|
const static std::size_t voidsize = sizeof(void*);
|
|
|
|
const static std::size_t voidsizem1 = voidsize - 1;
|
|
|
|
const static std::size_t data_t_count = (sizeof(TValue) + voidsizem1) / voidsize;
|
|
|
|
typedef std::array<void*, data_t_count> data_t;
|
2013-12-10 06:01:52 +08:00
|
|
|
|
2013-12-12 00:18:13 +08:00
|
|
|
data_t data{{}};
|
2016-02-17 10:22:07 +08:00
|
|
|
std::memcpy(&data[0], std::addressof(item), itemsize);
|
2015-03-02 10:14:42 +08:00
|
|
|
int pushcount = 0;
|
2014-08-11 08:49:34 +08:00
|
|
|
for(auto&& v : data) {
|
2016-03-12 00:34:44 +08:00
|
|
|
pushcount += push(L, light_userdata_value(v));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
return pushcount;
|
2013-12-10 06:01:52 +08:00
|
|
|
}
|
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename T>
|
|
|
|
inline std::pair<T, int> get_as_upvalues(lua_State* L, int index = 1) {
|
|
|
|
const static std::size_t data_t_count = (sizeof(T)+(sizeof(void*)-1)) / sizeof(void*);
|
|
|
|
typedef std::array<void*, data_t_count> data_t;
|
|
|
|
data_t voiddata{ {} };
|
2014-08-11 08:49:34 +08:00
|
|
|
for(std::size_t i = 0, d = 0; d < sizeof(T); ++i, d += sizeof(void*)) {
|
2016-03-12 00:34:44 +08:00
|
|
|
voiddata[i] = get<light_userdata_value>(L, up_value_index(index++));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
|
|
|
return std::pair<T, int>(*reinterpret_cast<T*>(static_cast<void*>(voiddata.data())), index);
|
|
|
|
}
|
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool b>
|
2016-03-12 06:47:15 +08:00
|
|
|
struct check_types {
|
|
|
|
template <std::size_t I0, std::size_t... I, typename Arg0, typename... Args, typename Handler>
|
|
|
|
static bool check(types<Arg0, Args...>, std::index_sequence<I0, I...>, lua_State* L, int firstargument, Handler&& handler) {
|
|
|
|
if (!stack::check<Arg0>(L, firstargument + I0, handler))
|
2016-02-22 08:26:58 +08:00
|
|
|
return false;
|
2016-03-12 06:47:15 +08:00
|
|
|
return check(types<Args...>(), std::index_sequence<I...>(), L, firstargument, std::forward<Handler>(handler));
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
2015-06-18 22:53:21 +08:00
|
|
|
|
2016-03-12 06:47:15 +08:00
|
|
|
template <typename Handler>
|
|
|
|
static bool check(types<>, std::index_sequence<>, lua_State*, int, Handler&&) {
|
2015-06-18 22:53:21 +08:00
|
|
|
return true;
|
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
};
|
2013-12-15 12:25:44 +08:00
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <>
|
2016-03-12 06:47:15 +08:00
|
|
|
struct check_types<false> {
|
|
|
|
template <std::size_t... I, typename... Args, typename Handler>
|
|
|
|
static bool check(types<Args...>, std::index_sequence<I...>, lua_State*, int, Handler&&) {
|
2015-03-02 10:14:42 +08:00
|
|
|
return true;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
2013-12-16 05:27:20 +08:00
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool checkargs = default_check_arguments, std::size_t... I, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline R call(types<R>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
|
2016-03-12 06:47:15 +08:00
|
|
|
check_types<checkargs>{}.check(ta, tai, L, start, type_panic);
|
2016-02-22 08:26:58 +08:00
|
|
|
return fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, start + I)...);
|
2013-12-16 05:27:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool checkargs = default_check_arguments, std::size_t... I, typename... Args, typename Fx, typename... FxArgs>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline void call(types<void>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
|
2016-03-12 06:47:15 +08:00
|
|
|
check_types<checkargs>{}.check(ta, tai, L, start, type_panic);
|
2016-02-22 08:26:58 +08:00
|
|
|
fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, start + I)...);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
2016-02-14 09:14:31 +08:00
|
|
|
} // stack_detail
|
2013-12-03 03:22:51 +08:00
|
|
|
|
2015-10-25 19:17:48 +08:00
|
|
|
inline void remove( lua_State* L, int index, int count ) {
|
2015-10-22 23:20:32 +08:00
|
|
|
if ( count < 1 )
|
|
|
|
return;
|
|
|
|
int top = lua_gettop( L );
|
|
|
|
if ( index == -1 || top == index ) {
|
|
|
|
// Slice them right off the top
|
|
|
|
lua_pop( L, static_cast<int>(count) );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove each item one at a time using stack operations
|
|
|
|
// Probably slower, maybe, haven't benchmarked,
|
|
|
|
// but necessary
|
|
|
|
if ( index < 0 ) {
|
|
|
|
index = lua_gettop( L ) + (index + 1);
|
|
|
|
}
|
|
|
|
int last = index + count;
|
|
|
|
for ( int i = index; i < last; ++i ) {
|
|
|
|
lua_remove( L, i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool check_args = stack_detail::default_check_arguments, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline R call(types<R> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
|
|
|
|
typedef typename types<Args...>::indices args_indices;
|
2016-02-14 09:14:31 +08:00
|
|
|
return stack_detail::call<check_args>(tr, ta, args_indices(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2014-04-27 06:23:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool check_args = stack_detail::default_check_arguments, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline R call(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
2016-02-22 08:26:58 +08:00
|
|
|
return call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline void call(types<void> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
|
|
|
|
typedef typename types<Args...>::indices args_indices;
|
2016-02-14 09:14:31 +08:00
|
|
|
stack_detail::call<check_args>(tr, ta, args_indices(), L, start, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
|
2016-02-14 09:14:31 +08:00
|
|
|
template <bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
2016-02-13 01:47:05 +08:00
|
|
|
inline void call(types<void> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
2016-02-22 08:26:58 +08:00
|
|
|
call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-02-26 00:07:39 +08:00
|
|
|
template <bool check_args = stack_detail::default_check_arguments, typename R, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<!std::is_void<R>::value>>
|
|
|
|
inline R call_from_top(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
|
|
|
return call<check_args>(tr, ta, L, static_cast<int>(lua_gettop(L) - sizeof...(Args)), std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
|
|
|
}
|
|
|
|
|
2016-02-22 08:26:58 +08:00
|
|
|
template <bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
2016-02-26 00:07:39 +08:00
|
|
|
inline void call_from_top(types<void> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
2016-02-22 08:26:58 +08:00
|
|
|
call<check_args>(tr, ta, L, static_cast<int>(lua_gettop(L) - sizeof...(Args)), std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2013-12-16 05:27:20 +08:00
|
|
|
}
|
|
|
|
|
2016-02-22 08:26:58 +08:00
|
|
|
template<bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
2016-02-26 00:07:39 +08:00
|
|
|
inline int call_into_lua(types<void> tr, types<Args...> ta, Fx&& fx, lua_State* L, int start, FxArgs&&... fxargs) {
|
2016-02-22 08:26:58 +08:00
|
|
|
call<check_args>(tr, ta, L, start, fx, std::forward<FxArgs>(fxargs)...);
|
2016-02-11 00:36:00 +08:00
|
|
|
int nargs = static_cast<int>(sizeof...(Args));
|
|
|
|
lua_pop(L, nargs);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-24 12:39:46 +08:00
|
|
|
template<bool check_args = stack_detail::default_check_arguments, typename Ret0, typename... Ret, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<meta::Not<std::is_void<Ret0>>::value>>
|
2016-02-26 00:07:39 +08:00
|
|
|
inline int call_into_lua(types<Ret0, Ret...>, types<Args...> ta, Fx&& fx, lua_State* L, int start, FxArgs&&... fxargs) {
|
2016-02-24 12:39:46 +08:00
|
|
|
decltype(auto) r = call<check_args>(types<meta::return_type_t<Ret0, Ret...>>(), ta, L, start, fx, std::forward<FxArgs>(fxargs)...);
|
2016-02-11 00:36:00 +08:00
|
|
|
int nargs = static_cast<int>(sizeof...(Args));
|
|
|
|
lua_pop(L, nargs);
|
2016-02-13 01:47:05 +08:00
|
|
|
return push(L, std::forward<decltype(r)>(r));
|
2016-02-11 00:36:00 +08:00
|
|
|
}
|
|
|
|
|
2014-04-27 06:23:56 +08:00
|
|
|
inline call_syntax get_call_syntax(lua_State* L, const std::string& meta) {
|
2016-02-19 11:17:52 +08:00
|
|
|
luaL_getmetatable(L, meta.c_str());
|
|
|
|
if (lua_compare(L, -1, -2, LUA_OPEQ) == 1) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return call_syntax::colon;
|
2014-04-27 06:23:56 +08:00
|
|
|
}
|
2016-02-19 11:17:52 +08:00
|
|
|
lua_pop(L, 1);
|
2014-04-27 06:23:56 +08:00
|
|
|
return call_syntax::dot;
|
|
|
|
}
|
2016-02-27 15:43:53 +08:00
|
|
|
|
2016-02-27 20:56:28 +08:00
|
|
|
inline void luajit_exception_handler(lua_State* L, int(*handler)(lua_State*, lua_CFunction) = detail::c_trampoline) {
|
2016-02-27 15:43:53 +08:00
|
|
|
#ifdef SOL_LUAJIT
|
|
|
|
lua_pushlightuserdata(L, (void*)handler);
|
|
|
|
luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_ON);
|
|
|
|
lua_pop(L, 1);
|
2016-02-27 15:59:47 +08:00
|
|
|
#else
|
|
|
|
(void)L;
|
|
|
|
(void)handler;
|
2016-02-27 15:43:53 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void luajit_exception_off(lua_State* L) {
|
|
|
|
#ifdef SOL_LUAJIT
|
|
|
|
luaJIT_setmode(L, -1, LUAJIT_MODE_WRAPCFUNC | LUAJIT_MODE_OFF);
|
2016-02-27 15:59:47 +08:00
|
|
|
#else
|
|
|
|
(void)L;
|
2016-02-27 15:43:53 +08:00
|
|
|
#endif
|
|
|
|
}
|
2013-11-25 17:56:27 +08:00
|
|
|
} // stack
|
|
|
|
} // sol
|
|
|
|
|
2014-05-29 14:38:02 +08:00
|
|
|
#endif // SOL_STACK_HPP
|