1
0
forked from Alepha/Alepha

Merge branch 'master' of github.com:adamlsd/Alepha

This commit is contained in:
2023-10-26 00:00:06 -04:00
20 changed files with 351 additions and 69 deletions

View File

@ -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 )

View File

@ -2,6 +2,8 @@ static_assert( __cplusplus > 2020'00 );
#pragma once
#include <Alepha/Alepha.h>
#include <concepts>
#include <type_traits>
#include <iosfwd>
@ -9,7 +11,7 @@ static_assert( __cplusplus > 2020'00 );
#include "meta.h"
#include "function_traits.h"
namespace Alepha::inline Cavorite ::detail:: core_concepts
namespace Alepha::Hydrogen ::detail:: core_concepts
{
inline namespace exports
{
@ -278,7 +280,7 @@ namespace Alepha::inline Cavorite ::detail:: core_concepts
}
}
namespace Alepha::Cavorite::inline exports::inline core_concepts
namespace Alepha::Hydrogen::inline exports::inline core_concepts
{
using namespace detail::core_concepts::exports;
}

View File

@ -1,2 +1 @@
link_libraries( unit-test )
unit_test( 0 )

View File

@ -10,6 +10,7 @@ static_assert( __cplusplus > 2020'00 );
#include <Alepha/auto_comparable.h>
#include <Alepha/IOStreams/delimiters.h>
#include <Alepha/IOStreams/Stream.h>
namespace
{
@ -27,24 +28,78 @@ namespace
auto
stringify( const Agg &agg, const char delim )
stringify_specific( const Agg &agg, const std::string delim )
{
std::ostringstream oss;
Alepha::IOStreams::setGlobalFieldDelimiter( "YOU SHOULD NOT SEE THIS" );
oss << Alepha::IOStreams::setFieldDelimiter( delim );
oss << agg;
return std::move( oss ).str();
}
auto
stringify_global( const Agg &agg, const std::string delim )
{
std::ostringstream oss;
Alepha::IOStreams::setGlobalFieldDelimiter( delim );
oss << agg;
return std::move( oss ).str();
}
auto
stringify_default( const Agg &agg )
{
std::ostringstream oss;
oss << agg;
return std::move( oss ).str();
}
}
static auto init= Alepha::Utility::enroll <=[]
{
using namespace Alepha::Testing::exports;
using namespace Alepha::Testing::literals::test_literals;
using namespace Alepha::IOStreams::exports::stream;
"Simple OStream"_test <=TableTest< stringify >
"Simple OStream (default delimiter)"_test <=TableTest< stringify_default >
::Cases
{
{ "smoke test", { { 1, 2, 3 }, '\t' }, { "1\t2\t3" } },
{ "smoke test", { { 1, 2, 3 }, ',' }, { "1,2,3" } },
{ "smoke test", { { 1, 2, 3 } }, { "1\t2\t3" } },
};
"Simple OStream (specific delimiter)"_test <=TableTest< stringify_specific >
::Cases
{
{ "smoke test", { { 1, 2, 3 }, "\t" }, { "1\t2\t3" } },
{ "smoke test", { { 1, 2, 3 }, "," }, { "1,2,3" } },
{ "smoke test", { { 1, 2, 3 }, ";;" }, { "1;;2;;3" } },
{ "smoke test", { { 1, 2, 3 }, ", " }, { "1, 2, 3" } },
};
"Simple OStream (stream builder)"_test <=TableTest
<
[]( const Agg agg, const std::string delim )
{
using Alepha::IOStreams::Stream;
using Alepha::IOStreams::setFieldDelimiter;
return Stream{} << setFieldDelimiter( delim ) << agg << FinishString;
}
>
::Cases
{
{ "smoke test", { { 1, 2, 3 }, "\t" }, { "1\t2\t3" } },
{ "smoke test", { { 1, 2, 3 }, "," }, { "1,2,3" } },
{ "smoke test", { { 1, 2, 3 }, ";;" }, { "1;;2;;3" } },
{ "smoke test", { { 1, 2, 3 }, ", " }, { "1, 2, 3" } },
};
"Simple OStream (global delimiter)"_test <=TableTest< stringify_global >
::Cases
{
{ "smoke test", { { 1, 2, 3 }, "\t" }, { "1\t2\t3" } },
{ "smoke test", { { 1, 2, 3 }, "," }, { "1,2,3" } },
{ "smoke test", { { 1, 2, 3 }, ";;" }, { "1;;2;;3" } },
{ "smoke test", { { 1, 2, 3 }, ", " }, { "1, 2, 3" } },
};
};

View File

@ -1,3 +1 @@
link_libraries( unit-test )
unit_test( 0 )

61
IOStreams/Stream.h Normal file
View File

@ -0,0 +1,61 @@
static_assert( __cplusplus > 2020'00 );
#pragma once
#include <Alepha/Alepha.h>
#include <sstream>
#include <Alepha/Concepts.h>
namespace Alepha::Hydrogen::IOStreams ::detail:: stream
{
inline namespace exports
{
class Stream;
enum { FinishString };
std::string stringify( const Alepha::OStreamable auto &item, Alepha::OStreamable auto && ... params );
}
class exports::Stream
{
private:
// TODO: We need the exception throwing capabilities of the
// `boost::lexical_cast` operation. But this stream technique
// lets us build strings using stream modifiers and manipulators,
// which `boost::lexical_cast` doesn't support.
std::ostringstream oss;
public:
Stream &&
operator << ( const Alepha::OStreamable auto &t ) &&
{
oss << t;
return std::move( *this );
}
std::string
operator << ( decltype( FinishString ) ) &&
{
return std::move( oss ).str();
}
operator std::string () &&
{
return std::move( *this ) << FinishString;
}
};
inline std::string
exports::stringify( const Alepha::OStreamable auto &item, Alepha::OStreamable auto && ... params )
{
return ( Stream{} << ... << params ) << item << FinishString;
}
}
namespace Alepha::Hydrogen::IOStreams::inline exports::inline stream
{
using namespace detail::stream::exports;
}

101
IOStreams/StreamState.h Normal file
View File

@ -0,0 +1,101 @@
static_assert( __cplusplus > 2020'00 );
#pragma once
#include <ios>
#include <exception>
#include <Alepha/Alepha.h>
namespace Alepha::Hydrogen::IOStreams ::detail:: stream_state
{
inline namespace exports
{
template< typename Tag, typename Type, auto Default= [] { return Type{}; } >
class StreamState;
}
template< typename Tag, typename Type, auto Default >
class exports::StreamState
{
private:
static auto
index()
{
static const auto rv= std::ios::xalloc();
return rv;
}
static Type *&
get_ptr( std::ios_base &ios )
{
return reinterpret_cast< Type *& >( ios.pword( index() ) );
}
static void
destroy( std::ios_base &ios )
{
delete get_ptr( ios );
get_ptr( ios )= nullptr;
}
static void
callback_impl( const std::ios_base::event event, std::ios_base &ios, const int idx )
{
if( idx != index() ) throw std::logic_error( "Wrong index." );
if( event == std::ios_base::erase_event ) destroy( ios );
else if( event == std::ios_base::imbue_event )
{
// Nothing to do... until I develop locale support.
}
else if( event == std::ios_base::copyfmt_event )
{
get_ptr( ios )= new Type{ get( ios ) };
}
}
static void
callback( const std::ios_base::event event, std::ios_base &ios, const int idx ) noexcept
{
return callback_impl( event, ios, idx );
}
static void
init( std::ios_base &ios )
{
if( not ios.iword( index() ) )
{
ios.iword( index() )= 1;
ios.register_callback( callback, index() );
}
auto *&ptr= get_ptr( ios );
if( not ptr ) ptr= new Type{ Default() };
}
public:
static Type &
get( std::ios_base &ios )
{
init( ios );
return *get_ptr( ios );
}
struct Setter
{
const Type val;
friend std::ostream &
operator << ( std::ostream &os, const Setter &s )
{
StreamState::get( os )= s.val;
return os;
}
};
};
}
namespace Alepha::Hydrogen::IOStreams::inline exports::inline stream_state
{
using namespace detail::stream_state::exports;
}

View File

@ -10,6 +10,8 @@ static_assert( __cplusplus > 2020'00 );
#include <Alepha/StaticValue.h>
#include <Alepha/IOStreams/StreamState.h>
namespace Alepha::Hydrogen::IOStreams ::detail:: delimiters
{
inline namespace exports
@ -20,71 +22,53 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: delimiters
namespace C
{
const char defaultFieldDelimiter= '\t';
const std::string defaultFieldDelimiter= "\t";
const char defaultRecordDelimiter= '\n';
}
namespace storage
{
inline StaticValue< std::optional< char > > globalFieldDelimiter;
inline StaticValue< std::optional< std::string > > globalFieldDelimiter;
inline StaticValue< std::optional< char > > globalRecordDelimiter;
}
inline char
inline std::string
globalFieldDelimiter()
{
if( not storage::globalFieldDelimiter().has_value() ) storage::globalFieldDelimiter()= C::defaultFieldDelimiter;
return storage::globalFieldDelimiter().value();
}
namespace exports
{
inline void
setGlobalFieldDelimiter( const std::string delim )
{
storage::globalFieldDelimiter()= delim;
}
}
inline char
globalRecordDelimiter()
{
if( not storage::globalRecordDelimiter().has_value() ) storage::globalRecordDelimiter()= C::defaultRecordDelimiter;
return storage::globalRecordDelimiter().value();
}
inline const int fieldIndex= std::ios::xalloc();
inline void
setFieldDelimiterOnIOS( std::ios &ios, const char ch )
{
ios.iword( fieldIndex )= ch;
}
inline char
getFieldDelimiter( std::ios &ios )
{
if( ios.iword( fieldIndex ) == 0 ) setFieldDelimiterOnIOS( ios, globalFieldDelimiter() );
return ios.iword( fieldIndex );
}
using FieldDelimiterState= StreamState< decltype( FieldDelimiter ), std::string, globalFieldDelimiter >;
inline std::ostream &
operator << ( std::ostream &os, decltype( FieldDelimiter ) )
{
return os << getFieldDelimiter( os );
return os << FieldDelimiterState::get( os );
}
struct FieldDelimiterSetter
{
const char ch;
friend std::ostream &
operator << ( std::ostream &os, const FieldDelimiterSetter &s )
{
setFieldDelimiterOnIOS( os, s.ch );
return os;
}
};
namespace exports
{
auto
setFieldDelimiter( const char ch )
setFieldDelimiter( const std::string delim )
{
return FieldDelimiterSetter{ ch };
return FieldDelimiterState::Setter{ delim };
}
}

View File

@ -89,10 +89,11 @@ static_assert( __cplusplus > 2020'00 );
#include <boost/lexical_cast.hpp>
#include <Alepha/Alepha.h>
#include <Alepha/Concepts.h>
#include <Alepha/string_algorithms.h>
#include <Alepha/IOStreams/Stream.h>
#include <Alepha/Utility/evaluation_helpers.h>
namespace Alepha::inline Cavorite ::detail:: program_options
@ -265,7 +266,7 @@ namespace Alepha::inline Cavorite ::detail:: program_options
// variable's value in C++ at runtime.
auto defaultBuilder= [&value]
{
auto text= boost::lexical_cast< std::string >( value );
auto text= IOStreams::stringify( value );
if( text.find_first_of( " \n\t" ) != std::string::npos )
{

View File

@ -29,6 +29,8 @@ static_assert( __cplusplus > 2020'00 );
#include <Alepha/function_traits.h>
#include <Alepha/template_for_each.h>
#include <Alepha/IOStreams/Stream.h>
#include <Alepha/Utility/evaluation_helpers.h>
#include <Alepha/TotalOrder.h>
@ -122,7 +124,7 @@ namespace Alepha::Hydrogen::Testing ::detail:: table_test
}
else if constexpr( Meta::is_ostreamable_v< T > )
{
return boost::lexical_cast< std::string >( v );
return IOStreams::stringify( v );
}
else if constexpr( Meta::is_optional_v< T > )
{

View File

@ -1,2 +1 @@
link_libraries( unit-test )
unit_test( 0 )

View File

@ -5,6 +5,10 @@ add_compile_options( -fdiagnostics-column-unit=byte )
add_compile_options( -fconcepts-diagnostics-depth=0 )
include_directories( ${CMAKE_BINARY_DIR} . )
if( "${CMAKE_BUILD_TYPE}" STREQUAL "Debug" )
add_compile_options( -O0 )
add_compile_options( -g )
endif()
list( APPEND CMAKE_CTEST_ARGUMENTS "-VV" )

View File

@ -6,7 +6,7 @@ static_assert( __cplusplus > 2020'00 );
#include <type_traits>
#include <functional>
namespace Alepha::inline Cavorite ::detail:: function_traits_module
namespace Alepha::Hydrogen ::detail:: function_traits_module
{
inline namespace exports
{
@ -76,7 +76,7 @@ namespace Alepha::inline Cavorite ::detail:: function_traits_module
{};
}
namespace Alepha::Cavorite::inline exports::inline function_traits_module
namespace Alepha::Hydrogen::inline exports::inline function_traits_module
{
using namespace detail::function_traits_module::exports;
}

4
meta.h
View File

@ -16,7 +16,7 @@ static_assert( __cplusplus > 2020'00 );
#include "type_lisp.h"
namespace Alepha::inline Cavorite ::detail:: meta
namespace Alepha::Hydrogen ::detail:: meta
{
inline namespace exports
{
@ -71,7 +71,7 @@ namespace Alepha::inline Cavorite ::detail:: meta
}
}
namespace Alepha::inline Cavorite::inline exports::inline meta
namespace Alepha::Hydrogen::inline exports::inline meta
{
using namespace detail::meta::exports;
}

View File

@ -1,3 +1 @@
link_libraries( unit-test )
unit_test( 0 )

View File

@ -12,10 +12,12 @@ static_assert( __cplusplus > 2020'00 );
#include <vector>
#include <string>
#include <optional>
#include <tuplle>
#include <tuple>
#include <boost/lexical_cast.hpp>
#include <Alepha/IOStreams/Stream.h>
#include "meta.h"
#include "error.h"
#include "Concepts.h"
@ -31,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 );
@ -48,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( ( clarification.empty() ? "" : ( clarification + ": " ) )
+ "Argument count mismatch. "
+ boost::lexical_cast< std::string >( remaining ) + " remaining "
+ boost::lexical_cast< std::string >( processed ) + " processed" ) {}
message( IOStreams::Stream{} << ( clarification.empty() ? "" : ( clarification + ": " ) )
<< "Argument count mismatch. "
<< remaining << " remaining "
<< processed << " processed" ) {}
const char *
@ -72,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 >
@ -99,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 ),
boost::lexical_cast< type, std::string > );
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
{
@ -129,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 );
}
}

44
tuplize_args.test/0.cc Normal file
View File

@ -0,0 +1,44 @@
#include "../tuplize_args.h"
#include <Alepha/Testing/test.h>
#include <Alepha/Testing/TableTest.h>
#include <Alepha/Utility/evaluation_helpers.h>
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 } },
};
};

View File

@ -0,0 +1 @@
unit_test( 0 )

View File

@ -4,7 +4,7 @@ static_assert( __cplusplus > 2020'00 );
#include <type_traits>
namespace Alepha::inline Cavorite ::detail:: type_lisp
namespace Alepha::Hydrogen ::detail:: type_lisp
{
inline namespace exports {}
@ -83,9 +83,39 @@ namespace Alepha::inline Cavorite ::detail:: type_lisp
template< typename Element >
constexpr bool list_contains_v< Nil, Element >{};
}
template< typename Element, typename List >
struct cons;
template< typename Element, typename ... Members >
struct cons< Element, TypeList< Members... > >
{
using type= TypeList< Element, Members... >;
};
namespace exports
{
template< typename Element, typename List >
using cons_t= typename cons< Element, List >::type;
}
template< typename Tuple >
struct list_from_tuple;
namespace exports
{
template< typename Tuple >
using list_from_tuple_t= typename list_from_tuple< Tuple >::type;
}
template< typename ... Members >
struct list_from_tuple< std::tuple< Members... > >
{
using type= TypeList< Members... >;
};
}
namespace Alepha::Cavorite::inline exports::inline type_lisp
namespace Alepha::Hydrogen::inline exports::inline type_lisp
{
using namespace detail::type_lisp::exports;
}

View File

@ -1,2 +1 @@
link_libraries( unit-test )
unit_test( 0 )