From 1cba0026b9ea7581902017074071a0da1a2e7a94 Mon Sep 17 00:00:00 2001 From: Vincent Chabannes Date: Fri, 12 Nov 2021 10:10:35 +0100 Subject: [PATCH] add make_default_argument_invocable + review create method --- include/napp/na.hpp | 73 ++++++++++++++++++++++++++++++++++-------- test/unit-function.cpp | 4 ++- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/include/napp/na.hpp b/include/napp/na.hpp index 0da6146..527d3b2 100644 --- a/include/napp/na.hpp +++ b/include/napp/na.hpp @@ -249,17 +249,54 @@ template constexpr bool is_argument_identifier_v = std::is_base_of_v>; +//! make a new arg type with value \t +template +constexpr auto/*type*/ make_argument(T&& t) +{ + return detail::infer_type_t>(std::forward(t)); +} + +//! make a new arg type with value \t +template ,bool> = true > +constexpr auto make_argument(ArgIdentifierType &&, T&& t) +{ + return make_argument::identifier_type>( std::forward(t) ); +} + + + template struct DefaultArgument : named_argument_t { + using super_type = named_argument_t; DefaultArgument() = delete; template constexpr DefaultArgument( U && v ) : named_argument_t( std::forward( v ) ) {} DefaultArgument( DefaultArgument const& ) = delete; DefaultArgument( DefaultArgument && ) = default;//delete; + + constexpr super_type const& to_named_argument() const & { return *this; } + constexpr super_type & to_named_argument() & { return *this; } + constexpr super_type && to_named_argument() && { return std::move(*this); } }; +template +struct DefaultArgumentInvocable : named_argument_t +{ + static_assert( std::is_invocable_v, "is_invocable_v should be true" ); + + DefaultArgumentInvocable() = delete; + template + constexpr DefaultArgumentInvocable( U && v ) : named_argument_t( std::forward( v ) ) {} + DefaultArgumentInvocable( DefaultArgumentInvocable const& ) = delete; + DefaultArgumentInvocable( DefaultArgumentInvocable && ) = default;//delete; + + constexpr auto to_named_argument() const & { return make_argument( this->value()() ); } + constexpr auto to_named_argument() & { return make_argument( this->value()() ); } + constexpr auto to_named_argument() && { return make_argument( std::move(*this).value()() ); } +}; + template constexpr auto make_default_argument( ValueType && val ) { @@ -272,18 +309,17 @@ constexpr auto make_default_argument( ArgIdentifierType &&, ValueType && val ) return make_default_argument::identifier_type>( std::forward( val ) ); } -//! make a new arg type with value \t -template -constexpr auto/*type*/ make_argument(T&& t) + +template +constexpr auto make_default_argument_invocable( ValueType && val ) { - return detail::infer_type_t>(std::forward(t)); + return DefaultArgumentInvocable{ std::forward( val ) }; } -//! make a new arg type with value \t -template ,bool> = true > -constexpr auto make_argument(ArgIdentifierType &&, T&& t) +template ,bool> = true > +constexpr auto make_default_argument_invocable( ArgIdentifierType &&, ValueType && val ) { - return make_argument::identifier_type>( std::forward(t) ); + return make_default_argument_invocable::identifier_type>( std::forward( val ) ); } @@ -301,6 +337,7 @@ constexpr auto&& getImplBIS(TupleType && t) } +#if 0 template constexpr auto&& getImplBIS2(TupleType && t, DefaultArgsTupleType && defaultArgs) { @@ -315,6 +352,8 @@ constexpr auto&& getImplBIS2(TupleType && t, DefaultArgsTupleType && defaultArgs return getImplBIS2( std::forward(t ), std::forward( defaultArgs ) ); } +#endif + #if 0 template constexpr auto /*decltype(auto)*/ newtuple( DefaultArgsTupleType && defaultArgs, RetTupleType && ret ) @@ -342,7 +381,7 @@ constexpr decltype(auto) to_tuple_args_not_present( DefaultArgsTupleType && defa return to_tuple_args_not_present( std::forward( defaultArgs ), std::forward( ret )... ); else return to_tuple_args_not_present( std::forward( defaultArgs ), - std::get( std::forward( defaultArgs ) ), std::forward( ret )... ); + std::get( std::forward( defaultArgs ) ).to_named_argument(), std::forward( ret )... ); } #endif @@ -600,15 +639,16 @@ struct arguments : arguments_base } +#if 0 template constexpr auto && getOptionalArgument( DefaultArgsTupleType && defaultArgs ) //&& { return detail::getImplBIS2( std::move( this->values ), std::forward( defaultArgs ) ); } - +#endif template - constexpr auto add_default_arguments( DefaultArgType && ... defaultArg ) && + constexpr decltype(auto) add_default_arguments( DefaultArgType && ... defaultArg ) && { //static constexpr bool __matches[sizeof...(_Args)] = {is_same<_T1, _Args>::value...}; using self_type = arguments; @@ -623,18 +663,23 @@ struct arguments : arguments_base template static constexpr auto create( GivenArgsType && givenArgs, DefaultArgType && ... defaultArg ) { - return createImpl( std::forward( givenArgs ), std::make_tuple( std::forward( defaultArg ) ... ) ); - } + //return createImpl( std::forward( givenArgs ), std::make_tuple( std::forward( defaultArg ) ... ) ); + return arguments{ std::forward( givenArgs).add_default_arguments( std::forward( defaultArg ) ... ) }; + } +#if 0 private : template static constexpr auto createImpl( TheType && a, DefaultArgsTupleType && defaultParams ) { //return args{ std::forward( a ).template getArgument() ... }; +#if 1 return arguments{ std::forward( a ).template getOptionalArgument( std::forward( defaultParams ) ) ... }; +#endif + //return arguments{ std::forward( a ).add_default_arguments( std::forward( defaultParams ) ... ) }; } - +#endif private : diff --git a/test/unit-function.cpp b/test/unit-function.cpp index 43a3fe1..bfbf193 100644 --- a/test/unit-function.cpp +++ b/test/unit-function.cpp @@ -182,7 +182,9 @@ std::string test4( Ts && ... v ) std::string default_ln = "default_ln"; return test4( test4_arg_type::create( std::move(args), NA::make_default_argument( "default_fn" ), - NA::make_default_argument(_last_name, default_ln) + //NA::make_default_argument(_last_name, default_ln) + //NA::make_default_argument_invocable( [](){ return "default_fn"; } ), + NA::make_default_argument_invocable(_last_name, [&default_ln]() -> decltype(auto){ return default_ln; }) ) ); }