2013-11-25 17:56:27 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
2015-05-25 09:29:21 +08:00
|
|
|
// Copyright (c) 2013-2015 Danny Y., Rapptz
|
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"
|
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"
|
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 {
|
|
|
|
namespace detail {
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename T>
|
2014-06-09 14:06:51 +08:00
|
|
|
inline T* get_ptr(T& val) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return std::addressof(val);
|
2014-05-09 22:48:55 +08:00
|
|
|
}
|
|
|
|
|
2014-06-29 14:16:48 +08:00
|
|
|
template<typename T>
|
|
|
|
inline T* get_ptr(std::reference_wrapper<T> val) {
|
|
|
|
return std::addressof(val.get());
|
|
|
|
}
|
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename T>
|
2014-06-09 14:06:51 +08:00
|
|
|
inline T* get_ptr(T* val) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return val;
|
2014-05-09 22:48:55 +08:00
|
|
|
}
|
2014-08-05 15:08:41 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<typename Decorated>
|
2014-08-05 15:08:41 +08:00
|
|
|
struct return_forward {
|
|
|
|
typedef Unqualified<Decorated> T;
|
|
|
|
|
|
|
|
T& operator()(T& value) const {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
T&& operator()(T&& value) const {
|
|
|
|
return std::move(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
T operator()(const T& value) const {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// handle retarded libraries
|
|
|
|
T operator()(const T&& value) const {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
};
|
2014-06-07 10:54:45 +08:00
|
|
|
} // detail
|
2014-05-09 22:48:55 +08:00
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
namespace stack {
|
|
|
|
namespace detail {
|
2014-06-08 04:33:39 +08:00
|
|
|
template<typename T, typename Key, typename... Args>
|
2015-03-02 10:14:42 +08:00
|
|
|
inline int push_userdata(lua_State* L, Key&& metatablekey, Args&&... args) {
|
2014-06-08 04:33:39 +08:00
|
|
|
T* pdatum = static_cast<T*>(lua_newuserdata(L, sizeof(T)));
|
|
|
|
std::allocator<T> alloc{};
|
|
|
|
alloc.construct(pdatum, std::forward<Args>(args)...);
|
2014-06-07 10:54:45 +08:00
|
|
|
luaL_getmetatable(L, std::addressof(metatablekey[0]));
|
|
|
|
lua_setmetatable(L, -2);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-05-31 07:58:47 +08:00
|
|
|
}
|
2014-06-07 10:54:45 +08:00
|
|
|
} // detail
|
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>
|
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) {
|
|
|
|
return pusher<Unqualified<T>>{}.push(L, std::forward<T>(t), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, std::size_t... I>
|
|
|
|
inline int push_tuple(lua_State* L, indices<I...>, T&& tuplen) {
|
|
|
|
using swallow = char[1 + sizeof...(I)];
|
|
|
|
int pushcount = 0;
|
|
|
|
swallow {'\0', (pushcount += sol::stack::push(L, std::get<I>(tuplen)), '\0')... };
|
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return pusher<Unqualified<T>>{}.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int push_args(lua_State*) {
|
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline int push_args(lua_State* L, T&& t, Args&&... args) {
|
|
|
|
int pushcount = push(L, std::forward<T>(t));
|
|
|
|
using swallow = char[];
|
|
|
|
void(swallow{'\0', (pushcount += push(L, std::forward<Args>(args)), '\0')... });
|
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U = Unqualified<T>>
|
|
|
|
inline auto get(lua_State* L, int index = -1) -> decltype(getter<U>{}.get(L, index)) {
|
|
|
|
return getter<U>{}.get(L, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
auto pop(lua_State* L) -> decltype(get<T>(L)) {
|
|
|
|
typedef decltype(get<T>(L)) ret_t;
|
|
|
|
ret_t r = get<T>(L);
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct get_return {
|
|
|
|
typedef decltype(get<T>(nullptr)) type;
|
|
|
|
};
|
|
|
|
|
|
|
|
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 Handler>
|
|
|
|
bool check(lua_State* L, int index, Handler&& handler) {
|
|
|
|
typedef Unqualified<T> Tu;
|
|
|
|
checker<Tu> c;
|
|
|
|
return c.check(L, index, std::forward<Handler>(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool check(lua_State* L, int index) {
|
|
|
|
auto handler = type_panic;
|
|
|
|
return check<T>(L, index, handler);
|
|
|
|
}
|
2014-05-31 07:58:47 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<typename T, typename>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter {
|
|
|
|
template<typename U = T, EnableIf<std::is_floating_point<U>> = 0>
|
|
|
|
static U get(lua_State* L, int index = -1) {
|
2014-09-30 10:24:32 +08:00
|
|
|
return static_cast<U>(lua_tonumber(L, index));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
2014-05-09 22:48:55 +08:00
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename U = T, EnableIf<std::is_integral<U>, std::is_signed<U>> = 0>
|
|
|
|
static U 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
|
|
|
}
|
2014-05-09 22:48:55 +08:00
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename U = T, EnableIf<std::is_integral<U>, std::is_unsigned<U>> = 0>
|
|
|
|
static U get(lua_State* L, int index = -1) {
|
2015-03-02 10:14:42 +08:00
|
|
|
typedef typename std::make_signed<U>::type signed_int;
|
|
|
|
return static_cast<U>(stack::get<signed_int>(L, index));
|
2014-06-07 10:54:45 +08:00
|
|
|
}
|
2014-06-01 02:29:14 +08:00
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename U = T, EnableIf<std::is_base_of<reference, U>> = 0>
|
|
|
|
static U get(lua_State* L, int index = -1) {
|
|
|
|
return T(L, index);
|
|
|
|
}
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename U = T, EnableIf<Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
|
|
|
|
static U& get(lua_State* L, int index = -1) {
|
|
|
|
void* udata = lua_touserdata(L, index);
|
|
|
|
T* obj = static_cast<T*>(udata);
|
|
|
|
return *obj;
|
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<typename T>
|
2014-06-07 10:54:45 +08:00
|
|
|
struct getter<T*> {
|
|
|
|
static T* get(lua_State* L, int index = -1) {
|
|
|
|
void* udata = lua_touserdata(L, index);
|
|
|
|
T** obj = static_cast<T**>(udata);
|
|
|
|
return *obj;
|
|
|
|
}
|
|
|
|
};
|
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) {
|
|
|
|
void* udata = lua_touserdata(L, index);
|
|
|
|
T** obj = static_cast<T**>(udata);
|
|
|
|
return **obj;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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) {
|
|
|
|
std::string::size_type len;
|
|
|
|
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> {
|
2014-06-09 14:06:51 +08:00
|
|
|
static nil_t get(lua_State* L, int index = -1) {
|
2014-08-11 08:49:34 +08:00
|
|
|
if(lua_isnil(L, index) == 0) {
|
2014-06-07 10:54:45 +08:00
|
|
|
throw sol::error("not nil");
|
2014-08-11 08:49:34 +08:00
|
|
|
}
|
2014-06-07 10:54:45 +08:00
|
|
|
return nil_t{ };
|
|
|
|
}
|
|
|
|
};
|
2014-04-27 06:23:56 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2015-03-02 10:14:42 +08:00
|
|
|
struct getter<userdata> {
|
|
|
|
static userdata get(lua_State* L, int index = -1) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return{ lua_touserdata(L, index) };
|
2014-04-26 08:20:35 +08:00
|
|
|
}
|
2014-06-07 10:54:45 +08:00
|
|
|
};
|
2014-04-26 08:20:35 +08:00
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2015-03-02 10:14:42 +08:00
|
|
|
struct getter<light_userdata> {
|
|
|
|
static light_userdata get(lua_State* L, int index = 1) {
|
2014-06-07 12:24:48 +08:00
|
|
|
return{ lua_touserdata(L, index) };
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-09 19:54:58 +08:00
|
|
|
template<>
|
2015-03-02 10:14:42 +08:00
|
|
|
struct getter<upvalue> {
|
|
|
|
static upvalue get(lua_State* L, int index = 1) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return{ lua_touserdata(L, lua_upvalueindex(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<void*> {
|
2014-06-09 14:06:51 +08:00
|
|
|
static void* get(lua_State* L, int index = 1) {
|
2014-06-07 10:54:45 +08:00
|
|
|
return lua_touserdata(L, index);
|
|
|
|
}
|
|
|
|
};
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<typename T, typename>
|
2014-06-01 13:03:27 +08:00
|
|
|
struct pusher {
|
|
|
|
template<typename U = T, EnableIf<std::is_floating_point<U>> = 0>
|
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;
|
2014-06-01 08:02:54 +08:00
|
|
|
}
|
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<typename U = T, EnableIf<std::is_integral<U>, std::is_signed<U>> = 0>
|
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_pushinteger(L, value);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
2013-11-26 17:05:04 +08:00
|
|
|
|
2014-06-01 13:03:27 +08:00
|
|
|
template<typename U = T, EnableIf<std::is_integral<U>, std::is_unsigned<U>> = 0>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& value) {
|
|
|
|
typedef typename std::make_signed<T>::type signed_int;
|
|
|
|
return stack::push(L, static_cast<signed_int>(value));
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
2013-11-25 17:56:27 +08:00
|
|
|
|
2014-06-09 17:30:00 +08:00
|
|
|
template<typename U = T, EnableIf<has_begin_end<U>, Not<has_key_value_pair<U>>> = 0>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& cont) {
|
2014-06-08 04:33:39 +08:00
|
|
|
lua_createtable(L, cont.size(), 0);
|
|
|
|
unsigned index = 1;
|
|
|
|
for(auto&& i : cont) {
|
|
|
|
// push the index
|
|
|
|
pusher<unsigned>{}.push(L, index++);
|
|
|
|
// push the value
|
|
|
|
pusher<Unqualified<decltype(i)>>{}.push(L, i);
|
|
|
|
// set the table
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-08 04:33:39 +08:00
|
|
|
}
|
|
|
|
|
2014-06-09 17:30:00 +08:00
|
|
|
template<typename U = T, EnableIf<has_begin_end<U>, has_key_value_pair<U>> = 0>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const T& cont) {
|
2014-06-08 04:33:39 +08:00
|
|
|
lua_createtable(L, cont.size(), 0);
|
|
|
|
for(auto&& pair : cont) {
|
|
|
|
pusher<Unqualified<decltype(pair.first)>>{}.push(L, pair.first);
|
|
|
|
pusher<Unqualified<decltype(pair.second)>>{}.push(L, pair.second);
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-08 04:33:39 +08:00
|
|
|
}
|
|
|
|
|
2014-06-09 17:30:00 +08:00
|
|
|
template<typename U = T, EnableIf<std::is_base_of<reference, U>> = 0>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State*, T& ref) {
|
|
|
|
return ref.push();
|
2014-06-09 17:30:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U = Unqualified<T>, EnableIf<Not<has_begin_end<U>>, Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, T& t) {
|
|
|
|
return detail::push_userdata<U>(L, usertype_traits<T>::metatable, t);
|
2014-06-09 17:30:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U = Unqualified<T>, EnableIf<Not<has_begin_end<U>>, Not<std::is_base_of<reference, U>>, Not<std::is_integral<U>>, Not<std::is_floating_point<U>>> = 0>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, T&& t) {
|
|
|
|
return detail::push_userdata<U>(L, usertype_traits<T>::metatable, std::move(t));
|
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) {
|
|
|
|
return 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> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const 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> {
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, const 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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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<>
|
2015-03-02 10:14:42 +08:00
|
|
|
struct pusher<upvalue> {
|
|
|
|
static int push(lua_State* L, upvalue upvalue) {
|
2014-06-07 12:24:48 +08:00
|
|
|
lua_pushlightuserdata(L, upvalue);
|
2015-03-02 10:14:42 +08:00
|
|
|
return 1;
|
2014-06-07 12:24:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-07 10:54:45 +08:00
|
|
|
template<>
|
2015-03-02 10:14:42 +08:00
|
|
|
struct pusher<light_userdata> {
|
|
|
|
static int push(lua_State* L, light_userdata 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<>
|
2015-03-02 10:14:42 +08:00
|
|
|
struct pusher<userdata> {
|
2014-08-09 19:54:58 +08:00
|
|
|
template<typename T, typename U = Unqualified<T>>
|
2015-03-02 10:14:42 +08:00
|
|
|
static int push(lua_State* L, T&& data) {
|
2014-06-07 12:24:48 +08:00
|
|
|
U* userdata = static_cast<U*>(lua_newuserdata(L, sizeof(U)));
|
|
|
|
new(userdata)U(std::forward<T>(data));
|
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) {
|
|
|
|
return push_tuple(L, build_indices<sizeof...(Args)>(), std::forward<Tuple>(tuplen));
|
|
|
|
}
|
|
|
|
};
|
2014-06-07 10:54:45 +08:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
template<typename T>
|
|
|
|
inline int push_as_upvalues(lua_State* L, T& item) {
|
2013-12-12 00:18:13 +08:00
|
|
|
typedef typename std::decay<T>::type TValue;
|
|
|
|
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{{}};
|
2013-12-12 18:07:58 +08:00
|
|
|
std::memcpy(std::addressof(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) {
|
2015-03-02 10:14:42 +08:00
|
|
|
pushcount += push(L, upvalue(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*)) {
|
2015-03-02 10:14:42 +08:00
|
|
|
voiddata[i] = get<upvalue>(L, 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>
|
|
|
|
struct check_arguments {
|
|
|
|
template <std::size_t... I, typename... Args>
|
|
|
|
static bool check(lua_State* L, int firstargument, indices<I...>, types<Args...>) {
|
|
|
|
bool checks = true;
|
|
|
|
using swallow = int[sizeof...(Args)+2];
|
|
|
|
(void)swallow {
|
|
|
|
0, (checks &= stack::check<Args>(L, firstargument + I))..., 0
|
|
|
|
};
|
|
|
|
return checks;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
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 <>
|
|
|
|
struct check_arguments<false> {
|
|
|
|
template <std::size_t... I, typename... Args>
|
|
|
|
static bool check(lua_State*, int, indices<I...>, types<Args...>) {
|
|
|
|
return true;
|
2014-06-01 13:03:27 +08:00
|
|
|
}
|
|
|
|
};
|
2013-12-16 05:27:20 +08:00
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool checkargs = false, std::size_t... I, typename R, typename... Args, typename Fx, typename... FxArgs, typename = typename std::enable_if<!std::is_void<R>::value>::type>
|
|
|
|
inline R call(lua_State* L, int start, indices<I...>, types<R>, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
|
|
|
|
const int stacksize = lua_gettop(L);
|
|
|
|
const int firstargument = start + stacksize - std::max(sizeof...(Args)-1, static_cast<std::size_t>(0));
|
2013-12-16 05:27:20 +08:00
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
detail::check_arguments<checkargs>{}.check(L, firstargument, ta, ta);
|
|
|
|
|
|
|
|
return fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, firstargument + I)...);
|
2013-12-16 05:27:20 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool checkargs = false, std::size_t... I, typename... Args, typename Fx, typename... FxArgs>
|
|
|
|
inline void call(lua_State* L, int start, indices<I...>, types<void>, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
|
|
|
|
const int stacksize = lua_gettop(L);
|
|
|
|
const int firstargument = start + stacksize - std::max(sizeof...(Args)-1, static_cast<std::size_t>(0));
|
|
|
|
|
|
|
|
bool checks = detail::check_arguments<checkargs>{}.check(L, firstargument, ta, ta);
|
|
|
|
if ( !checks )
|
|
|
|
throw error("Arguments not of the proper types for this function call");
|
|
|
|
|
|
|
|
fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, firstargument + I)...);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
2015-03-02 10:14:42 +08:00
|
|
|
} // detail
|
2013-12-03 03:22:51 +08:00
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool checkargs = false, typename R, typename... Args, typename Fx, typename... FxArgs, typename = typename std::enable_if<!std::is_void<R>::value>::type>
|
|
|
|
inline R call(lua_State* L, int start, types<R> tr, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
|
|
|
|
return detail::call(L, start, ta, tr, ta, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2014-04-27 06:23:56 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool checkargs = false, typename R, typename... Args, typename Fx, typename... FxArgs, typename = typename std::enable_if<!std::is_void<R>::value>::type>
|
|
|
|
inline R call(lua_State* L, types<R> tr, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
|
|
|
|
return call(L, 0, ta, tr, ta, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2014-05-31 06:21:19 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool checkargs = false, typename... Args, typename Fx, typename... FxArgs>
|
|
|
|
inline void call(lua_State* L, int start, types<void> tr, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
|
|
|
|
detail::call(L, start, ta, tr, ta, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
|
2015-03-02 10:14:42 +08:00
|
|
|
template <bool checkargs = false, typename... Args, typename Fx, typename... FxArgs>
|
|
|
|
inline void call(lua_State* L, types<void> tr, types<Args...> ta, Fx&& fx, FxArgs&&... args) {
|
|
|
|
call(L, 0, ta, tr, ta, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
2013-12-16 05:27:20 +08:00
|
|
|
}
|
|
|
|
|
2014-04-27 06:23:56 +08:00
|
|
|
inline call_syntax get_call_syntax(lua_State* L, const std::string& meta) {
|
2015-03-02 10:14:42 +08:00
|
|
|
if (get<type>(L, 1) == type::table) {
|
|
|
|
if (luaL_newmetatable(L, meta.c_str()) == 0) {
|
2014-04-27 06:23:56 +08:00
|
|
|
lua_settop(L, -2);
|
|
|
|
return call_syntax::colon;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return call_syntax::dot;
|
|
|
|
}
|
2013-11-25 17:56:27 +08:00
|
|
|
} // stack
|
|
|
|
} // sol
|
|
|
|
|
2014-05-29 14:38:02 +08:00
|
|
|
#endif // SOL_STACK_HPP
|