diff --git a/CMakeLists.txt b/CMakeLists.txt index 2ce4949..f28bb98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ add_subdirectory( comparisons.test ) add_subdirectory( Exception.test ) add_subdirectory( word_wrap.test ) add_subdirectory( string_algorithms.test ) +add_subdirectory( tuplize_args.test ) # Sample applications add_executable( example example.cc ) diff --git a/tuplize_args.h b/tuplize_args.h index 704e0b7..0cb729c 100644 --- a/tuplize_args.h +++ b/tuplize_args.h @@ -33,7 +33,7 @@ namespace Alepha::Hydrogen ::detail:: tuplize_args * * Iterates thru the runtime arguments and attempts to parse them to the compiletime specified types. */ - template< typename Tuple T > + template< Tuple T > auto tuplizeArgs( const std::vector< std::string > &args ); @@ -50,10 +50,10 @@ namespace Alepha::Hydrogen ::detail:: tuplize_args explicit ArityMismatchError( const std::size_t remaining, const std::size_t processed, const std::string &clarification= "" ) : remaining_( remaining ), processed_( processed ), clarification( clarification ), - message( IOStream::Stream{} << ( clarification.empty() ? "" : ( clarification + ": " ) ) + message( IOStreams::Stream{} << ( clarification.empty() ? "" : ( clarification + ": " ) ) << "Argument count mismatch. " << remaining << " remaining " - << processed j< " processed" ) {} + << processed << " processed" ) {} const char * @@ -74,7 +74,7 @@ namespace Alepha::Hydrogen ::detail:: tuplize_args // TODO: Expand this to handle compiletime-bound defaulted values. template< typename Type > - concept Omittable= Optional< Type >; + concept Omittable= is_optional_v< Type >; template< TypeListType list > tuple_from_list_t< list > @@ -101,18 +101,19 @@ namespace Alepha::Hydrogen ::detail:: tuplize_args else return tuplizeArgsBackend( cons_t< typename first::value_type, tail >{}, args, offset ); } else if( offset >= args.size() ) throw ArityMismatchError{ args.size() - offset, offset, "no more arguments left" }; - else if constexpr( is_vector_v< first > ) + else if constexpr( Vector< first > ) { static_assert( std::is_same_v< cdr_t< list >, Nil >, "A vector is only permissible as the final argument." ); if( args.size() <= offset ) { - throw ArityMismatchError{ args.size() - offset, offset, "a vector/list requires at least one runtime argument." } + throw ArityMismatchError{ args.size() - offset, offset, "a vector/list requires at least one runtime argument." }; } + using first_type= typename first::value_type; - const std::vector< std::string > rv; + std::vector< first_type > rv; std::transform( begin( args ) + offset, end( args ), back_inserter( rv ), - IOStreams::stringify< type > ); - return std::tuple_cat( std::tuple{ arv }, tuplizeArgsBackend( tail{}, args, offset + rv.size() ) ); + boost::lexical_cast< first_type, std::string > ); + return std::tuple_cat( std::tuple{ rv }, tuplizeArgsBackend( tail{}, args, offset + rv.size() ) ); } else { @@ -131,7 +132,7 @@ namespace Alepha::Hydrogen ::detail:: tuplize_args auto exports::tuplizeArgs( const std::vector< std::string > &args ) { - return tuplizeArgsBackend( list_from_tuple_t< T > args ); + return tuplizeArgsBackend( list_from_tuple_t< T >{}, args ); } } diff --git a/tuplize_args.test/0.cc b/tuplize_args.test/0.cc new file mode 100644 index 0000000..d5d5cb1 --- /dev/null +++ b/tuplize_args.test/0.cc @@ -0,0 +1,44 @@ +#include "../tuplize_args.h" + +#include +#include + +#include + +static auto init= Alepha::Utility::enroll <=[] +{ + using namespace Alepha::Testing::exports; + using namespace Alepha::Utility::exports::evaluation_helpers; + + "A basic tuplization example"_test <=TableTest + < + Alepha::tuplizeArgs< std::tuple< int, std::string, int, char > > + > + ::Cases + { + { "Smoke example", { { "1", "Hello", "42", "x" } }, { 1, "Hello", 42, 'x' } }, + }; + + "Do trailing vectors permit variadics?"_test <=TableTest + < + Alepha::tuplizeArgs< std::tuple< int, std::string, std::vector< int > > > + > + ::Cases + { + { "One extra argument", { { "1", "Hello", "2" } }, { 1, "Hello", { 2 } } }, + { "Two extra arguments", { { "1", "Hello", "2", "3" } }, { 1, "Hello", { 2, 3 } } }, + { "Three extra arguments", { { "1", "Hello", "2", "3", "4" } }, { 1, "Hello", { 2, 3, 4 } } }, + }; + + "Does trailing optional stacked permit variadics??"_test <=TableTest + < + Alepha::tuplizeArgs< std::tuple< std::optional< int >, std::optional< std::string >, std::optional< int > > > + > + ::Cases + { + { "No arguments", { {} }, { std::nullopt, std::nullopt, std::nullopt } }, + { "One argument", { { "1" } }, { 1, std::nullopt, std::nullopt } }, + { "Two arguments", { { "1", "Hello" } }, { 1, "Hello", std::nullopt } }, + { "Three arguments", { { "1", "Hello", "2" } }, { 1, "Hello", 2 } }, + }; +}; diff --git a/tuplize_args.test/CMakeLists.txt b/tuplize_args.test/CMakeLists.txt new file mode 100644 index 0000000..b099603 --- /dev/null +++ b/tuplize_args.test/CMakeLists.txt @@ -0,0 +1 @@ +unit_test( 0 )