2013-11-25 17:56:27 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
|
|
|
// Copyright (c) 2013 Danny Y., Rapptz
|
|
|
|
|
|
|
|
// 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"
|
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>
|
2013-11-25 17:56:27 +08:00
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
namespace stack {
|
|
|
|
namespace detail {
|
|
|
|
template<typename T>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline T get_unsigned(lua_State* L, std::true_type, int index = -1) {
|
|
|
|
return lua_tounsigned(L, index);
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline T get_unsigned(lua_State* L, std::false_type, int index = -1) {
|
2013-12-12 00:18:13 +08:00
|
|
|
return static_cast<T>(lua_tointeger(L, index));
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline T get_arithmetic(lua_State* L, std::false_type, int index = -1) {
|
2013-11-25 17:56:27 +08:00
|
|
|
// T is a floating point
|
2013-12-14 13:15:14 +08:00
|
|
|
return static_cast<T>(lua_tonumber(L, index));
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline T get_arithmetic(lua_State* L, std::true_type, int index = -1) {
|
2013-11-25 17:56:27 +08:00
|
|
|
// T is an integral
|
2013-12-01 10:12:50 +08:00
|
|
|
return get_unsigned<T>(L, std::is_unsigned<T>{}, index);
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
2013-12-11 21:32:27 +08:00
|
|
|
template<typename T>
|
|
|
|
inline T get_nil(lua_State* L, std::true_type, int index = -1) {
|
|
|
|
if (lua_isnil(L, index) == 0)
|
2013-12-12 00:18:13 +08:00
|
|
|
throw sol::sol_error("not nil");
|
2013-12-11 21:32:27 +08:00
|
|
|
return nil_t{};
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T get_nil(lua_State* L, std::false_type, int index = -1) {
|
2013-11-25 17:56:27 +08:00
|
|
|
// T is a class type
|
2013-11-29 11:51:51 +08:00
|
|
|
return T(L, index);
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 00:56:34 +08:00
|
|
|
template<typename T>
|
|
|
|
inline T get_helper(lua_State* L, std::true_type, int index = -1) {
|
|
|
|
return get_nil<T>(L, std::is_same<nil_t, T>(), index);
|
|
|
|
}
|
|
|
|
|
2013-11-25 17:56:27 +08:00
|
|
|
template<typename T>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline T get_helper(lua_State* L, std::false_type, int index = -1) {
|
2013-11-25 17:56:27 +08:00
|
|
|
// T is a fundamental type
|
2013-11-29 11:51:51 +08:00
|
|
|
return get_arithmetic<T>(L, std::is_integral<T>{}, index);
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void push_unsigned(lua_State* L, T x, std::true_type) {
|
|
|
|
lua_pushunsigned(L, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void push_unsigned(lua_State* L, T x, std::false_type) {
|
|
|
|
lua_pushinteger(L, x);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void push_arithmetic(lua_State* L, T x, std::true_type) {
|
|
|
|
// T is an integral type
|
|
|
|
push_unsigned(L, x, std::is_unsigned<T>{});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void push_arithmetic(lua_State* L, T x, std::false_type) {
|
|
|
|
// T is an floating point type
|
|
|
|
lua_pushnumber(L, x);
|
|
|
|
}
|
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline T get(lua_State* L, int index = -1) {
|
|
|
|
return detail::get_helper<T>(L, std::is_class<T>{}, index);
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline bool get<bool>(lua_State* L, int index) {
|
|
|
|
return lua_toboolean(L, index) != 0;
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline std::string get<std::string>(lua_State* L, int index) {
|
2013-11-25 17:56:27 +08:00
|
|
|
std::string::size_type len;
|
2013-11-29 11:51:51 +08:00
|
|
|
auto str = lua_tolstring(L, index, &len);
|
2013-11-25 17:56:27 +08:00
|
|
|
return { str, len };
|
|
|
|
}
|
|
|
|
|
|
|
|
template<>
|
2013-11-29 11:51:51 +08:00
|
|
|
inline const char* get<const char*>(lua_State* L, int index) {
|
|
|
|
return lua_tostring(L, index);
|
2013-11-29 07:27:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline T pop(lua_State* L) {
|
|
|
|
auto r = get<T>(L);
|
2013-11-25 17:56:27 +08:00
|
|
|
lua_pop(L, 1);
|
2013-11-29 07:27:27 +08:00
|
|
|
return r;
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2013-11-30 06:57:46 +08:00
|
|
|
inline EnableIf<std::is_arithmetic<T>> push(lua_State* L, T arithmetic) {
|
2013-11-26 14:21:53 +08:00
|
|
|
detail::push_arithmetic(L, arithmetic, std::is_integral<T>{});
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
2013-11-26 18:07:10 +08:00
|
|
|
inline void push(lua_State*, reference& ref) {
|
2013-11-26 17:05:04 +08:00
|
|
|
ref.push();
|
|
|
|
}
|
|
|
|
|
2013-11-25 17:56:27 +08:00
|
|
|
inline void push(lua_State* L, bool boolean) {
|
|
|
|
lua_pushboolean(L, boolean);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void push(lua_State* L, const nil_t&) {
|
|
|
|
lua_pushnil(L);
|
|
|
|
}
|
|
|
|
|
2013-12-09 12:09:07 +08:00
|
|
|
inline void push(lua_State* L, lua_CFunction func) {
|
|
|
|
lua_pushcfunction(L, func);
|
2013-12-08 10:16:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-09 12:09:07 +08:00
|
|
|
inline void push(lua_State* L, lua_CFunction func, int n) {
|
|
|
|
lua_pushcclosure(L, func, n);
|
2013-12-08 10:16:23 +08:00
|
|
|
}
|
|
|
|
|
2013-12-09 12:09:07 +08:00
|
|
|
inline void push(lua_State* L, void* userdata) {
|
|
|
|
lua_pushlightuserdata(L, userdata);
|
2013-11-25 17:56:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<size_t N>
|
|
|
|
inline void push(lua_State* L, const char (&str)[N]) {
|
|
|
|
lua_pushlstring(L, str, N - 1);
|
|
|
|
}
|
|
|
|
|
2013-12-09 12:05:03 +08:00
|
|
|
inline void push(lua_State* L, const char* str) {
|
2013-12-09 12:09:07 +08:00
|
|
|
lua_pushlstring(L, str, std::char_traits<char>::length(str));
|
2013-12-09 12:05:03 +08:00
|
|
|
}
|
|
|
|
|
2013-11-25 17:56:27 +08:00
|
|
|
inline void push(lua_State* L, const std::string& str) {
|
|
|
|
lua_pushlstring(L, str.c_str(), str.size());
|
|
|
|
}
|
2013-12-03 03:22:51 +08:00
|
|
|
|
2013-12-10 03:12:38 +08:00
|
|
|
template<typename T>
|
|
|
|
inline void push_user(lua_State* L, T& userdata, const char* metatablekey) {
|
|
|
|
T* pdatum = static_cast<T*>(lua_newuserdata(L, sizeof(T)));
|
|
|
|
T& datum = *pdatum;
|
|
|
|
datum = userdata;
|
|
|
|
if (metatablekey != nullptr) {
|
|
|
|
lua_getfield(L, LUA_REGISTRYINDEX, metatablekey);
|
|
|
|
lua_setmetatable(L, -2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-09 12:05:03 +08:00
|
|
|
template<typename T, size_t N>
|
2013-12-09 12:09:07 +08:00
|
|
|
inline void push(lua_State* L, const std::array<T, N>& data) {
|
2013-12-09 12:42:20 +08:00
|
|
|
for (auto&& i : data) {
|
|
|
|
push(L, i);
|
2013-12-09 12:09:07 +08:00
|
|
|
}
|
2013-12-09 12:05:03 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 00:18:13 +08:00
|
|
|
template<typename T>
|
|
|
|
inline int push_user(lua_State* L, T& item) {
|
|
|
|
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);
|
2013-12-12 00:18:13 +08:00
|
|
|
push(L, data);
|
|
|
|
return data_t_count;
|
2013-12-10 06:01:52 +08:00
|
|
|
}
|
|
|
|
|
2013-12-03 03:22:51 +08:00
|
|
|
namespace detail {
|
|
|
|
template<typename T, std::size_t... I>
|
2013-12-15 12:25:44 +08:00
|
|
|
inline void push_tuple(lua_State* L, indices<I...>, T&& tuplen) {
|
2013-12-22 11:11:20 +08:00
|
|
|
using swallow = char[1 + sizeof...(I)];
|
2013-12-14 12:26:59 +08:00
|
|
|
swallow {'\0', (sol::stack::push(L, std::get<I>(tuplen)), '\0')... };
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
2013-12-03 08:34:10 +08:00
|
|
|
|
2013-12-16 06:09:23 +08:00
|
|
|
template<typename F, typename... Vs, typename... Args>
|
2013-12-16 11:14:56 +08:00
|
|
|
auto ltr_pop(lua_State*, F&& f, types<Args...>, types<>, Vs&&... vs) -> decltype(f(std::forward<Vs>(vs)...)) {
|
2013-12-09 12:09:07 +08:00
|
|
|
return f(std::forward<Vs>(vs)...);
|
2013-12-03 10:39:21 +08:00
|
|
|
}
|
2013-12-16 06:09:23 +08:00
|
|
|
template<typename F, typename Head, typename... Tail, typename... Vs, typename... Args>
|
|
|
|
auto ltr_pop(lua_State* L, F&& f, types<Args...> t, types<Head, Tail...>, Vs&&... vs) -> decltype(f(std::declval<Args>()...)) {
|
|
|
|
return ltr_pop(L, std::forward<F>(f), t, types<Tail...>(), std::forward<Vs>(vs)..., pop<Head>(L));
|
2013-12-03 08:34:10 +08:00
|
|
|
}
|
2013-12-16 05:27:20 +08:00
|
|
|
|
2013-12-16 05:52:01 +08:00
|
|
|
template<typename F, typename... Vs, typename... Args>
|
|
|
|
auto rtl_pop(lua_State*, F&& f, types<Args...>, types<>, Vs&&... vs) -> decltype(f(std::forward<Vs>(vs)...)) {
|
2013-12-16 05:27:20 +08:00
|
|
|
return f(std::forward<Vs>(vs)...);
|
|
|
|
}
|
2013-12-16 05:45:40 +08:00
|
|
|
template<typename F, typename Head, typename... Tail, typename... Vs, typename... Args>
|
2013-12-16 05:56:01 +08:00
|
|
|
auto rtl_pop(lua_State* L, F&& f, types<Args...> t, types<Head, Tail...>, Vs&&... vs) -> decltype(f(std::declval<Args>()...)) {
|
2013-12-16 11:09:06 +08:00
|
|
|
return rtl_pop(L, std::forward<F>(f), t, types<Tail...>(), pop<Head>(L), std::forward<Vs>(vs)...);
|
2013-12-16 05:27:20 +08:00
|
|
|
}
|
2013-12-03 03:22:51 +08:00
|
|
|
} // detail
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void push(lua_State* L, const std::tuple<Args...>& tuplen) {
|
2013-12-16 05:27:20 +08:00
|
|
|
detail::push_tuple(L, build_indices<sizeof...(Args)>(), tuplen);
|
2013-12-15 12:25:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void push(lua_State* L, std::tuple<Args...>&& tuplen) {
|
2013-12-16 05:27:20 +08:00
|
|
|
detail::push_tuple(L, build_indices<sizeof...(Args)>(), std::move(tuplen));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline void push_reverse(lua_State* L, T&& item) {
|
|
|
|
push(L, std::forward<T>(item));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void push_reverse(lua_State* L, const std::tuple<Args...>& tuplen) {
|
|
|
|
detail::push_tuple(L, build_reverse_indices<sizeof...(Args)>(), tuplen);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args>
|
|
|
|
inline void push_reverse(lua_State* L, std::tuple<Args...>&& tuplen) {
|
2013-12-15 12:25:44 +08:00
|
|
|
detail::push_tuple(L, build_reverse_indices<sizeof...(Args)>(), std::move(tuplen));
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Args, typename TFx>
|
2013-12-16 06:09:23 +08:00
|
|
|
inline auto pop_call(lua_State* L, TFx&& fx, types<Args...> t) -> decltype(detail::ltr_pop(L, std::forward<TFx>(fx), t, t)) {
|
|
|
|
return detail::ltr_pop(L, std::forward<TFx>(fx), t, t);
|
2013-12-03 03:22:51 +08:00
|
|
|
}
|
|
|
|
|
2013-12-16 05:27:20 +08:00
|
|
|
template<typename... Args, typename TFx>
|
2013-12-16 06:09:23 +08:00
|
|
|
inline auto pop_reverse_call(lua_State* L, TFx&& fx, types<Args...> t) -> decltype(detail::rtl_pop(L, std::forward<TFx>(fx), t, reversed<Args...>())) {
|
|
|
|
return detail::rtl_pop(L, std::forward<TFx>(fx), t, reversed<Args...>());
|
2013-12-16 05:27:20 +08:00
|
|
|
}
|
|
|
|
|
2013-12-15 12:31:23 +08:00
|
|
|
void push_args(lua_State*) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Arg, typename... Args>
|
|
|
|
void push_args(lua_State* L, Arg&& arg, Args&&... args) {
|
2013-12-03 12:33:23 +08:00
|
|
|
using swallow = char[];
|
2013-12-15 12:31:23 +08:00
|
|
|
stack::push(L, std::forward<Arg>(arg));
|
2013-12-12 18:07:58 +08:00
|
|
|
void(swallow{'\0', (stack::push(L, std::forward<Args>(args)), '\0')... });
|
2013-12-03 08:15:23 +08:00
|
|
|
}
|
2013-11-25 17:56:27 +08:00
|
|
|
} // stack
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_STACK_HPP
|