mirror of
https://github.com/ThePhD/sol2.git
synced 2024-03-22 13:10:44 +08:00
Huge improvements to the library and fixes to compile in g++.
usertype now respects factory functions and does not make default constructors/destructors unless the compiler says its okay new and __gc functions can be overridden for usertypes to provide handle-like creation and deletion functions Overloading match fixes RAII improvements for all usertypes Added tests to make sure these features stay
This commit is contained in:
parent
b2b73db5cb
commit
019c7b037b
|
@ -323,7 +323,7 @@ struct pusher<function_sig<Sigs...>> {
|
|||
int metapushed = luaL_newmetatable(L, metatablename);
|
||||
if(metapushed == 1) {
|
||||
lua_pushstring(L, "__gc");
|
||||
stack::push<lua_CFunction>(L, detail::gc);
|
||||
stack::push(L, detail::gc);
|
||||
lua_settable(L, -3);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
|
|
|
@ -28,5 +28,6 @@
|
|||
#include "function_types_member.hpp"
|
||||
#include "function_types_usertype.hpp"
|
||||
#include "function_types_overload.hpp"
|
||||
#include "function_types_allocator.hpp"
|
||||
|
||||
#endif // SOL_FUNCTION_TYPES_HPP
|
||||
|
|
|
@ -22,9 +22,126 @@
|
|||
#ifndef SOL_FUNCTION_TYPES_ALLOCATOR_HPP
|
||||
#define SOL_FUNCTION_TYPES_ALLOCATOR_HPP
|
||||
|
||||
#include "stack.hpp"
|
||||
#include "raii.hpp"
|
||||
#include "function_types_overload.hpp"
|
||||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
template <typename T, typename List>
|
||||
struct void_call;
|
||||
|
||||
template <typename T, typename... Args>
|
||||
struct void_call<T, types<Args...>> {
|
||||
static void call(Args... args) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct constructor_match {
|
||||
T* obj;
|
||||
|
||||
constructor_match(T* obj) : obj(obj) {}
|
||||
|
||||
template <bool b, typename Fx, std::size_t I, typename... R, typename... Args>
|
||||
int operator()(Bool<b>, types<Fx>, Index<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start) const {
|
||||
default_construct func{};
|
||||
return stack::typed_call<b ? false : stack::stack_detail::default_check_arguments>(r, a, func, L, start, obj);
|
||||
}
|
||||
};
|
||||
} // detail
|
||||
|
||||
template <typename T, typename... TypeLists, typename Match>
|
||||
inline int construct(Match&& matchfx, lua_State* L, int fxarity, int start) {
|
||||
// use same overload resolution matching as all other parts of the framework
|
||||
return overload_match_arity<decltype(detail::void_call<T, TypeLists>::call)...>(std::forward<Match>(matchfx), L, fxarity, start);
|
||||
}
|
||||
|
||||
template <typename T, typename... TypeLists>
|
||||
inline int construct(lua_State* L) {
|
||||
static const auto& meta = usertype_traits<T>::metatable;
|
||||
call_syntax syntax = stack::get_call_syntax(L, meta);
|
||||
int argcount = lua_gettop(L) - static_cast<int>(syntax);
|
||||
|
||||
T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
||||
T*& referencepointer = *pointerpointer;
|
||||
T* obj = reinterpret_cast<T*>(pointerpointer + 1);
|
||||
referencepointer = obj;
|
||||
reference userdataref(L, -1);
|
||||
userdataref.pop();
|
||||
|
||||
construct<T, TypeLists...>(detail::constructor_match<T>(obj), L, argcount, 1 + static_cast<int>(syntax));
|
||||
|
||||
userdataref.push();
|
||||
luaL_getmetatable(L, &meta[0]);
|
||||
if (stack::get<type>(L) == type::nil) {
|
||||
lua_pop(L, 1);
|
||||
std::string err = "unable to get usertype metatable for ";
|
||||
err += meta;
|
||||
throw error(err);
|
||||
}
|
||||
|
||||
lua_setmetatable(L, -2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline int destruct(lua_State* L) {
|
||||
userdata udata = stack::get<userdata>(L, 1);
|
||||
// The first sizeof(T*) bytes are the reference: the rest is
|
||||
// the actual data itself (if there is a reference at all)
|
||||
T** pobj = reinterpret_cast<T**>(udata.value);
|
||||
T*& obj = *pobj;
|
||||
std::allocator<T> alloc{};
|
||||
alloc.destroy(obj);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template <typename T, typename... Functions>
|
||||
struct usertype_constructor_function : base_function {
|
||||
typedef std::tuple<Functions...> overload_list;
|
||||
typedef std::index_sequence_for<Functions...> indices;
|
||||
overload_list overloads;
|
||||
|
||||
usertype_constructor_function(constructor_wrapper<Functions...> set) : usertype_constructor_function(indices(), set) {}
|
||||
|
||||
template <std::size_t... I>
|
||||
usertype_constructor_function(std::index_sequence<I...>, constructor_wrapper<Functions...> set) : usertype_constructor_function(std::get<I>(set)...) {}
|
||||
|
||||
usertype_constructor_function(Functions... fxs) : overloads(fxs...) {}
|
||||
|
||||
template <bool b, typename Fx, std::size_t I, typename... R, typename... Args>
|
||||
int call(Bool<b>, types<Fx>, Index<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start) {
|
||||
static const auto& meta = usertype_traits<T>::metatable;
|
||||
T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
||||
T*& referencepointer = *pointerpointer;
|
||||
T* obj = reinterpret_cast<T*>(pointerpointer + 1);
|
||||
referencepointer = obj;
|
||||
reference userdataref(L, -1);
|
||||
userdataref.pop();
|
||||
|
||||
auto& func = std::get<I>(overloads);
|
||||
stack::typed_call<b ? false : stack::stack_detail::default_check_arguments>(r, a, func, L, start, detail::implicit_wrapper<T>(obj));
|
||||
|
||||
userdataref.push();
|
||||
luaL_getmetatable(L, &meta[0]);
|
||||
if (stack::get<type>(L) == type::nil) {
|
||||
lua_pop(L, 1);
|
||||
std::string err = "unable to get usertype metatable for ";
|
||||
err += meta;
|
||||
throw error(err);
|
||||
}
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
static const auto& meta = usertype_traits<T>::metatable;
|
||||
call_syntax syntax = stack::get_call_syntax(L, meta);
|
||||
int argcount = lua_gettop(L) - static_cast<int>(syntax);
|
||||
auto mfx = [&](auto&&... args) { return this->call(std::forward<decltype(args)>(args)...); };
|
||||
return construct<T, pop_front_type_t<function_args_t<Functions>>...>(mfx, L, argcount, 1 + static_cast<int>(syntax));
|
||||
}
|
||||
};
|
||||
} // sol
|
||||
|
||||
#endif // SOL_FUNCTION_TYPES_ALLOCATOR_HPP
|
||||
|
|
|
@ -31,6 +31,7 @@ struct ref_call_t {} const ref_call = ref_call_t{};
|
|||
template <typename T>
|
||||
struct implicit_wrapper {
|
||||
T& item;
|
||||
implicit_wrapper(T* item) : item(*item) {}
|
||||
implicit_wrapper(T& item) : item(item) {}
|
||||
operator T& () {
|
||||
return item;
|
||||
|
@ -129,7 +130,7 @@ struct functor<T, Func, std::enable_if_t<std::is_member_object_pointer<Func>::va
|
|||
template<typename T, typename Func>
|
||||
struct functor<T, Func, std::enable_if_t<std::is_function<Func>::value || std::is_class<Func>::value>> {
|
||||
typedef callable_traits<Func> traits_type;
|
||||
typedef remove_one_type<typename traits_type::args_type> args_type;
|
||||
typedef pop_front_type_t<typename traits_type::args_type> args_type;
|
||||
typedef typename traits_type::return_type return_type;
|
||||
typedef std::tuple_element_t<0, typename traits_type::args_tuple_type> Arg0;
|
||||
typedef std::conditional_t<std::is_pointer<Func>::value || std::is_class<Func>::value, Func, std::add_pointer_t<Func>> function_type;
|
||||
|
@ -212,9 +213,10 @@ namespace detail {
|
|||
|
||||
template <std::size_t limit>
|
||||
static void func_gc(std::false_type, lua_State* L) {
|
||||
// Shut up clang tautological error without throwing out std::size_t
|
||||
for (std::size_t i = 0; i < limit; ++i) {
|
||||
upvalue up = stack::get<upvalue>(L, static_cast<int>(i + 1));
|
||||
if (up.value == nullptr)
|
||||
continue;
|
||||
base_function* obj = static_cast<base_function*>(up.value);
|
||||
std::allocator<base_function> alloc{};
|
||||
alloc.destroy(obj);
|
||||
|
@ -243,8 +245,7 @@ namespace detail {
|
|||
func_gc<I>(Bool<(I < 1)>(), L);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
} // detail
|
||||
} // sol
|
||||
|
||||
#endif // SOL_FUNCTION_TYPES_CORE_HPP
|
||||
|
|
|
@ -28,45 +28,46 @@
|
|||
|
||||
namespace sol {
|
||||
namespace detail {
|
||||
template <std::size_t... M, typename Match>
|
||||
inline int match_arity(types<>, std::index_sequence<>, std::index_sequence<M...>, std::ptrdiff_t, lua_State*, Match&&, int) {
|
||||
template <std::size_t... M, typename Match, typename... Args>
|
||||
inline int overload_match_arity(types<>, std::index_sequence<>, std::index_sequence<M...>, Match&&, lua_State*, int, int, Args&&...) {
|
||||
throw error("no matching function call takes this number of arguments and the specified types");
|
||||
}
|
||||
|
||||
template <typename Fx, typename... Fxs, std::size_t I, std::size_t... In, std::size_t... M, typename Match>
|
||||
inline int match_arity(types<Fx, Fxs...>, std::index_sequence<I, In...>, std::index_sequence<M...>, std::ptrdiff_t fxarity, lua_State* L, Match&& matchfx, int start) {
|
||||
template <typename Fx, typename... Fxs, std::size_t I, std::size_t... In, std::size_t... M, typename Match, typename... Args>
|
||||
inline int overload_match_arity(types<Fx, Fxs...>, std::index_sequence<I, In...>, std::index_sequence<M...>, Match&& matchfx, lua_State* L, int fxarity, int start, Args&&... args) {
|
||||
typedef function_traits<Fx> traits;
|
||||
typedef tuple_types<typename function_traits<Fx>::return_type> return_types;
|
||||
typedef typename function_traits<Fx>::args_type args_type;
|
||||
typedef typename args_type::indices args_indices;
|
||||
// compile-time eliminate any functions that we know ahead of time are of improper arity
|
||||
if (find_in_pack_v<Index<traits::arity>, Index<M>...>::value || traits::arity != fxarity) {
|
||||
return match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<M...>(), fxarity, L, std::forward<Match>(matchfx), start);
|
||||
if (find_in_pack_v<Index<traits::arity>, Index<M>...>::value) {
|
||||
return overload_match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
|
||||
}
|
||||
if (traits::arity != fxarity) {
|
||||
return match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<traits::arity, M...>(), fxarity, L, std::forward<Match>(matchfx), start);
|
||||
return overload_match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<traits::arity, M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
|
||||
}
|
||||
if (!detail::check_types(args_type(), args_indices(), L, start)) {
|
||||
return match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<M...>(), fxarity, L, std::forward<Match>(matchfx), start);
|
||||
if (sizeof...(Fxs) != 0 && !detail::check_types(args_type(), args_indices(), L, start)) {
|
||||
return overload_match_arity(types<Fxs...>(), std::index_sequence<In...>(), std::index_sequence<M...>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
|
||||
}
|
||||
return matchfx(Index<I>(), fxarity, L, start);
|
||||
return matchfx(Bool<sizeof...(Fxs) != 0>(), types<Fx>(), Index<I>(), return_types(), args_type(), L, fxarity, start, std::forward<Args>(args)...);
|
||||
}
|
||||
} // detail
|
||||
|
||||
template <typename... Functions, typename Match>
|
||||
inline int match(std::ptrdiff_t fxarity, lua_State* L, Match&& matchfx, int start = 1) {
|
||||
return detail::match_arity(types<Functions...>(), std::index_sequence_for<Functions...>(), std::index_sequence<>(), fxarity, L, std::forward<Match>(matchfx), start);
|
||||
template <typename... Functions, typename Match, typename... Args>
|
||||
inline int overload_match_arity(Match&& matchfx, lua_State* L, int fxarity, int start = 1, Args&&... args) {
|
||||
return detail::overload_match_arity(types<Functions...>(), std::index_sequence_for<Functions...>(), std::index_sequence<>(), std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Functions, typename Match>
|
||||
inline int match(lua_State* L, Match&& matchfx, int start = 1) {
|
||||
std::ptrdiff_t fxarity = lua_gettop(L) - (start - 1);
|
||||
return match<Functions...>(fxarity, L, std::forward<Match>(matchfx), start);
|
||||
template <typename... Functions, typename Match, typename... Args>
|
||||
inline int overload_match(Match&& matchfx, lua_State* L, int start = 1, Args&&... args) {
|
||||
int fxarity = lua_gettop(L) - (start - 1);
|
||||
return overload_match_arity<Functions...>(std::forward<Match>(matchfx), L, fxarity, start, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template <typename... Functions>
|
||||
struct overloaded_function : base_function {
|
||||
typedef std::tuple<Functions...> overload_list;
|
||||
typedef std::index_sequence_for<Functions...> indices;
|
||||
overload_list overloads;
|
||||
|
||||
overloaded_function(overload_set<Functions...> set)
|
||||
|
@ -74,26 +75,22 @@ struct overloaded_function : base_function {
|
|||
|
||||
template <std::size_t... I>
|
||||
overloaded_function(std::index_sequence<I...>, overload_set<Functions...> set)
|
||||
: overloaded_function(std::get<In>(set)...) {}
|
||||
: overloaded_function(std::get<I>(set)...) {}
|
||||
|
||||
overloaded_function(Functions... fxs)
|
||||
: overloads(fxs...) {
|
||||
|
||||
}
|
||||
|
||||
template <std::size_t I>
|
||||
int call(Index<I>, int, lua_State* L, int) {
|
||||
template <bool b, typename Fx, std::size_t I, typename... R, typename... Args>
|
||||
int call(Bool<b>, types<Fx>, Index<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start) {
|
||||
auto& func = std::get<I>(overloads);
|
||||
typedef Unqualified<decltype(func)> Fx;
|
||||
typedef function_traits<Fx> traits;
|
||||
typedef tuple_types<typename function_traits<Fx>::return_type> return_types;
|
||||
typedef typename function_traits<Fx>::args_type args_type;
|
||||
return stack::typed_call<false>(return_types(), args_type(), func, L);
|
||||
return stack::typed_call<b ? false : stack::stack_detail::default_check_arguments>(r, a, func, L, start);
|
||||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
auto mfx = [&](auto&&... args){ return call(std::forward<decltype(args)>(args)...); };
|
||||
return match<Functions...>(L, mfx);
|
||||
auto mfx = [&](auto&&... args){ return this->call(std::forward<decltype(args)>(args)...); };
|
||||
return overload_match<Functions...>(mfx, L);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -110,20 +107,16 @@ struct usertype_overloaded_function : base_function {
|
|||
|
||||
usertype_overloaded_function(Functions... fxs) : overloads(fxs...) {}
|
||||
|
||||
template <std::size_t I>
|
||||
int call(Index<I>, int, lua_State* L, int) {
|
||||
template <bool b,typename Fx, std::size_t I, typename... R, typename... Args>
|
||||
int call(Bool<b>, types<Fx>, Index<I>, types<R...> r, types<Args...> a, lua_State* L, int, int start) {
|
||||
auto& func = std::get<I>(overloads);
|
||||
typedef Unqualified<decltype(func)> Fx;
|
||||
typedef typename Fx::traits_type traits;
|
||||
typedef tuple_types<typename Fx::return_type> return_types;
|
||||
typedef typename Fx::args_type args_type;
|
||||
func.item = ptr(stack::get<T>(L, 1));
|
||||
return stack::typed_call<false>(return_types(), args_type(), func, L);
|
||||
return stack::typed_call<b ? false : stack::stack_detail::default_check_arguments>(r, a, func, L, start);
|
||||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
auto mfx = [&](auto&&... args){ return call(std::forward<decltype(args)>(args)...); };
|
||||
return match<Functions...>(L, mfx, 2);
|
||||
auto mfx = [&](auto&&... args){ return this->call(std::forward<decltype(args)>(args)...); };
|
||||
return overload_match<Functions...>(mfx, L, 2);
|
||||
}
|
||||
|
||||
};
|
||||
|
|
|
@ -62,18 +62,17 @@ struct usertype_function_core : public base_function {
|
|||
return stack::push(L, std::forward<Return>(r));
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
int operator()(types<void> tr, types<Args...> ta, lua_State* L) {
|
||||
//static const std::size_t skew = static_cast<std::size_t>(std::is_member_object_pointer<function_type>::value);
|
||||
stack::call(tr, ta, L, 0, fx);
|
||||
template<typename... Args, std::size_t Start>
|
||||
int operator()(types<void> tr, types<Args...> ta, Index<Start>, lua_State* L) {
|
||||
stack::call(tr, ta, L, static_cast<int>(Start), fx);
|
||||
int nargs = static_cast<int>(sizeof...(Args));
|
||||
lua_pop(L, nargs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename... Ret, typename... Args>
|
||||
int operator()(types<Ret...> tr, types<Args...> ta, lua_State* L) {
|
||||
decltype(auto) r = stack::call(tr, ta, L, 0, fx);
|
||||
template<typename... Ret, typename... Args, std::size_t Start>
|
||||
int operator()(types<Ret...> tr, types<Args...> ta, Index<Start>, lua_State* L) {
|
||||
decltype(auto) r = stack::call(tr, ta, L, static_cast<int>(Start), fx);
|
||||
int nargs = static_cast<int>(sizeof...(Args));
|
||||
lua_pop(L, nargs);
|
||||
int pushcount = push(L, std::forward<decltype(r)>(r));
|
||||
|
@ -97,7 +96,7 @@ struct usertype_function : public usertype_function_core<Function, Tp> {
|
|||
if(this->fx.item == nullptr) {
|
||||
throw error("userdata for function call is null: are you using the wrong syntax? (use item:function/variable(...) syntax)");
|
||||
}
|
||||
return static_cast<base_t&>(*this)(tuple_types<return_type>(), args_type(), L);
|
||||
return static_cast<base_t&>(*this)(tuple_types<return_type>(), args_type(), Index<2>(), L);
|
||||
}
|
||||
|
||||
virtual int operator()(lua_State* L) override {
|
||||
|
@ -124,9 +123,9 @@ struct usertype_variable_function : public usertype_function_core<Function, Tp>
|
|||
}
|
||||
switch(argcount) {
|
||||
case 2:
|
||||
return static_cast<base_t&>(*this)(tuple_types<return_type>(), types<>(), L);
|
||||
return static_cast<base_t&>(*this)(tuple_types<return_type>(), types<>(), Index<2>(), L);
|
||||
case 3:
|
||||
return static_cast<base_t&>(*this)(tuple_types<void>(), args_type(), L);
|
||||
return static_cast<base_t&>(*this)(tuple_types<void>(), args_type(), Index<3>(), L);
|
||||
default:
|
||||
throw error("cannot get/set userdata member variable with inappropriate number of arguments");
|
||||
}
|
||||
|
|
84
sol/raii.hpp
Normal file
84
sol/raii.hpp
Normal file
|
@ -0,0 +1,84 @@
|
|||
// The MIT License (MIT)
|
||||
|
||||
// Copyright (c) 2013-2016 Rapptz 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_RAII_HPP
|
||||
#define SOL_RAII_HPP
|
||||
|
||||
#include <memory>
|
||||
#include "traits.hpp"
|
||||
|
||||
namespace sol {
|
||||
struct default_construct {
|
||||
template<typename T, typename... Args>
|
||||
void operator()(T&& obj, Args&&... args) const {
|
||||
std::allocator<Unqualified<T>> alloc{};
|
||||
alloc.construct(obj, std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct placement_construct {
|
||||
T obj;
|
||||
|
||||
template <typename... Args>
|
||||
placement_construct( Args&&... args ) : obj(std::forward<Args>(args)...) {}
|
||||
|
||||
template<typename... Args>
|
||||
void operator()(Args&&... args) const {
|
||||
default_construct{}(obj, std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename... Args>
|
||||
using constructors = sol::types<Args...>;
|
||||
|
||||
const auto default_constructor = constructors<types<>>{};
|
||||
|
||||
template <typename... Functions>
|
||||
struct constructor_wrapper : std::tuple<Functions...> {
|
||||
using std::tuple<Functions...>::tuple;
|
||||
};
|
||||
|
||||
template <typename... Functions>
|
||||
constructor_wrapper<Functions...> constructor(Functions&&... functions) {
|
||||
return constructor_wrapper<Functions...>(std::forward<Functions>(functions)...);
|
||||
}
|
||||
|
||||
template <typename Function>
|
||||
struct destructor_wrapper {
|
||||
Function fx;
|
||||
template <typename... Args>
|
||||
destructor_wrapper(Args&&... args) : fx(std::forward<Args>(args)...) {}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct destructor_wrapper<void> {};
|
||||
|
||||
const destructor_wrapper<void> default_destructor{};
|
||||
|
||||
template <typename Fx>
|
||||
inline destructor_wrapper<Fx> destructor(Fx&& fx) {
|
||||
return destructor_wrapper<Fx>(std::forward<Fx>(fx));
|
||||
}
|
||||
|
||||
} // sol
|
||||
|
||||
#endif // SOL_RAII_HPP
|
|
@ -539,7 +539,7 @@ struct pusher<std::reference_wrapper<T>> {
|
|||
|
||||
template<>
|
||||
struct pusher<bool> {
|
||||
static int push(lua_State* L, const bool& b) {
|
||||
static int push(lua_State* L, bool b) {
|
||||
lua_pushboolean(L, b);
|
||||
return 1;
|
||||
}
|
||||
|
@ -547,12 +547,20 @@ struct pusher<bool> {
|
|||
|
||||
template<>
|
||||
struct pusher<nil_t> {
|
||||
static int push(lua_State* L, const nil_t&) {
|
||||
static int push(lua_State* L, nil_t) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct pusher<lua_CFunction> {
|
||||
static int push(lua_State* L, lua_CFunction func, int n = 0) {
|
||||
|
@ -759,7 +767,6 @@ template <bool b>
|
|||
struct check_arguments {
|
||||
template <std::size_t I0, std::size_t... I, typename Arg0, typename... Args>
|
||||
static bool check(types<Arg0, Args...>, std::index_sequence<I0, I...>, lua_State* L, int firstargument) {
|
||||
bool checks = true;
|
||||
if (!stack::check<Arg0>(L, firstargument + I0))
|
||||
return false;
|
||||
return check(types<Args...>(), std::index_sequence<I...>(), L, firstargument);
|
||||
|
@ -780,24 +787,14 @@ struct check_arguments<false> {
|
|||
|
||||
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>>
|
||||
inline R call(types<R>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
|
||||
const int stacksize = lua_gettop(L);
|
||||
const int firstargument = static_cast<int>(start + stacksize - std::max(sizeof...(Args)-1, static_cast<std::size_t>(0)));
|
||||
|
||||
check_arguments<checkargs>{}.check(ta, tai, L, firstargument);
|
||||
|
||||
return fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, firstargument + I)...);
|
||||
check_arguments<checkargs>{}.check(ta, tai, L, start);
|
||||
return fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, start + I)...);
|
||||
}
|
||||
|
||||
template <bool checkargs = default_check_arguments, std::size_t... I, typename... Args, typename Fx, typename... FxArgs>
|
||||
inline void call(types<void>, types<Args...> ta, std::index_sequence<I...> tai, lua_State* L, int start, Fx&& fx, FxArgs&&... args) {
|
||||
const int stacksize = lua_gettop(L);
|
||||
const int firstargument = static_cast<int>(start + stacksize - std::max(sizeof...(Args)-1, static_cast<std::size_t>(0)));
|
||||
|
||||
bool checks = check_arguments<checkargs>{}.check(ta, tai, L, firstargument);
|
||||
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)...);
|
||||
check_arguments<checkargs>{}.check(ta, tai, L, start);
|
||||
fx(std::forward<FxArgs>(args)..., stack::get<Args>(L, start + I)...);
|
||||
}
|
||||
} // stack_detail
|
||||
|
||||
|
@ -831,7 +828,12 @@ inline R call(types<R> tr, types<Args...> ta, lua_State* L, int start, Fx&& fx,
|
|||
|
||||
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(types<R> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
||||
return call<check_args>(tr, ta, L, 0, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
||||
return call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
||||
}
|
||||
|
||||
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 top_call(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)...);
|
||||
}
|
||||
|
||||
template <bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
||||
|
@ -842,20 +844,25 @@ inline void call(types<void> tr, types<Args...> ta, lua_State* L, int start, Fx&
|
|||
|
||||
template <bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
||||
inline void call(types<void> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
||||
call<check_args>(tr, ta, L, 0, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
||||
call<check_args>(tr, ta, L, 1, std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
||||
}
|
||||
|
||||
template<bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx>
|
||||
inline int typed_call(types<void> tr, types<Args...> ta, Fx&& fx, lua_State* L, int start = 0) {
|
||||
call<check_args>(tr, ta, L, start, fx);
|
||||
template <bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
||||
inline void top_call(types<void> tr, types<Args...> ta, lua_State* L, Fx&& fx, FxArgs&&... args) {
|
||||
call<check_args>(tr, ta, L, static_cast<int>(lua_gettop(L) - sizeof...(Args)), std::forward<Fx>(fx), std::forward<FxArgs>(args)...);
|
||||
}
|
||||
|
||||
template<bool check_args = stack_detail::default_check_arguments, typename... Args, typename Fx, typename... FxArgs>
|
||||
inline int typed_call(types<void> tr, types<Args...> ta, Fx&& fx, lua_State* L, int start = 1, FxArgs&&... fxargs) {
|
||||
call<check_args>(tr, ta, L, start, fx, std::forward<FxArgs>(fxargs)...);
|
||||
int nargs = static_cast<int>(sizeof...(Args));
|
||||
lua_pop(L, nargs);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<bool check_args = stack_detail::default_check_arguments, typename... Ret, typename... Args, typename Fx>
|
||||
inline int typed_call(types<Ret...>, types<Args...> ta, Fx&& fx, lua_State* L, int start = 0) {
|
||||
decltype(auto) r = call<check_args>(types<ReturnType<Ret...>>(), ta, L, start, fx);
|
||||
template<bool check_args = stack_detail::default_check_arguments, typename Ret0, typename... Ret, typename... Args, typename Fx, typename... FxArgs, typename = std::enable_if_t<Not<std::is_void<Ret0>>::value>>
|
||||
inline int typed_call(types<Ret0, Ret...>, types<Args...> ta, Fx&& fx, lua_State* L, int start = 1, FxArgs&&... fxargs) {
|
||||
decltype(auto) r = call<check_args>(types<return_type_t<Ret0, Ret...>>(), ta, L, start, fx, std::forward<FxArgs>(fxargs)...);
|
||||
int nargs = static_cast<int>(sizeof...(Args));
|
||||
lua_pop(L, nargs);
|
||||
return push(L, std::forward<decltype(r)>(r));
|
||||
|
|
|
@ -177,9 +177,16 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
template<typename Class, typename... CTor, typename... Args>
|
||||
template<typename Class, typename... Args>
|
||||
state_view& new_usertype(const std::string& name, Args&&... args) {
|
||||
constructors<types<CTor...>> ctor{};
|
||||
usertype<Class> utype(std::forward<Args>(args)...);
|
||||
set_usertype(name, utype);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename Class, typename CTor0, typename... CTor, typename... Args>
|
||||
state_view& new_usertype(const std::string& name, Args&&... args) {
|
||||
constructors<types<CTor0, CTor...>> ctor{};
|
||||
return new_usertype<Class>(name, ctor, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
|
|
@ -125,6 +125,15 @@ struct find_in_pack_v : Bool<false> { };
|
|||
template<typename V, typename Vs1, typename... Vs>
|
||||
struct find_in_pack_v<V, Vs1, Vs...> : Or<Bool<(V::value == Vs1::value)>, find_in_pack_v<V, Vs...>> { };
|
||||
|
||||
template<std::size_t I, typename... Args>
|
||||
struct at_in_pack {};
|
||||
|
||||
template<std::size_t I, typename... Args>
|
||||
using at_in_pack_t = typename at_in_pack<I, Args...>::type;
|
||||
|
||||
template<std::size_t I, typename Arg, typename... Args>
|
||||
struct at_in_pack<I, Arg, Args...> : std::conditional<I == 0, Arg, at_in_pack_t<I - 1, Args...>> {};
|
||||
|
||||
template<typename... Args>
|
||||
struct return_type {
|
||||
typedef std::tuple<Args...> type;
|
||||
|
@ -143,12 +152,6 @@ struct return_type<> {
|
|||
template <typename... Args>
|
||||
using return_type_t = typename return_type<Args...>::type;
|
||||
|
||||
template <typename Empty, typename... Args>
|
||||
using ReturnTypeOr = typename std::conditional<(sizeof...(Args) < 1), Empty, return_type_t<Args...>>::type;
|
||||
|
||||
template <typename... Args>
|
||||
using ReturnType = ReturnTypeOr<void, Args...>;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T, bool isclass = std::is_class<Unqualified<T>>::value>
|
||||
|
|
|
@ -30,15 +30,6 @@ template<typename... Args>
|
|||
struct types { typedef std::index_sequence_for<Args...> indices; static constexpr std::size_t size() { return sizeof...(Args); } };
|
||||
|
||||
namespace detail {
|
||||
template<typename Arg>
|
||||
struct chop_one : types<> {};
|
||||
|
||||
template<typename Arg0, typename Arg1, typename... Args>
|
||||
struct chop_one<types<Arg0, Arg1, Args...>> : types<Arg1, Args...> {};
|
||||
|
||||
template<typename Arg, typename... Args>
|
||||
struct chop_one<types<Arg, Args...>> : types<Args...> {};
|
||||
|
||||
template<typename... Args>
|
||||
struct tuple_types_ { typedef types<Args...> type; };
|
||||
|
||||
|
@ -53,12 +44,15 @@ template<typename... Args>
|
|||
using tuple_types = typename detail::tuple_types_<Args...>::type;
|
||||
|
||||
template<typename Arg>
|
||||
struct remove_one_type : detail::chop_one<Arg> {};
|
||||
struct pop_front_type;
|
||||
|
||||
template<typename Arg>
|
||||
using pop_front_type_t = typename pop_front_type<Arg>::type;
|
||||
|
||||
template<typename Arg, typename... Args>
|
||||
struct pop_front_type<types<Arg, Args...>> { typedef types<Args...> type; };
|
||||
|
||||
template<typename... Args>
|
||||
using constructors = sol::types<Args...>;
|
||||
|
||||
const auto default_constructor = constructors<types<>>{};
|
||||
|
||||
} // sol
|
||||
|
||||
|
|
257
sol/usertype.hpp
257
sol/usertype.hpp
|
@ -25,7 +25,7 @@
|
|||
#include "state.hpp"
|
||||
#include "function_types.hpp"
|
||||
#include "usertype_traits.hpp"
|
||||
#include "default_construct.hpp"
|
||||
#include "raii.hpp"
|
||||
#include "deprecate.hpp"
|
||||
#include <vector>
|
||||
#include <array>
|
||||
|
@ -88,8 +88,34 @@ namespace sol {
|
|||
};
|
||||
|
||||
namespace usertype_detail {
|
||||
template<typename T, typename Funcs, typename FuncTable, typename MetaFuncTable>
|
||||
inline void push_metatable(lua_State* L, Funcs&& funcs, FuncTable&&, MetaFuncTable&& metafunctable) {
|
||||
struct add_destructor_tag {};
|
||||
struct check_destructor_tag {};
|
||||
struct verified_tag {} const verified {};
|
||||
|
||||
template <typename T>
|
||||
struct is_constructor : std::false_type {};
|
||||
|
||||
template <typename... Args>
|
||||
struct is_constructor<constructors<Args...>> : std::true_type {};
|
||||
|
||||
template <typename... Args>
|
||||
struct is_constructor<constructor_wrapper<Args...>> : std::true_type {};
|
||||
|
||||
template <typename... Args>
|
||||
using has_constructor = Or<is_constructor<Unqualified<Args>>...>;
|
||||
|
||||
template <typename T>
|
||||
struct is_destructor : std::false_type {};
|
||||
|
||||
template <typename Fx>
|
||||
struct is_destructor<destructor_wrapper<Fx>> : std::true_type {};
|
||||
|
||||
template <typename... Args>
|
||||
using has_destructor = Or<is_destructor<Unqualified<Args>>...>;
|
||||
|
||||
template<typename T, bool refmeta, typename Funcs, typename FuncTable, typename MetaFuncTable>
|
||||
inline void push_metatable(lua_State* L, bool needsindexfunction, Funcs&& funcs, FuncTable&& functable, MetaFuncTable&& metafunctable) {
|
||||
static const auto& gcname = meta_function_names[static_cast<int>(meta_function::garbage_collect)];
|
||||
luaL_newmetatable(L, &usertype_traits<T>::metatable[0]);
|
||||
int metatableindex = lua_gettop(L);
|
||||
if (funcs.size() < 1 && metafunctable.size() < 2) {
|
||||
|
@ -97,53 +123,46 @@ namespace sol {
|
|||
}
|
||||
// Metamethods directly on the metatable itself
|
||||
int metaup = stack::stack_detail::push_upvalues(L, funcs);
|
||||
if (std::is_pointer<T>::value) {
|
||||
// Insert nullptr before new/gc methods for pointer types;
|
||||
// prevents calling new/GC on pointer-based tables.
|
||||
luaL_Reg& oldref = metafunctable[metafunctable.size() - 3];
|
||||
luaL_Reg old = oldref;
|
||||
oldref = { nullptr, nullptr };
|
||||
if (refmeta && gcname == metafunctable[metafunctable.size()-2].name) {
|
||||
// We can just "clip" out the __gc function,
|
||||
// which we always put as the last entry in the meta function table.
|
||||
luaL_Reg& target = metafunctable[metafunctable.size() - 2];
|
||||
luaL_Reg old = target;
|
||||
target = { nullptr, nullptr };
|
||||
luaL_setfuncs(L, metafunctable.data(), metaup);
|
||||
oldref = old;
|
||||
target = old;
|
||||
}
|
||||
else {
|
||||
// Otherwise, just slap it in there.
|
||||
luaL_setfuncs(L, metafunctable.data(), metaup);
|
||||
}
|
||||
if (needsindexfunction) {
|
||||
// We don't need to do anything more
|
||||
// since we've already bound the __index field using
|
||||
// setfuncs above...
|
||||
return;
|
||||
}
|
||||
// Otherwise, we use quick, fast table indexing for methods
|
||||
// gives us performance boost in calling them
|
||||
lua_createtable(L, 0, functable.size());
|
||||
int up = stack::stack_detail::push_upvalues(L, funcs);
|
||||
luaL_setfuncs(L, functable.data(), up);
|
||||
lua_setfield(L, metatableindex, "__index");
|
||||
return;
|
||||
}
|
||||
|
||||
template <typename T, typename Functions>
|
||||
inline void set_global_deleter(lua_State* L, lua_CFunction cleanup, Functions&& metafunctions) {
|
||||
inline void set_global_deleter(lua_State* L, lua_CFunction cleanup, Functions&& functions) {
|
||||
// Automatic deleter table -- stays alive until lua VM dies
|
||||
// even if the user calls collectgarbage(), weirdly enough
|
||||
lua_createtable(L, 0, 0);
|
||||
lua_createtable(L, 0, 1);
|
||||
int up = stack::stack_detail::push_upvalues<true>(L, metafunctions);
|
||||
lua_pushcclosure(L, cleanup, up);
|
||||
lua_setfield(L, -2, "__gc");
|
||||
lua_createtable(L, 0, 0); // global table that sits at toplevel
|
||||
lua_createtable(L, 0, 1); // metatable for the global table
|
||||
int up = stack::stack_detail::push_upvalues<true>(L, functions);
|
||||
stack::set_field(L, "__gc", c_closure(cleanup, up));
|
||||
lua_setmetatable(L, -2);
|
||||
// gctable name by default has ♻ part of it
|
||||
lua_setglobal(L, &usertype_traits<T>::gc_table[0]);
|
||||
}
|
||||
|
||||
template<typename T, typename... Args>
|
||||
static void do_constructor(lua_State* L, T* obj, call_syntax syntax, int, types<Args...>) {
|
||||
default_construct fx{};
|
||||
stack::call(types<void>(), types<Args...>(), L, -1 + static_cast<int>(syntax), fx, obj);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void match_constructor(lua_State*, T*, call_syntax, int) {
|
||||
throw error("No matching constructor for the arguments provided");
|
||||
}
|
||||
|
||||
template<typename T, typename ...CArgs, typename... Args>
|
||||
static void match_constructor(lua_State* L, T* obj, call_syntax syntax, int argcount, types<CArgs...> t, Args&&... args) {
|
||||
if (argcount == sizeof...(CArgs)) {
|
||||
do_constructor<T>(L, obj, syntax, argcount, t);
|
||||
return;
|
||||
}
|
||||
match_constructor<T>(L, obj, syntax, argcount, std::forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -157,50 +176,20 @@ namespace sol {
|
|||
base_function* indexfunc;
|
||||
base_function* newindexfunc;
|
||||
function_map_t indexwrapper, newindexwrapper;
|
||||
base_function* constructfunc;
|
||||
base_function* destructfunc;
|
||||
lua_CFunction functiongc;
|
||||
|
||||
template<typename... TTypes>
|
||||
struct constructor {
|
||||
static int construct(lua_State* L) {
|
||||
const auto& meta = usertype_traits<T>::metatable;
|
||||
call_syntax syntax = stack::get_call_syntax(L, meta);
|
||||
int argcount = lua_gettop(L);
|
||||
|
||||
T** pointerpointer = reinterpret_cast<T**>(lua_newuserdata(L, sizeof(T*) + sizeof(T)));
|
||||
T*& referencepointer = *pointerpointer;
|
||||
T* obj = reinterpret_cast<T*>(pointerpointer + 1);
|
||||
referencepointer = obj;
|
||||
int userdataindex = lua_gettop(L);
|
||||
usertype_detail::match_constructor(L, obj, syntax, argcount - static_cast<int>(syntax), identity_t<TTypes>()...);
|
||||
|
||||
if (luaL_newmetatable(L, &meta[0]) == 1) {
|
||||
lua_pop(L, 1);
|
||||
std::string err = "unable to get usertype metatable for ";
|
||||
err += meta;
|
||||
throw error(err);
|
||||
}
|
||||
lua_setmetatable(L, userdataindex);
|
||||
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
static int destruct(lua_State* L) {
|
||||
userdata udata = stack::get<userdata>(L, 1);
|
||||
// The first sizeof(T*) bytes are the reference: the rest is
|
||||
// the actual data itself (if there is a reference at all)
|
||||
T** pobj = reinterpret_cast<T**>(udata.value);
|
||||
T*& obj = *pobj;
|
||||
std::allocator<T> alloc{};
|
||||
alloc.destroy(obj);
|
||||
return 0;
|
||||
}
|
||||
lua_CFunction constructfunc;
|
||||
const char* destructfuncname;
|
||||
lua_CFunction destructfunc;
|
||||
lua_CFunction functiongcfunc;
|
||||
bool needsindexfunction;
|
||||
|
||||
template<typename... Functions>
|
||||
std::unique_ptr<base_function> make_function(const std::string&, overload_set<Functions...> func) {
|
||||
return std::make_unique<usertype_overloaded_function<T, Functions...>>(func);
|
||||
return std::make_unique<usertype_overloaded_function<T, Functions...>>(std::move(func));
|
||||
}
|
||||
|
||||
template<typename... Functions>
|
||||
std::unique_ptr<base_function> make_function(const std::string&, constructor_wrapper<Functions...> func) {
|
||||
return std::make_unique<usertype_constructor_function<T, Functions...>>(std::move(func));
|
||||
}
|
||||
|
||||
template<typename Arg, typename... Args, typename Ret>
|
||||
|
@ -241,33 +230,85 @@ namespace sol {
|
|||
return std::make_unique<usertype_function<function_type, T>>(func);
|
||||
}
|
||||
|
||||
template<std::size_t N, typename... Args>
|
||||
void build_function(std::string funcname, constructors<Args...>) {
|
||||
functionnames.push_back(std::move(funcname));
|
||||
std::string& name = functionnames.back();
|
||||
// Insert bubble to keep with compile-time argument count (simpler and cheaper to do)
|
||||
functions.push_back(nullptr);
|
||||
constructfunc = construct<T, Args...>;
|
||||
metafunctiontable.push_back({ functionnames.back().c_str(), constructfunc });
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
void build_function(std::string funcname, destructor_wrapper<void>) {
|
||||
auto metamethodfind = std::find(meta_function_names.begin(), meta_function_names.end(), funcname);
|
||||
if (metamethodfind == meta_function_names.end())
|
||||
throw error("cannot set destructor to anything but the metamethod \"__gc\"");
|
||||
meta_function metafunction = static_cast<meta_function>(metamethodfind - meta_function_names.begin());
|
||||
if (metafunction != meta_function::garbage_collect)
|
||||
throw error("cannot set destructor to anything but the metamethod \"__gc\"");
|
||||
|
||||
functionnames.push_back(std::move(funcname));
|
||||
std::string& name = functionnames.back();
|
||||
destructfunc = destruct<T>;
|
||||
destructfuncname = name.c_str();
|
||||
// Insert bubble to stay with the compile-time count
|
||||
functions.push_back(nullptr);
|
||||
}
|
||||
|
||||
template<std::size_t N, typename Fx>
|
||||
void build_function(std::string funcname, destructor_wrapper<Fx> dx) {
|
||||
auto metamethodfind = std::find(meta_function_names.begin(), meta_function_names.end(), funcname);
|
||||
if (metamethodfind == meta_function_names.end())
|
||||
throw error("cannot set destructor to anything but the metamethod \"__gc\"");
|
||||
meta_function metafunction = static_cast<meta_function>(metamethodfind - meta_function_names.begin());
|
||||
if (metafunction != meta_function::garbage_collect)
|
||||
throw error("cannot set destructor to anything but the metamethod \"__gc\"");
|
||||
|
||||
functionnames.push_back(std::move(funcname));
|
||||
std::string& name = functionnames.back();
|
||||
auto baseptr = make_function(name, std::move(dx.fx));
|
||||
functions.emplace_back(std::move(baseptr));
|
||||
destructfunc = detail::usertype_call<N>;
|
||||
destructfuncname = name.c_str();
|
||||
}
|
||||
|
||||
template<std::size_t N, typename Fx>
|
||||
void build_function(std::string funcname, Fx&& func) {
|
||||
typedef std::is_member_object_pointer<Unqualified<Fx>> is_variable;
|
||||
typedef std::decay_t<Fx> function_type;
|
||||
functionnames.push_back(std::move(funcname));
|
||||
std::string& name = functionnames.back();
|
||||
auto baseptr = make_function(name, std::forward<Fx>(func));
|
||||
functions.emplace_back(std::move(baseptr));
|
||||
if (is_variable::value) {
|
||||
indexwrapper.insert({ name, { false, functions.back().get() } });
|
||||
newindexwrapper.insert({ name, { false, functions.back().get() } });
|
||||
return;
|
||||
}
|
||||
auto metamethodfind = std::find(meta_function_names.begin(), meta_function_names.end(), name);
|
||||
if (metamethodfind != meta_function_names.end()) {
|
||||
metafunctiontable.push_back({ name.c_str(), detail::usertype_call<N> });
|
||||
meta_function metafunction = static_cast<meta_function>(metamethodfind - meta_function_names.begin());
|
||||
switch (metafunction) {
|
||||
case meta_function::garbage_collect:
|
||||
destructfuncname = name.c_str();
|
||||
destructfunc = detail::usertype_call<N>;
|
||||
return;
|
||||
case meta_function::index:
|
||||
indexfunc = functions.back().get();
|
||||
needsindexfunction = true;
|
||||
break;
|
||||
case meta_function::new_index:
|
||||
newindexfunc = functions.back().get();
|
||||
break;
|
||||
case meta_function::construct:
|
||||
constructfunc = detail::usertype_call<N>;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
metafunctiontable.push_back({ name.c_str(), detail::usertype_call<N> });
|
||||
return;
|
||||
}
|
||||
if (is_variable::value) {
|
||||
needsindexfunction = true;
|
||||
indexwrapper.insert({ name, { false, functions.back().get() } });
|
||||
newindexwrapper.insert({ name, { false, functions.back().get() } });
|
||||
return;
|
||||
}
|
||||
indexwrapper.insert({ name, { true, functions.back().get() } });
|
||||
|
@ -300,55 +341,59 @@ namespace sol {
|
|||
metafunctiontable.push_back({ "__newindex", indexwrapper.empty() ? detail::usertype_call<N> : detail::usertype_call<N + 1> });
|
||||
++variableend;
|
||||
}
|
||||
if (destructfunc != nullptr) {
|
||||
metafunctiontable.push_back({ destructfuncname, destructfunc });
|
||||
}
|
||||
switch (variableend) {
|
||||
case 2:
|
||||
functiongc = detail::usertype_gc<N + 2>;
|
||||
functiongcfunc = detail::usertype_gc<N + 2>;
|
||||
break;
|
||||
case 1:
|
||||
functiongc = detail::usertype_gc<N + 1>;
|
||||
functiongcfunc = detail::usertype_gc<N + 1>;
|
||||
break;
|
||||
case 0:
|
||||
functiongc = detail::usertype_gc<N + 0>;
|
||||
functiongcfunc = detail::usertype_gc<N + 0>;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
template<typename... Args>
|
||||
usertype(Args&&... args) : usertype(default_constructor, std::forward<Args>(args)...) {}
|
||||
|
||||
template<typename... Args, typename... CArgs>
|
||||
usertype(constructors<CArgs...>, Args&&... args)
|
||||
: indexfunc(nullptr), newindexfunc(nullptr), constructfunc(nullptr), destructfunc(nullptr), functiongc(nullptr) {
|
||||
usertype(usertype_detail::verified_tag, Args&&... args) : indexfunc(nullptr), newindexfunc(nullptr), constructfunc(nullptr), destructfunc(nullptr), functiongcfunc(nullptr), needsindexfunction(false) {
|
||||
functionnames.reserve(sizeof...(args)+3);
|
||||
functiontable.reserve(sizeof...(args)+3);
|
||||
metafunctiontable.reserve(sizeof...(args)+3);
|
||||
|
||||
build_function_tables<0>(std::forward<Args>(args)...);
|
||||
|
||||
functionnames.push_back("new");
|
||||
metafunctiontable.push_back({ functionnames.back().c_str(), &constructor<CArgs...>::construct });
|
||||
functionnames.push_back("__gc");
|
||||
metafunctiontable.push_back({ functionnames.back().c_str(), destruct });
|
||||
|
||||
// ptr_functions does not participate in garbage collection/new,
|
||||
// as all pointered types are considered
|
||||
// to be references. This makes returns of
|
||||
// `std::vector<int>&` and `std::vector<int>*` work
|
||||
metafunctiontable.push_back({ nullptr, nullptr });
|
||||
functiontable.push_back({ nullptr, nullptr });
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
usertype(usertype_detail::add_destructor_tag, Args&&... args) : usertype(usertype_detail::verified, "__gc", default_destructor, std::forward<Args>(args)...) {}
|
||||
|
||||
template<typename... Args>
|
||||
usertype(usertype_detail::check_destructor_tag, Args&&... args) : usertype(If<And<std::is_destructible<T>, Not<usertype_detail::has_destructor<Args...>>>, usertype_detail::add_destructor_tag, usertype_detail::verified_tag>(), std::forward<Args>(args)...) {}
|
||||
|
||||
public:
|
||||
|
||||
template<typename... Args>
|
||||
usertype(Args&&... args) : usertype(If<And<std::is_default_constructible<T>, Not<usertype_detail::has_constructor<Args...>>>, decltype(default_constructor), usertype_detail::check_destructor_tag>(), std::forward<Args>(args)...) {}
|
||||
|
||||
template<typename... Args, typename... CArgs>
|
||||
usertype(constructors<CArgs...> constructorlist, Args&&... args) : usertype(usertype_detail::verified, "new", constructorlist, "__gc", default_destructor, std::forward<Args>(args)...) {
|
||||
|
||||
}
|
||||
|
||||
int push(lua_State* L) {
|
||||
// push pointer tables first,
|
||||
usertype_detail::push_metatable<T*>(L, functions, functiontable, metafunctiontable);
|
||||
usertype_detail::push_metatable<T*, true>(L, needsindexfunction, functions, functiontable, metafunctiontable);
|
||||
lua_pop(L, 1);
|
||||
// but leave the regular T table on last
|
||||
// so it can be linked to a type for usage with `.new(...)` or `:new(...)`
|
||||
usertype_detail::push_metatable<T>(L, functions, functiontable, metafunctiontable);
|
||||
usertype_detail::push_metatable<T, false>(L, needsindexfunction, functions, functiontable, metafunctiontable);
|
||||
// Make sure to drop a table in the global namespace to properly destroy the pushed functions
|
||||
// at some later point in life
|
||||
usertype_detail::set_global_deleter<T>(L, functiongc, functions);
|
||||
usertype_detail::set_global_deleter<T>(L, functiongcfunc, functions);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
|
65
tests.cpp
65
tests.cpp
|
@ -224,6 +224,41 @@ struct giver {
|
|||
|
||||
};
|
||||
|
||||
struct factory_test {
|
||||
private:
|
||||
factory_test() { a = true_a; }
|
||||
~factory_test() { a = 0; }
|
||||
public:
|
||||
static int num_saved;
|
||||
static int num_killed;
|
||||
|
||||
struct deleter {
|
||||
void operator()(factory_test* f) {
|
||||
f->~factory_test();
|
||||
}
|
||||
};
|
||||
|
||||
static const int true_a = 156;
|
||||
int a;
|
||||
|
||||
static std::unique_ptr<factory_test, deleter> make() {
|
||||
return std::unique_ptr<factory_test, deleter>( new factory_test(), deleter());
|
||||
}
|
||||
|
||||
static void save(factory_test& f) {
|
||||
new(&f)factory_test();
|
||||
++num_saved;
|
||||
}
|
||||
|
||||
static void kill(factory_test& f) {
|
||||
f.~factory_test();
|
||||
++num_killed;
|
||||
}
|
||||
};
|
||||
|
||||
int factory_test::num_saved = 0;
|
||||
int factory_test::num_killed = 0;
|
||||
|
||||
TEST_CASE("table/traversal", "ensure that we can chain requests and tunnel down into a value if we desire") {
|
||||
|
||||
sol::state lua;
|
||||
|
@ -1307,6 +1342,36 @@ func(1,2,3)
|
|||
REQUIRE_THROWS(lua.script("func(1,2,'meow')"));
|
||||
}
|
||||
|
||||
TEST_CASE("usertype/private constructible", "Check to make sure special snowflake types from Enterprise thingamahjongs work properly.") {
|
||||
int numsaved = factory_test::num_saved;
|
||||
int numkilled = factory_test::num_killed;
|
||||
{
|
||||
sol::state lua;
|
||||
lua.open_libraries(sol::lib::base);
|
||||
|
||||
lua.new_usertype<factory_test>("factory_test",
|
||||
"new", sol::constructor(factory_test::save),
|
||||
"__gc", sol::destructor(factory_test::kill),
|
||||
"a", &factory_test::a
|
||||
);
|
||||
|
||||
std::unique_ptr<factory_test, factory_test::deleter> f = factory_test::make();
|
||||
lua.set("true_a", factory_test::true_a, "f", f.get());
|
||||
REQUIRE_NOTHROW(lua.script(R"(
|
||||
assert(f.a == true_a)
|
||||
)"));
|
||||
|
||||
REQUIRE_NOTHROW(lua.script(R"(
|
||||
local fresh_f = factory_test:new()
|
||||
assert(fresh_f.a == true_a)
|
||||
)"));
|
||||
}
|
||||
int expectednumsaved = numsaved + 1;
|
||||
int expectednumkilled = numkilled + 1;
|
||||
REQUIRE(expectednumsaved == factory_test::num_saved);
|
||||
REQUIRE(expectednumkilled == factory_test::num_killed);
|
||||
}
|
||||
|
||||
TEST_CASE("usertype/overloading", "Check if overloading works properly for usertypes") {
|
||||
struct woof {
|
||||
int var;
|
||||
|
|
Loading…
Reference in New Issue
Block a user