2016-03-25 03:45:44 +08:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
|
|
|
// Copyright (c) 2013-2016 Rapptz, ThePhD and contributors
|
|
|
|
|
|
|
|
// 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_CORE_HPP
|
|
|
|
#define SOL_STACK_CORE_HPP
|
|
|
|
|
|
|
|
#include "types.hpp"
|
|
|
|
#include "reference.hpp"
|
2016-04-01 04:16:07 +08:00
|
|
|
#include "stack_reference.hpp"
|
2016-03-25 03:45:44 +08:00
|
|
|
#include "userdata.hpp"
|
|
|
|
#include "tuple.hpp"
|
|
|
|
#include "traits.hpp"
|
2016-04-24 22:09:05 +08:00
|
|
|
#include "tie.hpp"
|
2016-03-25 03:45:44 +08:00
|
|
|
|
|
|
|
namespace sol {
|
|
|
|
namespace detail {
|
2016-04-07 17:21:49 +08:00
|
|
|
struct as_reference_tag{};
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
using special_destruct_func = void(*)(void*);
|
|
|
|
|
|
|
|
template <typename T, typename Real>
|
|
|
|
inline void special_destruct(void* memory) {
|
|
|
|
T** pointerpointer = static_cast<T**>(memory);
|
|
|
|
special_destruct_func* dx = static_cast<special_destruct_func*>(static_cast<void*>(pointerpointer + 1));
|
|
|
|
Real* target = static_cast<Real*>(static_cast<void*>(dx + 1));
|
|
|
|
target->~Real();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline int unique_destruct(lua_State* L) {
|
|
|
|
void* memory = lua_touserdata(L, 1);
|
|
|
|
T** pointerpointer = static_cast<T**>(memory);
|
|
|
|
special_destruct_func& dx = *static_cast<special_destruct_func*>( static_cast<void*>( pointerpointer + 1 ) );
|
|
|
|
(dx)(memory);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // detail
|
|
|
|
namespace stack {
|
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename T, bool global = false, bool raw = false, typename = void>
|
2016-03-25 03:45:44 +08:00
|
|
|
struct field_getter;
|
2016-06-07 03:46:53 +08:00
|
|
|
template <typename T, bool global = false, bool raw = false, typename = void>
|
2016-03-25 17:27:19 +08:00
|
|
|
struct probe_field_getter;
|
2016-06-07 03:46:53 +08:00
|
|
|
template<typename T, bool global = false, bool raw = false, typename = void>
|
2016-03-30 12:31:18 +08:00
|
|
|
struct field_setter;
|
2016-03-25 03:45:44 +08:00
|
|
|
template<typename T, typename = void>
|
|
|
|
struct getter;
|
|
|
|
template<typename T, typename = void>
|
|
|
|
struct popper;
|
|
|
|
template<typename T, typename = void>
|
|
|
|
struct pusher;
|
|
|
|
template<typename T, type = lua_type_of<T>::value, typename = void>
|
|
|
|
struct checker;
|
|
|
|
template<typename T, typename = void>
|
|
|
|
struct check_getter;
|
|
|
|
|
2016-03-25 17:27:19 +08:00
|
|
|
struct probe {
|
|
|
|
bool success;
|
|
|
|
int levels;
|
|
|
|
|
|
|
|
probe(bool s, int l) : success(s), levels(l) {}
|
|
|
|
|
|
|
|
operator bool() const { return success; };
|
|
|
|
};
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
namespace stack_detail {
|
|
|
|
template <typename T>
|
|
|
|
struct strip {
|
|
|
|
typedef T type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
|
|
|
struct strip<std::reference_wrapper<T>> {
|
|
|
|
typedef T& type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
|
|
|
struct strip<non_null<T>> {
|
|
|
|
typedef T type;
|
|
|
|
};
|
|
|
|
template <typename T>
|
|
|
|
using strip_t = typename strip<T>::type;
|
|
|
|
const bool default_check_arguments =
|
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
|
|
|
true;
|
|
|
|
#else
|
|
|
|
false;
|
|
|
|
#endif
|
|
|
|
template<typename T>
|
|
|
|
inline decltype(auto) unchecked_get(lua_State* L, int index = -1) {
|
2016-05-19 14:15:42 +08:00
|
|
|
return getter<meta::unqualified_t<T>>{}.get(L, index);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
} // stack_detail
|
|
|
|
|
2016-03-25 17:27:19 +08:00
|
|
|
inline bool maybe_indexable(lua_State* L, int index = -1) {
|
|
|
|
type t = type_of(L, index);
|
|
|
|
return t == type::userdata || t == type::table;
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline int push(lua_State* L, T&& t, Args&&... args) {
|
2016-05-19 14:15:42 +08:00
|
|
|
return pusher<meta::unqualified_t<T>>{}.push(L, std::forward<T>(t), std::forward<Args>(args)...);
|
2016-03-25 03:45:44 +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-05-19 14:15:42 +08:00
|
|
|
return pusher<meta::unqualified_t<T>>{}.push(L, std::forward<Arg>(arg), std::forward<Args>(args)...);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-05-02 05:10:41 +08:00
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline int push_reference(lua_State* L, T&& t, Args&&... args) {
|
2016-05-19 14:15:42 +08:00
|
|
|
typedef meta::all<
|
2016-05-02 05:10:41 +08:00
|
|
|
std::is_lvalue_reference<T>,
|
2016-05-19 14:15:42 +08:00
|
|
|
meta::neg<std::is_const<T>>,
|
|
|
|
meta::neg<is_lua_primitive<T>>
|
2016-05-02 05:10:41 +08:00
|
|
|
> use_reference_tag;
|
2016-05-19 14:15:42 +08:00
|
|
|
return pusher<std::conditional_t<use_reference_tag::value, detail::as_reference_tag, meta::unqualified_t<T>>>{}.push(L, std::forward<T>(t), std::forward<Args>(args)...);
|
2016-05-02 05:10:41 +08:00
|
|
|
}
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
inline int multi_push(lua_State*) {
|
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename... Args>
|
|
|
|
inline int multi_push(lua_State* L, T&& t, Args&&... args) {
|
|
|
|
int pushcount = push(L, std::forward<T>(t));
|
|
|
|
void(sol::detail::swallow{(pushcount += sol::stack::push(L, std::forward<Args>(args)), 0)... });
|
|
|
|
return pushcount;
|
|
|
|
}
|
|
|
|
|
2016-05-02 05:10:41 +08:00
|
|
|
inline int multi_push_reference(lua_State*) {
|
|
|
|
// do nothing
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:21:49 +08:00
|
|
|
template<typename T, typename... Args>
|
2016-05-02 05:10:41 +08:00
|
|
|
inline int multi_push_reference(lua_State* L, T&& t, Args&&... args) {
|
|
|
|
int pushcount = push_reference(L, std::forward<T>(t));
|
|
|
|
void(sol::detail::swallow{(pushcount += sol::stack::push_reference(L, std::forward<Args>(args)), 0)... });
|
|
|
|
return pushcount;
|
2016-04-07 17:21:49 +08:00
|
|
|
}
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
template <typename T, typename Handler>
|
|
|
|
bool check(lua_State* L, int index, Handler&& handler) {
|
2016-05-19 14:15:42 +08:00
|
|
|
typedef meta::unqualified_t<T> Tu;
|
2016-03-25 03:45:44 +08:00
|
|
|
checker<Tu> c;
|
|
|
|
// VC++ has a bad warning here: shut it up
|
|
|
|
(void)c;
|
|
|
|
return c.check(L, index, std::forward<Handler>(handler));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool check(lua_State* L, int index = -1) {
|
|
|
|
auto handler = no_panic;
|
|
|
|
return check<T>(L, index, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename Handler>
|
|
|
|
inline decltype(auto) check_get(lua_State* L, int index, Handler&& handler) {
|
2016-05-19 14:15:42 +08:00
|
|
|
return check_getter<meta::unqualified_t<T>>{}.get(L, index, std::forward<Handler>(handler));
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline decltype(auto) check_get(lua_State* L, int index = -1) {
|
|
|
|
auto handler = no_panic;
|
|
|
|
return check_get<T>(L, index, handler);
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:39:54 +08:00
|
|
|
namespace stack_detail {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
inline decltype(auto) tagged_get(types<T>, lua_State* L, int index = -1) {
|
2016-03-25 03:45:44 +08:00
|
|
|
#ifdef SOL_CHECK_ARGUMENTS
|
|
|
|
auto op = check_get<T>(L, index, type_panic);
|
2016-05-19 14:15:42 +08:00
|
|
|
typedef typename meta::unqualified_t<decltype(op)>::value_type U;
|
2016-03-25 03:45:44 +08:00
|
|
|
return static_cast<U>(*op);
|
|
|
|
#else
|
|
|
|
return stack_detail::unchecked_get<T>(L, index);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-03-25 22:39:54 +08:00
|
|
|
template <typename T>
|
|
|
|
inline decltype(auto) tagged_get(types<optional<T>>, lua_State* L, int index = -1) {
|
|
|
|
return stack_detail::unchecked_get<optional<T>>(L, index);
|
|
|
|
}
|
|
|
|
|
2016-06-04 09:40:23 +08:00
|
|
|
template <typename T>
|
|
|
|
inline int alloc_destroy(lua_State* L) {
|
|
|
|
void* rawdata = lua_touserdata(L, up_value_index(1));
|
|
|
|
T* data = static_cast<T*>(rawdata);
|
|
|
|
std::allocator<T> alloc;
|
|
|
|
alloc.destroy(data);
|
2016-06-07 03:46:53 +08:00
|
|
|
return 0;
|
2016-06-04 09:40:23 +08:00
|
|
|
}
|
|
|
|
|
2016-03-25 22:39:54 +08:00
|
|
|
} // stack_detail
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline decltype(auto) get(lua_State* L, int index = -1) {
|
|
|
|
return stack_detail::tagged_get(types<T>(), L, index);
|
|
|
|
}
|
|
|
|
|
2016-03-25 03:45:44 +08:00
|
|
|
template<typename T>
|
|
|
|
inline decltype(auto) pop(lua_State* L) {
|
2016-05-19 14:15:42 +08:00
|
|
|
return popper<meta::unqualified_t<T>>{}.pop(L);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:32:10 +08:00
|
|
|
template <bool global = false, bool raw = false, typename Key>
|
2016-03-25 03:45:44 +08:00
|
|
|
void get_field(lua_State* L, Key&& key) {
|
2016-06-07 03:46:53 +08:00
|
|
|
field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key));
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-06-08 08:32:10 +08:00
|
|
|
template <bool global = false, bool raw = false, typename Key>
|
2016-03-25 03:45:44 +08:00
|
|
|
void get_field(lua_State* L, Key&& key, int tableindex) {
|
2016-06-07 03:46:53 +08:00
|
|
|
field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key), tableindex);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-03-25 17:27:19 +08:00
|
|
|
template <bool global = false, typename Key>
|
2016-06-07 03:46:53 +08:00
|
|
|
void raw_get_field(lua_State* L, Key&& key) {
|
|
|
|
get_field<global, true>(L, std::forward<Key>(key));
|
2016-03-25 17:27:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key>
|
2016-06-07 03:46:53 +08:00
|
|
|
void raw_get_field(lua_State* L, Key&& key, int tableindex) {
|
|
|
|
get_field<global, true>(L, std::forward<Key>(key), tableindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key>
|
|
|
|
probe probe_get_field(lua_State* L, Key&& key) {
|
|
|
|
return probe_field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key>
|
2016-03-25 17:27:19 +08:00
|
|
|
probe probe_get_field(lua_State* L, Key&& key, int tableindex) {
|
2016-06-07 03:46:53 +08:00
|
|
|
return probe_field_getter<meta::unqualified_t<Key>, global, raw>{}.get(L, std::forward<Key>(key), tableindex);
|
2016-03-25 17:27:19 +08:00
|
|
|
}
|
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template <bool global = false, typename Key>
|
|
|
|
probe probe_raw_get_field(lua_State* L, Key&& key) {
|
|
|
|
return probe_get_field<global, true>(L, std::forward<Key>(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key>
|
|
|
|
probe probe_raw_get_field(lua_State* L, Key&& key, int tableindex) {
|
|
|
|
return probe_get_field<global, true>(L, std::forward<Key>(key), tableindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, bool raw = false, typename Key, typename Value>
|
2016-03-25 03:45:44 +08:00
|
|
|
void set_field(lua_State* L, Key&& key, Value&& value) {
|
2016-06-07 03:46:53 +08:00
|
|
|
field_setter<meta::unqualified_t<Key>, global, raw>{}.set(L, std::forward<Key>(key), std::forward<Value>(value));
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
|
2016-06-07 03:46:53 +08:00
|
|
|
template <bool global = false, bool raw = false, typename Key, typename Value>
|
2016-03-25 03:45:44 +08:00
|
|
|
void set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
2016-06-07 03:46:53 +08:00
|
|
|
field_setter<meta::unqualified_t<Key>, global, raw>{}.set(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key, typename Value>
|
|
|
|
void raw_set_field(lua_State* L, Key&& key, Value&& value) {
|
|
|
|
set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool global = false, typename Key, typename Value>
|
|
|
|
void raw_set_field(lua_State* L, Key&& key, Value&& value, int tableindex) {
|
|
|
|
set_field<global, true>(L, std::forward<Key>(key), std::forward<Value>(value), tableindex);
|
2016-03-25 03:45:44 +08:00
|
|
|
}
|
|
|
|
} // stack
|
|
|
|
} // sol
|
|
|
|
|
|
|
|
#endif // SOL_STACK_CORE_HPP
|