diff --git a/.gitignore b/.gitignore index 158d2c1b..fcdebdb5 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ main2.cpp lua-5.3.2/ lua-5.2.4/ bench/ +*.props diff --git a/bootstrap.py b/bootstrap.py index ce6750eb..0c8fe489 100755 --- a/bootstrap.py +++ b/bootstrap.py @@ -50,7 +50,7 @@ args = parser.parse_args() # general variables include = [ '.', os.path.join('Catch', 'include')] depends = [] -cxxflags = [ '-Wall', '-Wextra', '-pedantic', '-pedantic-errors', '-std=c++11', '-Wno-unused-variable' ] +cxxflags = [ '-Wall', '-Wextra', '-pedantic', '-pedantic-errors', '-std=c++14', '-Wno-unused-variable' ] ldflags = [] script_dir = os.path.dirname(os.path.realpath(sys.argv[0])) sol_dir = os.path.join(script_dir, 'sol') diff --git a/sol/function.hpp b/sol/function.hpp index 28ca7e3c..3a2d9aaf 100644 --- a/sol/function.hpp +++ b/sol/function.hpp @@ -226,7 +226,7 @@ struct pusher> { template> static void set_memfx(types t, lua_State* L, Fx&& fx) { - typedef Decay>> raw_fx_t; + typedef std::decay_t>> raw_fx_t; typedef R(* fx_ptr_t)(Args...); typedef std::is_convertible is_convertible; set_isconvertible_fx(is_convertible(), t, L, std::forward(fx)); @@ -234,7 +234,7 @@ struct pusher> { template static void set_memfx(types<>, lua_State* L, Fx&& fx) { - typedef Unwrap> fx_t; + typedef Unwrapped> fx_t; typedef decltype(&fx_t::operator()) Sig; set_memfx(types>(), L, std::forward(fx)); } @@ -269,13 +269,13 @@ struct pusher> { template static void set_isconvertible_fx(std::true_type, types, lua_State* L, Fx&& fx) { typedef R(* fx_ptr_t)(Args...); - fx_ptr_t fxptr = unwrapper(std::forward(fx)); + fx_ptr_t fxptr = unwrap(std::forward(fx)); set(L, fxptr); } template static void set_isconvertible_fx(std::false_type, types, lua_State* L, Fx&& fx) { - typedef Unwrap> fx_t; + typedef Unwrapped> fx_t; std::unique_ptr sptr(new functor_function(std::forward(fx))); set_fx(L, std::move(sptr)); } @@ -287,7 +287,7 @@ struct pusher> { template static void set_reference_fx(std::false_type, lua_State* L, Fx&& fx, T&& obj) { - typedef std::remove_pointer_t> clean_fx; + typedef std::remove_pointer_t> clean_fx; std::unique_ptr sptr(new member_function>(std::forward(obj), std::forward(fx))); return set_fx(L, std::move(sptr)); } @@ -299,12 +299,12 @@ struct pusher> { // idx n + 1: is the object's void pointer // We don't need to store the size, because the other side is templated // with the same member function pointer type - typedef Decay dFx; + typedef std::decay_t dFx; typedef Unqualified uFx; dFx memfxptr(std::forward(fx)); auto userptr = sol::detail::get_ptr(obj); void* userobjdata = static_cast(userptr); - lua_CFunction freefunc = &static_member_function, uFx>::call; + lua_CFunction freefunc = &static_member_function, uFx>::call; int upvalues = stack::detail::push_as_upvalues(L, memfxptr); upvalues += stack::push(L, userobjdata); @@ -314,7 +314,7 @@ struct pusher> { template static void set_fx(std::false_type, lua_State* L, Fx&& fx) { - Decay target(std::forward(fx)); + std::decay_t target(std::forward(fx)); lua_CFunction freefunc = &static_function::call; int upvalues = stack::detail::push_as_upvalues(L, target); diff --git a/sol/function_types.hpp b/sol/function_types.hpp index d70bff86..780719d5 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -28,12 +28,19 @@ namespace sol { namespace detail { -struct ref_call_t { - ref_call_t() {} +struct ref_call_t {} const ref_call = ref_call_t{}; +template +struct implicit_wrapper { + T& item; + implicit_wrapper(T& item) : item(item) {} + operator T& () { + return item; + } + operator T* () { + return std::addressof(item); + } }; -const auto ref_call = ref_call_t{}; - template struct function_packer : std::tuple { using std::tuple::tuple; }; @@ -51,8 +58,8 @@ struct functor { T* item; Func invocation; - template - functor(FxArgs&&... fxargs): item(nullptr), invocation(std::forward(fxargs)...) {} + template + functor(Args&&... args): item(nullptr), invocation(std::forward(args)...) {} bool check () const { return invocation != nullptr; @@ -71,7 +78,7 @@ struct functor { } template - auto operator()(Args&&... args) -> decltype(std::declval().call(types{}, std::forward(args)...)) { + decltype(auto) operator()(Args&&... args) { return this->call(types{}, std::forward(args)...); } }; @@ -138,13 +145,13 @@ public: template void call(types, Args&&... args) { T& member = *item; - invocation(member, std::forward(args)...); + invocation(implicit_wrapper(member), std::forward(args)...); } template Ret call(types, Args&&... args) { T& member = *item; - return invocation(member, std::forward(args)...); + return invocation(implicit_wrapper(member), std::forward(args)...); } template @@ -154,7 +161,6 @@ public: }; } // detail - template struct static_function { typedef std::remove_pointer_t> function_type; @@ -168,16 +174,10 @@ struct static_function { return 0; } - template - static int typed_call(types<>, types t, function_type* fx, lua_State* L) { - return typed_call(types(), t, fx, L); - } - template static int typed_call(types, types ta, function_type* fx, lua_State* L) { typedef return_type_t return_type; - typedef decltype(stack::call(L, 0, types(), ta, fx)) ret_t; - ret_t r = stack::call(L, 0, types(), ta, fx); + decltype(auto) r = stack::call(L, 0, types(), ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); return stack::push(L, std::forward(r)); @@ -209,19 +209,13 @@ struct static_member_function { return 0; } - template - static int typed_call(types<>, types t, T& item, function_type& ifx, lua_State* L) { - return typed_call(types(), t, item, ifx, L); - } - template static int typed_call(types tr, types ta, T& item, function_type& ifx, lua_State* L) { - typedef return_type_t return_type; auto fx = [&item, &ifx](Args&&... args) -> return_type { return (item.*ifx)(std::forward(args)...); }; - return_type r = stack::call(L, 0, tr, ta, fx); + decltype(auto) r = stack::call(L, 0, tr, ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); - return stack::push(L, std::forward(r)); + return stack::push(L, std::forward(r)); } static int call(lua_State* L) { @@ -311,7 +305,7 @@ struct base_function { } static int gc(lua_State* L) { - func_gc(std::integral_constant(), L); + func_gc(Bool<(I < 1)>(), L); return 0; } }; @@ -334,8 +328,8 @@ struct functor_function : public base_function { typedef function_args_t args_type; Function fx; - template - functor_function(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} + template + functor_function(Args&&... args): fx(std::forward(args)...) {} template int operator()(types r, types t, lua_State* L) { @@ -345,11 +339,6 @@ struct functor_function : public base_function { return 0; } - template - int operator()(types<>, types t, lua_State* L) { - return (*this)(types(), t, L); - } - template int operator()(types tr, types ta, lua_State* L) { return_type r = stack::call(L, 0, tr, ta, fx); @@ -376,18 +365,18 @@ struct member_function : public base_function { T member; function_type invocation; - template - functor(Tm&& m, FxArgs&&... fxargs): member(std::forward(m)), invocation(std::forward(fxargs)...) {} + template + functor(Tm&& m, Args&&... args): member(std::forward(m)), invocation(std::forward(args)...) {} template return_type operator()(Args&&... args) { - auto& mem = unwrapper(unref(member)); + auto& mem = unwrap(deref(member)); return (mem.*invocation)(std::forward(args)...); } } fx; - template - member_function(Tm&& m, FxArgs&&... fxargs): fx(std::forward(m), std::forward(fxargs)...) {} + template + member_function(Tm&& m, Args&&... args): fx(std::forward(m), std::forward(args)...) {} template int operator()(types tr, types ta, lua_State* L) { @@ -395,15 +384,9 @@ struct member_function : public base_function { return 0; } - template - int operator()(types<>, types t, lua_State* L) { - return (*this)(types(), t, L); - } - template int operator()(types tr, types ta, lua_State* L) { - typedef decltype(stack::call(L, 0, tr, ta, fx)) ret_t; - ret_t r = stack::call(L, 0, tr, ta, fx); + decltype(auto) r = stack::call(L, 0, tr, ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); return stack::push(L, std::forward(r)); @@ -429,12 +412,12 @@ struct usertype_function_core : public base_function { fx_t fx; - template - usertype_function_core(FxArgs&&... fxargs): fx(std::forward(fxargs)...) {} + template + usertype_function_core(Args&&... args): fx(std::forward(args)...) {} template> std::enable_if_t::value, int> push(lua_State* L, Return&& r) { - if(detail::get_ptr(r) == fx.item) { + if(ptr(unwrap(r)) == fx.item) { // push nothing // note that pushing nothing with the ':' // syntax means we leave the instance of what @@ -454,33 +437,22 @@ struct usertype_function_core : public base_function { } template - int call(types r, types t, lua_State* L) { + int operator()(types tr, types ta, lua_State* L) { //static const std::size_t skew = static_cast(std::is_member_object_pointer::value); - stack::call(L, 0, r, t, fx); + stack::call(L, 0, tr, ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); return 0; } - template - int call(types<>, types t, lua_State* L) { - return this->call(types(), t, L); - } - template - int call(types tr, types ta, lua_State* L) { - typedef decltype(stack::call(L, 0, tr, ta, fx)) ret_t; - ret_t r = stack::call(L, 0, tr, ta, fx); + int operator()(types tr, types ta, lua_State* L) { + decltype(auto) r = stack::call(L, 0, tr, ta, fx); int nargs = static_cast(sizeof...(Args)); lua_pop(L, nargs); int pushcount = push(L, std::forward(r)); return pushcount; } - - template - int operator()(types r, types t, lua_State* L) { - return this->call(r, t, L); - } }; template @@ -494,9 +466,8 @@ struct usertype_function : public usertype_function_core { template usertype_function(FxArgs&&... fxargs): base_t(std::forward(fxargs)...) {} - template - int fx_call(lua_State* L) { - this->fx.item = detail::get_ptr(stack::get(L, 1)); + int prelude(lua_State* L) { + this->fx.item = ptr(stack::get(L, 1)); if(this->fx.item == nullptr) { throw error("userdata for function call is null: are you using the wrong syntax? (use item:function/variable(...) syntax)"); } @@ -504,11 +475,11 @@ struct usertype_function : public usertype_function_core { } virtual int operator()(lua_State* L) override { - return fx_call(L); + return prelude(L); } virtual int operator()(lua_State* L, detail::ref_call_t) override { - return fx_call(L); + return prelude(L); } }; @@ -523,9 +494,8 @@ struct usertype_variable_function : public usertype_function_core template usertype_variable_function(FxArgs&&... fxargs): base_t(std::forward(fxargs)...) {} - template - int fx_call(lua_State* L) { - this->fx.item = detail::get_ptr(stack::get(L, 1)); + int prelude(lua_State* L) { + this->fx.item = ptr(stack::get(L, 1)); if(this->fx.item == nullptr) { throw error("userdata for member variable is null"); } @@ -543,11 +513,11 @@ struct usertype_variable_function : public usertype_function_core } virtual int operator()(lua_State* L) override { - return fx_call(L); + return prelude(L); } virtual int operator()(lua_State* L, detail::ref_call_t) override { - return fx_call(L); + return prelude(L); } }; @@ -565,41 +535,30 @@ struct usertype_indexing_function : public usertype_function_core template usertype_indexing_function(std::string name, FxArgs&&... fxargs): base_t(std::forward(fxargs)...), name(std::move(name)) {} - template - int fx_call(lua_State* L) { + int prelude(lua_State* L) { std::string accessor = stack::get(L, 1 - lua_gettop(L)); auto function = functions.find(accessor); if(function != functions.end()) { if(function->second.second) { stack::push(L, function->second.first.get()); - if(std::is_same::value) { - stack::push(L, &base_function::usertype<0>::ref_call, 1); - } - else { - stack::push(L, &base_function::usertype<0>::call, 1); - } + stack::push(L, &base_function::usertype<0>::ref_call, 1); return 1; } - else if(std::is_same::value) { - return (*function->second.first)(L, detail::ref_call); - } - else { - return (*function->second.first)(L); - } + return (*function->second.first)(L, detail::ref_call); } if (!this->fx.check()) { throw error("invalid indexing \"" + accessor + "\" on type: " + name); } - this->fx.item = detail::get_ptr(stack::get(L, 1)); + this->fx.item = ptr(stack::get(L, 1)); return static_cast(*this)(tuple_types(), args_type(), L); } virtual int operator()(lua_State* L) override { - return fx_call(L); + return prelude(L); } virtual int operator()(lua_State* L, detail::ref_call_t) override { - return fx_call(L); + return prelude(L); } }; diff --git a/sol/proxy.hpp b/sol/proxy.hpp index 80def0f0..3bc6e98c 100644 --- a/sol/proxy.hpp +++ b/sol/proxy.hpp @@ -86,22 +86,22 @@ public: template inline bool operator==(T&& left, const proxy& right) { - return left == right.template get>(); + return left == right.template get>(); } template inline bool operator==(const proxy& right, T&& left) { - return right.template get>() == left; + return right.template get>() == left; } template inline bool operator!=(T&& left, const proxy& right) { - return right.template get>() != left; + return right.template get>() != left; } template inline bool operator!=(const proxy& right, T&& left) { - return right.template get>() != left; + return right.template get>() != left; } } // sol diff --git a/sol/stack.hpp b/sol/stack.hpp index c1c60856..0f4d8468 100644 --- a/sol/stack.hpp +++ b/sol/stack.hpp @@ -33,23 +33,6 @@ #include namespace sol { -namespace detail { -template -inline T* get_ptr(T& val) { - return std::addressof(val); -} - -template -inline T* get_ptr(std::reference_wrapper val) { - return std::addressof(val.get()); -} - -template -inline T* get_ptr(T* val) { - return val; -} -} // detail - namespace stack { template @@ -137,10 +120,10 @@ template struct userdata_pusher { template static void push (lua_State* L, Key&& metatablekey, Args&&... args) { - // Basically, we store all data like this: - // If it's a new value (no std::ref(x)), then we store the pointer to the new + // Basically, we store all user0data like this: + // If it's a movable/copyable value (no std::ref(x)), then we store the pointer to the new // data in the first sizeof(T*) bytes, and then however many bytes it takes to - // do the actual object. Things that are just references/pointers are stored as + // do the actual object. Things that are std::ref or plain T* are stored as // just the sizeof(T*), and nothing else. T** pdatum = static_cast(lua_newuserdata(L, sizeof(T*) + sizeof(T))); T** referencepointer = pdatum; @@ -624,12 +607,12 @@ inline void remove( lua_State* L, int index, int count ) { } } -template ::value>::type> +template ::value>> inline R call(lua_State* L, int start, types tr, types ta, Fx&& fx, FxArgs&&... args) { return detail::call(L, start, ta, tr, ta, std::forward(fx), std::forward(args)...); } -template ::value>::type> +template ::value>> inline R call(lua_State* L, types tr, types ta, Fx&& fx, FxArgs&&... args) { return call(L, 0, ta, tr, ta, std::forward(fx), std::forward(args)...); } diff --git a/sol/state_view.hpp b/sol/state_view.hpp index d2d54003..b720386f 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -150,9 +150,9 @@ public: return globals.get(std::forward(keys)...); } - template - state_view& set(Tn&&... argn) { - globals.set(std::forward(argn)...); + template + state_view& set(Args&&... args) { + globals.set(std::forward(args)...); return *this; } @@ -214,11 +214,11 @@ public: return result; } - template - table create_table(int narr = 0, int nrec = sizeof...(Tn), Tn&&... argn) { + template + table create_table(int narr = 0, int nrec = sizeof...(Args), Args&&... args) { lua_createtable(L, narr, nrec); table result(L); - result.set(std::forward(argn)...); + result.set(std::forward(args)...); lua_pop(L, 1); return result; } diff --git a/sol/table_core.hpp b/sol/table_core.hpp index a086ff85..dab706fa 100644 --- a/sol/table_core.hpp +++ b/sol/table_core.hpp @@ -114,9 +114,9 @@ public: return tuple_get( types( ), build_indices( ), std::forward_as_tuple(std::forward(keys)...)); } - template - table_core& set( Tn&&... argn ) { - tuple_set(build_indices(), std::forward_as_tuple(std::forward(argn)...)); + template + table_core& set( Args&&... args ) { + tuple_set(build_indices(), std::forward_as_tuple(std::forward(args)...)); return *this; } @@ -214,7 +214,7 @@ private: template void set_fx( types<>, Key&& key, Fx&& fx ) { - typedef Unqualified> fx_t; + typedef Unwrapped> fx_t; typedef decltype( &fx_t::operator() ) Sig; set_fx( types>( ), std::forward( key ), std::forward( fx ) ); } diff --git a/sol/traits.hpp b/sol/traits.hpp index a64989da..c60dd692 100644 --- a/sol/traits.hpp +++ b/sol/traits.hpp @@ -31,6 +31,9 @@ namespace sol { template struct identity { typedef T type; }; +template +using identity_t = typename identity::type; + template struct is_tuple : std::false_type{ }; @@ -38,12 +41,12 @@ template struct is_tuple> : std::true_type{ }; template -struct unwrap { +struct unwrapped { typedef T type; }; template -struct unwrap> { +struct unwrapped> { typedef T type; }; @@ -55,6 +58,14 @@ struct remove_member_pointer { typedef R type; }; +template +struct remove_member_pointer { + typedef R type; +}; + +template +using remove_member_pointer_t = remove_member_pointer; + template class Templ> struct is_specialization_of : std::false_type { }; template class Templ> @@ -100,13 +111,10 @@ template using DisableIf = typename std::enable_if>::value, int>::type; template -using Unqualified = typename std::remove_cv::type>::type; +using Unqualified = std::remove_cv_t>; template -using Decay = typename std::decay::type; - -template -using Unwrap = typename unwrap::type; +using Unwrapped = typename unwrapped::type; template struct return_type { @@ -126,16 +134,16 @@ struct return_type<> { template using return_type_t = typename return_type::type; -template -using ReturnTypeOr = typename std::conditional<(sizeof...(Tn) < 1), Empty, typename return_type::type>::type; +template +using ReturnTypeOr = typename std::conditional<(sizeof...(Args) < 1), Empty, typename return_type::type>::type; -template -using ReturnType = ReturnTypeOr; +template +using ReturnType = ReturnTypeOr; namespace detail { template>::value> -struct is_function_impl : std::is_function::type> {}; +struct is_function_impl : std::is_function> {}; template struct is_function_impl { @@ -192,12 +200,12 @@ struct fx_traits { typedef std::tuple arg_tuple_type; typedef types args_type; typedef R(T::* function_pointer_type)(Args...); - typedef typename std::remove_pointer::type function_type; + typedef std::remove_pointer_t function_type; typedef R(*free_function_pointer_type)(Args...); typedef R return_type; - typedef typename std::remove_pointer::type signature_type; + typedef std::remove_pointer_t signature_type; template - using arg = typename std::tuple_element::type; + using arg = std::tuple_element_t; }; template @@ -207,12 +215,12 @@ struct fx_traits { typedef std::tuple arg_tuple_type; typedef types args_type; typedef R(T::* function_pointer_type)(Args...); - typedef typename std::remove_pointer::type function_type; + typedef std::remove_pointer_t function_type; typedef R(*free_function_pointer_type)(Args...); typedef R return_type; - typedef typename std::remove_pointer::type signature_type; + typedef std::remove_pointer_t signature_type; template - using arg = typename std::tuple_element::type; + using arg = std::tuple_element_t; }; template @@ -225,9 +233,9 @@ struct fx_traits { typedef R(*function_pointer_type)(Args...); typedef R(*free_function_pointer_type)(Args...); typedef R return_type; - typedef typename std::remove_pointer::type signature_type; + typedef std::remove_pointer_t signature_type; template - using arg = typename std::tuple_element::type; + using arg = std::tuple_element_t; }; template @@ -240,9 +248,9 @@ struct fx_traits { typedef R(*function_pointer_type)(Args...); typedef R(*free_function_pointer_type)(Args...); typedef R return_type; - typedef typename std::remove_pointer::type signature_type; + typedef std::remove_pointer_t signature_type; template - using arg = typename std::tuple_element::type; + using arg = std::tuple_element_t; }; } // detail @@ -279,7 +287,7 @@ struct member_traits { typedef R(*function_pointer_type)(Arg); typedef R(*free_function_pointer_type)(Arg); template - using arg = typename std::tuple_element::type; + using arg = std::tuple_element_t; }; } // detail @@ -322,43 +330,58 @@ template using is_c_str = Or>, char*>, std::is_same, std::string>>; template -auto unwrapper(T&& item) -> decltype(std::forward(item)) { +auto unwrap(T&& item) -> decltype(std::forward(item)) { return std::forward(item); } -template -Arg& unwrapper(std::reference_wrapper arg) { +template +T& unwrap(std::reference_wrapper arg) { return arg.get(); } template -T& unref(T& item) { +T& deref(T& item) { return item; } template -T& unref(T* item) { +T& deref(T* item) { + return *item; +} + +template +decltype(auto) deref(std::unique_ptr& item) { return *item; } template -T& unref(std::unique_ptr& item) { +T& deref(std::shared_ptr& item) { + return *item; +} + +template +decltype(auto) deref(const std::unique_ptr& item) { return *item; } template -T& unref(std::shared_ptr& item) { +T& deref(const std::shared_ptr& item) { return *item; } template -T& unref(const std::unique_ptr& item) { - return *item; +inline T* ptr(T& val) { + return std::addressof(val); } template -T& unref(const std::shared_ptr& item) { - return *item; +inline T* ptr(std::reference_wrapper val) { + return std::addressof(val.get()); +} + +template +inline T* ptr(T* val) { + return val; } } // sol diff --git a/sol/tuple.hpp b/sol/tuple.hpp index 22863cb4..9386b010 100644 --- a/sol/tuple.hpp +++ b/sol/tuple.hpp @@ -29,6 +29,9 @@ namespace sol { template struct reverse_tuple; +template +using reverse_tuple_t = typename reverse_tuple::type; + template<> struct reverse_tuple> { using type = std::tuple<>; @@ -57,7 +60,7 @@ template struct build_reverse_indices<0, Ns...> : indices {}; template -struct types : build_indices { typedef types type; }; +struct types : build_indices { typedef types type; static constexpr std::size_t size() { return sizeof...(Args); } }; namespace detail { template @@ -91,8 +94,8 @@ using tuple_types_t = typename tuple_types::type; template struct remove_one_type : detail::chop_one {}; -template -struct constructors {}; +template +using constructors = sol::types; const auto default_constructor = constructors>{}; diff --git a/sol/types.hpp b/sol/types.hpp index a02def33..bc1721f8 100644 --- a/sol/types.hpp +++ b/sol/types.hpp @@ -25,7 +25,6 @@ #include "compatibility.hpp" #include "traits.hpp" #include -#include "traits.hpp" namespace sol { struct nil_t {}; @@ -172,7 +171,7 @@ template struct lua_type_of : std::integral_constant {}; template -struct lua_type_of::value>::type> : std::integral_constant {}; +struct lua_type_of::value>> : std::integral_constant {}; template inline type type_of() { @@ -186,7 +185,7 @@ inline type type_of(lua_State* L, int index) { // All enumerations are given and taken from lua // as numbers as well template -struct lua_type_of::value>::type> : std::integral_constant { +struct lua_type_of::value>> : std::integral_constant { }; @@ -199,8 +198,8 @@ struct is_proxy_primitive : is_lua_primitive { }; template struct is_proxy_primitive> : std::true_type { }; -template -struct is_proxy_primitive> : std::true_type { }; +template +struct is_proxy_primitive> : std::true_type { }; } // sol #endif // SOL_TYPES_HPP diff --git a/sol/usertype.hpp b/sol/usertype.hpp index 9344b8a8..9c386460 100644 --- a/sol/usertype.hpp +++ b/sol/usertype.hpp @@ -129,7 +129,7 @@ private: T*& referencereference = *referencepointer; T* obj = reinterpret_cast(referencepointer + 1); referencereference = obj; - match_constructor(L, obj, syntax, argcount - static_cast(syntax), typename identity::type()...); + match_constructor(L, obj, syntax, argcount - static_cast(syntax), identity_t()...); if(luaL_newmetatable(L, std::addressof(meta[0])) == 1) { lua_pop(L, 1); @@ -143,17 +143,16 @@ private: } }; - struct destructor { - static int destruct(lua_State* L) { - userdata udata = stack::get(L, 1); - // The first sizeof(T*) bytes are the reference: the rest is - // the actual data itself - T* obj = static_cast(static_cast(reinterpret_cast(udata.value) + sizeof(T*))); - std::allocator alloc{}; - alloc.destroy(obj); - return 0; - } - }; + static int destruct(lua_State* L) { + userdata udata = stack::get(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(udata.value); + T*& obj = *pobj; + std::allocator alloc{}; + alloc.destroy(obj); + return 0; + } template void build_cleanup() { @@ -218,7 +217,7 @@ private: template bool build_function(std::true_type, function_map_t*&, function_map_t*&, std::string funcname, Ret Base::* func) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); - typedef typename std::decay::type function_type; + typedef std::decay_t function_type; indexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique>(func), false)); newindexmetafunctions.emplace(funcname, std::make_pair(detail::make_unique>(func), false)); return false; @@ -228,43 +227,43 @@ private: std::unique_ptr make_function(const std::string&, Ret(*func)(Arg, Args...)) { typedef Unqualified Argu; static_assert(std::is_base_of::value, "Any non-member-function must have a first argument which is covariant with the desired userdata type."); - typedef typename std::decay::type function_type; - return detail::make_unique>(func); + typedef std::decay_t function_type; + return std::make_unique>(func); } template std::unique_ptr make_variable_function(std::true_type, const std::string&, Ret Base::* func) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); - typedef typename std::decay::type function_type; - return detail::make_unique>(func); + typedef std::decay_t function_type; + return std::make_unique>(func); } template std::unique_ptr make_variable_function(std::false_type, const std::string&, Ret Base::* func) { static_assert(std::is_base_of::value, "Any registered function must be part of the class"); - typedef typename std::decay::type function_type; - return detail::make_unique>(func); + typedef std::decay_t function_type; + return std::make_unique>(func); } template std::unique_ptr make_function(const std::string& name, Ret Base::* func) { - typedef typename std::decay::type function_type; + typedef std::decay_t function_type; return make_variable_function(std::is_member_object_pointer(), name, func); } template std::unique_ptr make_function(const std::string&, Fx&& func) { typedef Unqualified Fxu; - typedef typename std::tuple_element<0, typename function_traits::arg_tuple_type>::type TArg; - typedef Unqualified TArgu; - static_assert(std::is_base_of::value, "Any non-member-function must have a first argument which is covariant with the desired userdata type."); - typedef typename std::decay::type function_type; - return detail::make_unique>(func); + typedef std::tuple_element_t<0, typename function_traits::arg_tuple_type> Arg; + typedef Unqualified Argu; + static_assert(std::is_base_of::value, "Any non-member-function must have a first argument which is covariant with the desired usertype."); + typedef std::decay_t function_type; + return std::make_unique>(func); } template bool build_function(std::false_type, function_map_t*& index, function_map_t*& newindex, std::string funcname, Fx&& func) { - typedef typename std::decay::type function_type; + typedef std::decay_t function_type; auto metamethod = std::find(meta_function_names.begin(), meta_function_names.end(), funcname); if(metamethod != meta_function_names.end()) { functionnames.push_back(std::move(funcname)); @@ -300,7 +299,7 @@ private: template void build_function_tables(function_map_t*& index, function_map_t*& newindex, std::string funcname, Fx&& func, Args&&... args) { - typedef typename std::is_member_object_pointer>::type is_variable; + typedef std::is_member_object_pointer> is_variable; static const std::size_t V = static_cast(!is_variable::value); if(build_function(is_variable(), index, newindex, std::move(funcname), std::forward(func))) { build_function_tables(index, newindex, std::forward(args)...); @@ -347,7 +346,7 @@ public: functionnames.push_back("new"); metafunctiontable.push_back({ functionnames.back().c_str(), &constructor::construct }); functionnames.push_back("__gc"); - metafunctiontable.push_back({ functionnames.back().c_str(), &destructor::destruct }); + 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 @@ -384,7 +383,7 @@ private: void set_global_deleter(lua_State* L) { // Automatic deleter table -- stays alive until lua VM dies - // even if the user calls collectgarbage() + // even if the user calls collectgarbage(), weirdly enough lua_createtable(L, 0, 0); lua_createtable(L, 0, 1); int up = push_upvalues(L, metafunctions);