From 4bd4b4e3d974eeb65edb4c2438de1ee6557dcd1b Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Sun, 12 Feb 2023 13:09:45 +0700 Subject: [PATCH] cleanup function/method calls --- develop/singles/headers/meta.hpp/meta_all.hpp | 355 +++++++++--------- headers/meta.hpp/meta_base/base.hpp | 4 + headers/meta.hpp/meta_base/type_list.hpp | 21 ++ .../meta_detail/value_utilities/uarg.hpp | 30 ++ headers/meta.hpp/meta_states/constructor.hpp | 101 ++--- headers/meta.hpp/meta_states/function.hpp | 92 ++--- headers/meta.hpp/meta_states/member.hpp | 4 +- headers/meta.hpp/meta_states/method.hpp | 101 ++--- headers/meta.hpp/meta_states/variable.hpp | 2 +- 9 files changed, 358 insertions(+), 352 deletions(-) diff --git a/develop/singles/headers/meta.hpp/meta_all.hpp b/develop/singles/headers/meta.hpp/meta_all.hpp index 803e301..6c77f83 100644 --- a/develop/singles/headers/meta.hpp/meta_all.hpp +++ b/develop/singles/headers/meta.hpp/meta_all.hpp @@ -47,6 +47,10 @@ # include #endif +#if !defined(META_HPP_FWD) +# define META_HPP_FWD(v) std::forward(v) +#endif + #if !defined(META_HPP_ASSERT) # include # define META_HPP_ASSERT(...) assert(__VA_ARGS__) // NOLINT @@ -1156,6 +1160,15 @@ namespace meta_hpp::detail template < typename... Types > struct type_list {}; + template < std::size_t I > + using size_constant = std::integral_constant; + + template < std::size_t I > + using index_constant = std::integral_constant; +} + +namespace meta_hpp::detail +{ template < std::size_t Index, typename TypeList > struct type_list_at; @@ -1168,6 +1181,18 @@ namespace meta_hpp::detail using type_list_at_t = typename type_list_at::type; } +namespace meta_hpp::detail +{ + template < typename TypeList > + struct type_list_arity; + + template < typename... Types > + struct type_list_arity> : size_constant {}; + + template < typename TypeList > + inline constexpr std::size_t type_list_arity_v = type_list_arity::value; +} + namespace meta_hpp::detail { class type_id final { @@ -5551,6 +5576,36 @@ namespace meta_hpp::detail } } +namespace meta_hpp::detail +{ + template < typename ArgTypeList, typename F > + auto call_with_uargs(std::span args, F&& f) { + META_HPP_ASSERT(args.size() == type_list_arity_v); + return [ args, &f ](std::index_sequence) { + return f(args[Is].cast>()...); + } + (std::make_index_sequence>()); + } + + template < typename ArgTypeList > + bool can_cast_all_uargs(std::span args) { + META_HPP_ASSERT(args.size() == type_list_arity_v); + return [args](std::index_sequence) { + return (... && args[Is].can_cast_to>()); + } + (std::make_index_sequence>()); + } + + template < typename ArgTypeList > + bool can_cast_all_uargs(std::span args) { + META_HPP_ASSERT(args.size() == type_list_arity_v); + return [args](std::index_sequence) { + return (... && args[Is].can_cast_to>()); + } + (std::make_index_sequence>()); + } +} + namespace meta_hpp::detail { template < function_pointer_kind Function > @@ -5595,17 +5650,17 @@ namespace meta_hpp::detail using return_type = typename ft::return_type; using argument_types = typename ft::argument_types; - constexpr bool as_copy // - = std::is_copy_constructible_v // - && std::is_same_v; // + constexpr bool as_copy // + = std::is_copy_constructible_v // + && std::is_same_v; - constexpr bool as_void // - = std::is_void_v // - || std::is_same_v; // + constexpr bool as_void // + = std::is_void_v // + || std::is_same_v; - constexpr bool ref_as_ptr // - = std::is_reference_v // - && std::is_same_v; // + constexpr bool ref_as_ptr // + = std::is_reference_v // + && std::is_same_v; static_assert(as_copy || as_void || ref_as_ptr); @@ -5614,31 +5669,27 @@ namespace meta_hpp::detail && "an attempt to call a function with an incorrect arity" ); - return std::invoke( - [ function_ptr, args ](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a function with incorrect argument types" - ); - - if constexpr ( std::is_void_v ) { - function_ptr(args[Is].cast>()...); - return uvalue{}; - } else if constexpr ( std::is_same_v ) { - std::ignore = function_ptr(args[Is].cast>()...); - return uvalue{}; - } else { - return_type&& return_value = function_ptr(args[Is].cast>()...); - - if constexpr ( ref_as_ptr ) { - return uvalue{std::addressof(return_value)}; - } else { - return uvalue{std::forward(return_value)}; - } - } - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a function with incorrect argument types" ); + + return call_with_uargs(args, [function_ptr](auto&&... all_args) { + if constexpr ( std::is_void_v ) { + function_ptr(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( std::is_same_v ) { + std::ignore = function_ptr(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( !std::is_void_v ) { + return_type&& result = function_ptr(META_HPP_FWD(all_args)...); + return ref_as_ptr ? uvalue{std::addressof(result)} : uvalue{META_HPP_FWD(result)}; + } + }); } template < function_pointer_kind Function > @@ -5646,16 +5697,8 @@ namespace meta_hpp::detail using ft = function_traits; using argument_types = typename ft::argument_types; - if ( args.size() != ft::arity ) { - return false; - } - - return std::invoke( - [args](std::index_sequence) { - return (... && args[Is].can_cast_to>()); - }, - std::make_index_sequence() - ); + return args.size() == ft::arity // + && can_cast_all_uargs(args); } } @@ -5678,16 +5721,14 @@ namespace meta_hpp::detail using ft = function_traits; using ft_argument_types = typename ft::argument_types; - return std::invoke( - [](std::index_sequence) { - [[maybe_unused]] const auto make_argument = [](std::index_sequence) { - using P = type_list_at_t; - return argument{argument_state::make

(I, metadata_map{})}; - }; - return argument_list{make_argument(std::index_sequence{})...}; - }, - std::make_index_sequence() - ); + return [](std::index_sequence) { + [[maybe_unused]] const auto make_argument = [](index_constant) { + using P = type_list_at_t; + return argument{argument_state::make

(I, metadata_map{})}; + }; + return argument_list{make_argument(index_constant{})...}; + } + (std::make_index_sequence()); } } @@ -6040,7 +6081,7 @@ namespace meta_hpp::detail auto&& return_value = inst.cast().*member_ptr; if constexpr ( as_copy ) { - return uvalue{std::forward(return_value)}; + return uvalue{META_HPP_FWD(return_value)}; } if constexpr ( as_ptr ) { @@ -6059,7 +6100,7 @@ namespace meta_hpp::detail auto&& return_value = inst.cast().*member_ptr; if constexpr ( as_copy ) { - return uvalue{std::forward(return_value)}; + return uvalue{META_HPP_FWD(return_value)}; } if constexpr ( as_ptr ) { @@ -6314,17 +6355,17 @@ namespace meta_hpp::detail using qualified_type = typename mt::qualified_type; using argument_types = typename mt::argument_types; - constexpr bool as_copy // - = std::is_copy_constructible_v // - && std::is_same_v; // + constexpr bool as_copy // + = std::is_copy_constructible_v // + && std::is_same_v; - constexpr bool as_void // - = std::is_void_v // - || std::is_same_v; // + constexpr bool as_void // + = std::is_void_v // + || std::is_same_v; - constexpr bool ref_as_ptr // - = std::is_reference_v // - && std::is_same_v; // + constexpr bool ref_as_ptr // + = std::is_reference_v // + && std::is_same_v; static_assert(as_copy || as_void || ref_as_ptr); @@ -6332,39 +6373,33 @@ namespace meta_hpp::detail args.size() == mt::arity // && "an attempt to call a method with an incorrect arity" ); + META_HPP_ASSERT( // inst.can_cast_to() // && "an attempt to call a method with an incorrect instance type" ); - return std::invoke( - [ method_ptr, &inst, args ](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a method with incorrect argument types" - ); - - if constexpr ( std::is_void_v ) { - (inst.cast().*method_ptr)(args[Is].cast>()...); - return uvalue{}; - } else if constexpr ( std::is_same_v ) { - std::ignore = (inst.cast().*method_ptr)(args[Is].cast>()... - ); - return uvalue{}; - } else { - return_type&& return_value = (inst.cast().*method_ptr)( - args[Is].cast>()... - ); - - if constexpr ( ref_as_ptr ) { - return uvalue{std::addressof(return_value)}; - } else { - return uvalue{std::forward(return_value)}; - } - } - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a method with incorrect argument types" ); + + return call_with_uargs(args, [method_ptr, &inst](auto&&... all_args) { + if constexpr ( std::is_void_v ) { + (inst.cast().*method_ptr)(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( std::is_same_v ) { + std::ignore = (inst.cast().*method_ptr)(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( !std::is_void_v ) { + return_type&& result = (inst.cast().*method_ptr)(META_HPP_FWD(all_args)...); + return ref_as_ptr ? uvalue{std::addressof(result)} : uvalue{META_HPP_FWD(result)}; + } + }); } template < method_pointer_kind Method > @@ -6373,20 +6408,9 @@ namespace meta_hpp::detail using qualified_type = typename mt::qualified_type; using argument_types = typename mt::argument_types; - if ( args.size() != mt::arity ) { - return false; - } - - if ( !inst.can_cast_to() ) { - return false; - } - - return std::invoke( - [args](std::index_sequence) { - return (... && args[Is].can_cast_to>()); - }, - std::make_index_sequence() - ); + return args.size() == mt::arity // + && inst.can_cast_to() // + && can_cast_all_uargs(args); } } @@ -6409,16 +6433,14 @@ namespace meta_hpp::detail using mt = method_traits; using mt_argument_types = typename mt::argument_types; - return std::invoke( - [](std::index_sequence) { - [[maybe_unused]] const auto make_argument = [](std::index_sequence) { - using P = type_list_at_t; - return argument{argument_state::make

(I, metadata_map{})}; - }; - return argument_list{make_argument(std::index_sequence{})...}; - }, - std::make_index_sequence() - ); + return [](std::index_sequence) { + [[maybe_unused]] const auto make_argument = [](index_constant) { + using P = type_list_at_t; + return argument{argument_state::make

(I, metadata_map{})}; + }; + return argument_list{make_argument(index_constant{})...}; + } + (std::make_index_sequence()); } } @@ -6695,15 +6717,15 @@ namespace meta_hpp::detail using class_type = typename ct::class_type; using argument_types = typename ct::argument_types; - constexpr bool as_object // - = std::is_copy_constructible_v // - && std::is_same_v; // + constexpr bool as_object // + = std::is_copy_constructible_v // + && std::is_same_v; - constexpr bool as_raw_ptr // - = std::is_same_v; // + constexpr bool as_raw_ptr // + = std::is_same_v; - constexpr bool as_shared_ptr // - = std::is_same_v; // + constexpr bool as_shared_ptr // + = std::is_same_v; static_assert(as_object || as_raw_ptr || as_shared_ptr); @@ -6712,27 +6734,24 @@ namespace meta_hpp::detail && "an attempt to call a constructor with an incorrect arity" ); - return std::invoke( - [args](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a constructor with incorrect argument types" - ); - - if constexpr ( as_object ) { - return make_uvalue(args[Is].cast>()...); - } - - if constexpr ( as_raw_ptr ) { - return std::make_unique(args[Is].cast>()...).release(); - } - - if constexpr ( as_shared_ptr ) { - return std::make_shared(args[Is].cast>()...); - } - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a constructor with incorrect argument types" ); + + return call_with_uargs(args, [](auto&&... all_args) -> uvalue { + if constexpr ( as_object ) { + return make_uvalue(META_HPP_FWD(all_args)...); + } + + if constexpr ( as_raw_ptr ) { + return std::make_unique(META_HPP_FWD(all_args)...).release(); + } + + if constexpr ( as_shared_ptr ) { + return std::make_shared(META_HPP_FWD(all_args)...); + } + }); } template < class_kind Class, typename... Args > @@ -6746,20 +6765,14 @@ namespace meta_hpp::detail && "an attempt to call a constructor with an incorrect arity" ); - return std::invoke( - [ mem, args ](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a constructor with incorrect argument types" - ); - - return std::construct_at( // - static_cast(mem), // - args[Is].cast>()... - ); - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a constructor with incorrect argument types" ); + + return call_with_uargs(args, [mem](auto&&... all_args) { + return std::construct_at(static_cast(mem), META_HPP_FWD(all_args)...); + }); } template < class_kind Class, typename... Args > @@ -6767,16 +6780,8 @@ namespace meta_hpp::detail using ct = constructor_traits; using argument_types = typename ct::argument_types; - if ( args.size() != ct::arity ) { - return false; - } - - return std::invoke( - [args](std::index_sequence) { - return (... && args[Is].can_cast_to>()); - }, - std::make_index_sequence() - ); + return args.size() == ct::arity // + && can_cast_all_uargs(args); } } @@ -6802,16 +6807,14 @@ namespace meta_hpp::detail using ct = constructor_traits; using ct_argument_types = typename ct::argument_types; - return std::invoke( - [](std::index_sequence) { - [[maybe_unused]] const auto make_argument = [](std::index_sequence) { - using P = type_list_at_t; - return argument{argument_state::make

(I, metadata_map{})}; - }; - return argument_list{make_argument(std::index_sequence{})...}; - }, - std::make_index_sequence() - ); + return [](std::index_sequence) { + [[maybe_unused]] const auto make_argument = [](index_constant) { + using P = type_list_at_t; + return argument{argument_state::make

(I, metadata_map{})}; + }; + return argument_list{make_argument(index_constant{})...}; + } + (std::make_index_sequence()); } } @@ -7152,7 +7155,7 @@ namespace meta_hpp::detail auto&& return_value = *variable_ptr; if constexpr ( as_copy ) { - return uvalue{std::forward(return_value)}; + return uvalue{META_HPP_FWD(return_value)}; } if constexpr ( as_ptr ) { diff --git a/headers/meta.hpp/meta_base/base.hpp b/headers/meta.hpp/meta_base/base.hpp index 887e59e..220469d 100644 --- a/headers/meta.hpp/meta_base/base.hpp +++ b/headers/meta.hpp/meta_base/base.hpp @@ -50,6 +50,10 @@ # include #endif +#if !defined(META_HPP_FWD) +# define META_HPP_FWD(v) std::forward(v) +#endif + #if !defined(META_HPP_ASSERT) # include # define META_HPP_ASSERT(...) assert(__VA_ARGS__) // NOLINT diff --git a/headers/meta.hpp/meta_base/type_list.hpp b/headers/meta.hpp/meta_base/type_list.hpp index 83180a1..a0058f1 100644 --- a/headers/meta.hpp/meta_base/type_list.hpp +++ b/headers/meta.hpp/meta_base/type_list.hpp @@ -13,6 +13,15 @@ namespace meta_hpp::detail template < typename... Types > struct type_list {}; + template < std::size_t I > + using size_constant = std::integral_constant; + + template < std::size_t I > + using index_constant = std::integral_constant; +} + +namespace meta_hpp::detail +{ template < std::size_t Index, typename TypeList > struct type_list_at; @@ -24,3 +33,15 @@ namespace meta_hpp::detail template < std::size_t Index, typename TypeList > using type_list_at_t = typename type_list_at::type; } + +namespace meta_hpp::detail +{ + template < typename TypeList > + struct type_list_arity; + + template < typename... Types > + struct type_list_arity> : size_constant {}; + + template < typename TypeList > + inline constexpr std::size_t type_list_arity_v = type_list_arity::value; +} diff --git a/headers/meta.hpp/meta_detail/value_utilities/uarg.hpp b/headers/meta.hpp/meta_detail/value_utilities/uarg.hpp index f800339..b96fd6b 100644 --- a/headers/meta.hpp/meta_detail/value_utilities/uarg.hpp +++ b/headers/meta.hpp/meta_detail/value_utilities/uarg.hpp @@ -346,3 +346,33 @@ namespace meta_hpp::detail META_HPP_THROW("bad argument cast"); } } + +namespace meta_hpp::detail +{ + template < typename ArgTypeList, typename F > + auto call_with_uargs(std::span args, F&& f) { + META_HPP_ASSERT(args.size() == type_list_arity_v); + return [ args, &f ](std::index_sequence) { + return f(args[Is].cast>()...); + } + (std::make_index_sequence>()); + } + + template < typename ArgTypeList > + bool can_cast_all_uargs(std::span args) { + META_HPP_ASSERT(args.size() == type_list_arity_v); + return [args](std::index_sequence) { + return (... && args[Is].can_cast_to>()); + } + (std::make_index_sequence>()); + } + + template < typename ArgTypeList > + bool can_cast_all_uargs(std::span args) { + META_HPP_ASSERT(args.size() == type_list_arity_v); + return [args](std::index_sequence) { + return (... && args[Is].can_cast_to>()); + } + (std::make_index_sequence>()); + } +} diff --git a/headers/meta.hpp/meta_states/constructor.hpp b/headers/meta.hpp/meta_states/constructor.hpp index 113dfc2..a796d1a 100644 --- a/headers/meta.hpp/meta_states/constructor.hpp +++ b/headers/meta.hpp/meta_states/constructor.hpp @@ -20,15 +20,15 @@ namespace meta_hpp::detail using class_type = typename ct::class_type; using argument_types = typename ct::argument_types; - constexpr bool as_object // - = std::is_copy_constructible_v // - && std::is_same_v; // + constexpr bool as_object // + = std::is_copy_constructible_v // + && std::is_same_v; - constexpr bool as_raw_ptr // - = std::is_same_v; // + constexpr bool as_raw_ptr // + = std::is_same_v; - constexpr bool as_shared_ptr // - = std::is_same_v; // + constexpr bool as_shared_ptr // + = std::is_same_v; static_assert(as_object || as_raw_ptr || as_shared_ptr); @@ -37,27 +37,24 @@ namespace meta_hpp::detail && "an attempt to call a constructor with an incorrect arity" ); - return std::invoke( - [args](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a constructor with incorrect argument types" - ); - - if constexpr ( as_object ) { - return make_uvalue(args[Is].cast>()...); - } - - if constexpr ( as_raw_ptr ) { - return std::make_unique(args[Is].cast>()...).release(); - } - - if constexpr ( as_shared_ptr ) { - return std::make_shared(args[Is].cast>()...); - } - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a constructor with incorrect argument types" ); + + return call_with_uargs(args, [](auto&&... all_args) -> uvalue { + if constexpr ( as_object ) { + return make_uvalue(META_HPP_FWD(all_args)...); + } + + if constexpr ( as_raw_ptr ) { + return std::make_unique(META_HPP_FWD(all_args)...).release(); + } + + if constexpr ( as_shared_ptr ) { + return std::make_shared(META_HPP_FWD(all_args)...); + } + }); } template < class_kind Class, typename... Args > @@ -71,20 +68,14 @@ namespace meta_hpp::detail && "an attempt to call a constructor with an incorrect arity" ); - return std::invoke( - [ mem, args ](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a constructor with incorrect argument types" - ); - - return std::construct_at( // - static_cast(mem), // - args[Is].cast>()... - ); - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a constructor with incorrect argument types" ); + + return call_with_uargs(args, [mem](auto&&... all_args) { + return std::construct_at(static_cast(mem), META_HPP_FWD(all_args)...); + }); } template < class_kind Class, typename... Args > @@ -92,16 +83,8 @@ namespace meta_hpp::detail using ct = constructor_traits; using argument_types = typename ct::argument_types; - if ( args.size() != ct::arity ) { - return false; - } - - return std::invoke( - [args](std::index_sequence) { - return (... && args[Is].can_cast_to>()); - }, - std::make_index_sequence() - ); + return args.size() == ct::arity // + && can_cast_all_uargs(args); } } @@ -127,16 +110,14 @@ namespace meta_hpp::detail using ct = constructor_traits; using ct_argument_types = typename ct::argument_types; - return std::invoke( - [](std::index_sequence) { - [[maybe_unused]] const auto make_argument = [](std::index_sequence) { - using P = type_list_at_t; - return argument{argument_state::make

(I, metadata_map{})}; - }; - return argument_list{make_argument(std::index_sequence{})...}; - }, - std::make_index_sequence() - ); + return [](std::index_sequence) { + [[maybe_unused]] const auto make_argument = [](index_constant) { + using P = type_list_at_t; + return argument{argument_state::make

(I, metadata_map{})}; + }; + return argument_list{make_argument(index_constant{})...}; + } + (std::make_index_sequence()); } } diff --git a/headers/meta.hpp/meta_states/function.hpp b/headers/meta.hpp/meta_states/function.hpp index a75529b..5ec8a29 100644 --- a/headers/meta.hpp/meta_states/function.hpp +++ b/headers/meta.hpp/meta_states/function.hpp @@ -20,17 +20,17 @@ namespace meta_hpp::detail using return_type = typename ft::return_type; using argument_types = typename ft::argument_types; - constexpr bool as_copy // - = std::is_copy_constructible_v // - && std::is_same_v; // + constexpr bool as_copy // + = std::is_copy_constructible_v // + && std::is_same_v; - constexpr bool as_void // - = std::is_void_v // - || std::is_same_v; // + constexpr bool as_void // + = std::is_void_v // + || std::is_same_v; - constexpr bool ref_as_ptr // - = std::is_reference_v // - && std::is_same_v; // + constexpr bool ref_as_ptr // + = std::is_reference_v // + && std::is_same_v; static_assert(as_copy || as_void || ref_as_ptr); @@ -39,31 +39,27 @@ namespace meta_hpp::detail && "an attempt to call a function with an incorrect arity" ); - return std::invoke( - [ function_ptr, args ](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a function with incorrect argument types" - ); - - if constexpr ( std::is_void_v ) { - function_ptr(args[Is].cast>()...); - return uvalue{}; - } else if constexpr ( std::is_same_v ) { - std::ignore = function_ptr(args[Is].cast>()...); - return uvalue{}; - } else { - return_type&& return_value = function_ptr(args[Is].cast>()...); - - if constexpr ( ref_as_ptr ) { - return uvalue{std::addressof(return_value)}; - } else { - return uvalue{std::forward(return_value)}; - } - } - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a function with incorrect argument types" ); + + return call_with_uargs(args, [function_ptr](auto&&... all_args) { + if constexpr ( std::is_void_v ) { + function_ptr(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( std::is_same_v ) { + std::ignore = function_ptr(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( !std::is_void_v ) { + return_type&& result = function_ptr(META_HPP_FWD(all_args)...); + return ref_as_ptr ? uvalue{std::addressof(result)} : uvalue{META_HPP_FWD(result)}; + } + }); } template < function_pointer_kind Function > @@ -71,16 +67,8 @@ namespace meta_hpp::detail using ft = function_traits; using argument_types = typename ft::argument_types; - if ( args.size() != ft::arity ) { - return false; - } - - return std::invoke( - [args](std::index_sequence) { - return (... && args[Is].can_cast_to>()); - }, - std::make_index_sequence() - ); + return args.size() == ft::arity // + && can_cast_all_uargs(args); } } @@ -103,16 +91,14 @@ namespace meta_hpp::detail using ft = function_traits; using ft_argument_types = typename ft::argument_types; - return std::invoke( - [](std::index_sequence) { - [[maybe_unused]] const auto make_argument = [](std::index_sequence) { - using P = type_list_at_t; - return argument{argument_state::make

(I, metadata_map{})}; - }; - return argument_list{make_argument(std::index_sequence{})...}; - }, - std::make_index_sequence() - ); + return [](std::index_sequence) { + [[maybe_unused]] const auto make_argument = [](index_constant) { + using P = type_list_at_t; + return argument{argument_state::make

(I, metadata_map{})}; + }; + return argument_list{make_argument(index_constant{})...}; + } + (std::make_index_sequence()); } } diff --git a/headers/meta.hpp/meta_states/member.hpp b/headers/meta.hpp/meta_states/member.hpp index 6c8b6f3..7fd9639 100644 --- a/headers/meta.hpp/meta_states/member.hpp +++ b/headers/meta.hpp/meta_states/member.hpp @@ -42,7 +42,7 @@ namespace meta_hpp::detail auto&& return_value = inst.cast().*member_ptr; if constexpr ( as_copy ) { - return uvalue{std::forward(return_value)}; + return uvalue{META_HPP_FWD(return_value)}; } if constexpr ( as_ptr ) { @@ -61,7 +61,7 @@ namespace meta_hpp::detail auto&& return_value = inst.cast().*member_ptr; if constexpr ( as_copy ) { - return uvalue{std::forward(return_value)}; + return uvalue{META_HPP_FWD(return_value)}; } if constexpr ( as_ptr ) { diff --git a/headers/meta.hpp/meta_states/method.hpp b/headers/meta.hpp/meta_states/method.hpp index 12f0630..6304e52 100644 --- a/headers/meta.hpp/meta_states/method.hpp +++ b/headers/meta.hpp/meta_states/method.hpp @@ -22,17 +22,17 @@ namespace meta_hpp::detail using qualified_type = typename mt::qualified_type; using argument_types = typename mt::argument_types; - constexpr bool as_copy // - = std::is_copy_constructible_v // - && std::is_same_v; // + constexpr bool as_copy // + = std::is_copy_constructible_v // + && std::is_same_v; - constexpr bool as_void // - = std::is_void_v // - || std::is_same_v; // + constexpr bool as_void // + = std::is_void_v // + || std::is_same_v; - constexpr bool ref_as_ptr // - = std::is_reference_v // - && std::is_same_v; // + constexpr bool ref_as_ptr // + = std::is_reference_v // + && std::is_same_v; static_assert(as_copy || as_void || ref_as_ptr); @@ -40,39 +40,33 @@ namespace meta_hpp::detail args.size() == mt::arity // && "an attempt to call a method with an incorrect arity" ); + META_HPP_ASSERT( // inst.can_cast_to() // && "an attempt to call a method with an incorrect instance type" ); - return std::invoke( - [ method_ptr, &inst, args ](std::index_sequence)->uvalue { - META_HPP_ASSERT( // - (... && args[Is].can_cast_to>()) // - && "an attempt to call a method with incorrect argument types" - ); - - if constexpr ( std::is_void_v ) { - (inst.cast().*method_ptr)(args[Is].cast>()...); - return uvalue{}; - } else if constexpr ( std::is_same_v ) { - std::ignore = (inst.cast().*method_ptr)(args[Is].cast>()... - ); - return uvalue{}; - } else { - return_type&& return_value = (inst.cast().*method_ptr)( - args[Is].cast>()... - ); - - if constexpr ( ref_as_ptr ) { - return uvalue{std::addressof(return_value)}; - } else { - return uvalue{std::forward(return_value)}; - } - } - }, - std::make_index_sequence() + META_HPP_ASSERT( // + can_cast_all_uargs(args) // + && "an attempt to call a method with incorrect argument types" ); + + return call_with_uargs(args, [method_ptr, &inst](auto&&... all_args) { + if constexpr ( std::is_void_v ) { + (inst.cast().*method_ptr)(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( std::is_same_v ) { + std::ignore = (inst.cast().*method_ptr)(META_HPP_FWD(all_args)...); + return uvalue{}; + } + + if constexpr ( !std::is_void_v ) { + return_type&& result = (inst.cast().*method_ptr)(META_HPP_FWD(all_args)...); + return ref_as_ptr ? uvalue{std::addressof(result)} : uvalue{META_HPP_FWD(result)}; + } + }); } template < method_pointer_kind Method > @@ -81,20 +75,9 @@ namespace meta_hpp::detail using qualified_type = typename mt::qualified_type; using argument_types = typename mt::argument_types; - if ( args.size() != mt::arity ) { - return false; - } - - if ( !inst.can_cast_to() ) { - return false; - } - - return std::invoke( - [args](std::index_sequence) { - return (... && args[Is].can_cast_to>()); - }, - std::make_index_sequence() - ); + return args.size() == mt::arity // + && inst.can_cast_to() // + && can_cast_all_uargs(args); } } @@ -117,16 +100,14 @@ namespace meta_hpp::detail using mt = method_traits; using mt_argument_types = typename mt::argument_types; - return std::invoke( - [](std::index_sequence) { - [[maybe_unused]] const auto make_argument = [](std::index_sequence) { - using P = type_list_at_t; - return argument{argument_state::make

(I, metadata_map{})}; - }; - return argument_list{make_argument(std::index_sequence{})...}; - }, - std::make_index_sequence() - ); + return [](std::index_sequence) { + [[maybe_unused]] const auto make_argument = [](index_constant) { + using P = type_list_at_t; + return argument{argument_state::make

(I, metadata_map{})}; + }; + return argument_list{make_argument(index_constant{})...}; + } + (std::make_index_sequence()); } } diff --git a/headers/meta.hpp/meta_states/variable.hpp b/headers/meta.hpp/meta_states/variable.hpp index e9ee592..81ab55b 100644 --- a/headers/meta.hpp/meta_states/variable.hpp +++ b/headers/meta.hpp/meta_states/variable.hpp @@ -34,7 +34,7 @@ namespace meta_hpp::detail auto&& return_value = *variable_ptr; if constexpr ( as_copy ) { - return uvalue{std::forward(return_value)}; + return uvalue{META_HPP_FWD(return_value)}; } if constexpr ( as_ptr ) {