1
0
forked from Alepha/Alepha

Merge branch 'delimiter-changes'

This commit is contained in:
2023-10-31 02:25:10 -04:00
6 changed files with 100 additions and 135 deletions

View File

@ -34,7 +34,7 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: istreamable_module
const auto commentChar= line.find( "#" ); const auto commentChar= line.find( "#" );
if( commentChar != std::string::npos ) line= line.substr( line.find( "#" ) ); if( commentChar != std::string::npos ) line= line.substr( line.find( "#" ) );
const auto delim= getFieldDelimiter( is ); const auto delim= getDelimiter( fieldDelimiter, is );
const auto tokens= split( line, delim ); const auto tokens= split( line, delim );
auto decomposed= Alepha::Reflection::tuplizeAggregate( istreamable ); auto decomposed= Alepha::Reflection::tuplizeAggregate( istreamable );

View File

@ -45,7 +45,7 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: ostreamable_module
// aggregates, so we'll go with this simple case for now... // aggregates, so we'll go with this simple case for now...
tuple_for_each( decomposed ) <=[&]( const auto &element ) tuple_for_each( decomposed ) <=[&]( const auto &element )
{ {
if( not first ) os << FieldDelimiter; if( not first ) os << fieldDelimiter;
first= false; first= false;
os << element; os << element;
}; };

View File

@ -31,8 +31,9 @@ namespace
stringify_specific( const Agg &agg, const std::string delim ) stringify_specific( const Agg &agg, const std::string delim )
{ {
std::ostringstream oss; std::ostringstream oss;
Alepha::IOStreams::setGlobalFieldDelimiter( "YOU SHOULD NOT SEE THIS" ); using Alepha::IOStreams::fieldDelimiter;
oss << Alepha::IOStreams::setFieldDelimiter( delim ); Alepha::IOStreams::setGlobalDelimiter( fieldDelimiter, "YOU SHOULD NOT SEE THIS" );
oss << Alepha::IOStreams::setDelimiter( fieldDelimiter, delim );
oss << agg; oss << agg;
return std::move( oss ).str(); return std::move( oss ).str();
} }
@ -41,7 +42,8 @@ namespace
stringify_global( const Agg &agg, const std::string delim ) stringify_global( const Agg &agg, const std::string delim )
{ {
std::ostringstream oss; std::ostringstream oss;
Alepha::IOStreams::setGlobalFieldDelimiter( delim ); using Alepha::IOStreams::fieldDelimiter;
Alepha::IOStreams::setGlobalDelimiter( fieldDelimiter, delim );
oss << agg; oss << agg;
return std::move( oss ).str(); return std::move( oss ).str();
} }
@ -81,8 +83,8 @@ static auto init= Alepha::Utility::enroll <=[]
[]( const Agg agg, const std::string delim ) []( const Agg agg, const std::string delim )
{ {
using Alepha::IOStreams::String; using Alepha::IOStreams::String;
using Alepha::IOStreams::setFieldDelimiter; using Alepha::IOStreams::fieldDelimiter;
return String{} << setFieldDelimiter( delim ) << agg << FinishString; return String{} << setDelimiter( fieldDelimiter, delim ) << agg << FinishString;
} }
> >
::Cases ::Cases

View File

@ -11,47 +11,53 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: stream_state
{ {
inline namespace exports inline namespace exports
{ {
template< typename Tag, typename Type, auto Default= [] { return Type{}; } > template< typename >
class StreamState; class StreamState;
} }
template< typename Tag, typename Type, auto Default > template< typename Type >
class exports::StreamState class exports::StreamState : boost::noncopyable
{ {
private: private:
static auto const int index= std::ios::xalloc();
index() std::function< Type () > build;
public:
explicit
StreamState( const std::function< Type () > build )
: build( build ) {}
private:
static Type *&
get_ptr( std::ios_base &ios, const int idx )
{ {
static const auto rv= std::ios::xalloc(); return reinterpret_cast< Type *& >( ios.pword( idx ) );
return rv;
} }
static Type *& Type *&
get_ptr( std::ios_base &ios ) get_ptr( std::ios_base &ios )
{ {
return reinterpret_cast< Type *& >( ios.pword( index() ) ); return get_ptr( ios, index );
} }
static void static void
destroy( std::ios_base &ios ) destroy( std::ios_base &ios, const int idx )
{ {
delete get_ptr( ios ); delete get_ptr( ios, idx );
get_ptr( ios )= nullptr; get_ptr( ios, idx )= nullptr;
} }
static void static void
callback_impl( const std::ios_base::event event, std::ios_base &ios, const int idx ) 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, idx );
if( event == std::ios_base::erase_event ) destroy( ios );
else if( event == std::ios_base::imbue_event ) else if( event == std::ios_base::imbue_event )
{ {
// Nothing to do... until I develop locale support. // Nothing to do... until I develop locale support.
} }
else if( event == std::ios_base::copyfmt_event ) else if( event == std::ios_base::copyfmt_event )
{ {
get_ptr( ios )= new Type{ get( ios ) }; get_ptr( ios, idx )= new Type{ *get_ptr( ios, idx ) };
} }
} }
@ -61,44 +67,57 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: stream_state
return callback_impl( event, ios, idx ); return callback_impl( event, ios, idx );
} }
static void void
init( std::ios_base &ios ) init( std::ios_base &ios )
{ {
if( not ios.iword( index() ) ) if( not ios.iword( index ) )
{ {
ios.iword( index() )= 1; ios.iword( index )= 1;
ios.register_callback( callback, index() ); ios.register_callback( callback, index );
} }
auto *&ptr= get_ptr( ios ); auto *&ptr= get_ptr( ios, index );
if( not ptr ) ptr= new Type{ Default() }; if( not ptr ) ptr= new Type{ build() };
} }
public: public:
static Type & Type &
get( std::ios_base &ios ) get( std::ios_base &ios )
{ {
init( ios ); init( ios );
return *get_ptr( ios ); return *get_ptr( ios );
} }
void
setDefault( const Type value )
{
build= [value] { return value; };
}
struct Setter struct Setter
{ {
StreamState *state;
const Type val; const Type val;
friend std::ostream & friend std::ostream &
operator << ( std::ostream &os, const Setter &s ) operator << ( std::ostream &os, const Setter &s )
{ {
StreamState::get( os )= s.val; s.state->get( os )= s.val;
return os; return os;
} }
friend std::istream & friend std::istream &
operator >> ( std::istream &is, const Setter &s ) operator >> ( std::istream &is, const Setter &s )
{ {
StreamState::get( is )= s.val; s.state->get( is )= s.val;
return is; return is;
} }
}; };
auto
makeSetter( const Type val )
{
return Setter{ this, val };
}
}; };
} }

View File

@ -12,117 +12,61 @@ static_assert( __cplusplus > 2020'00 );
#include <Alepha/IOStreams/StreamState.h> #include <Alepha/IOStreams/StreamState.h>
namespace Alepha::Hydrogen::IOStreams ::detail:: delimiters namespace Alepha::Hydrogen::IOStreams ::detail:: delimiters_m
{ {
inline namespace exports inline namespace exports {}
// Syntax I want to work:
//
// std::cout << someDelimiter;
// auto value= getDelimiter( someDelimiter, std::cin );
// std::cout << setDelimiter( someDelimiter, value );
struct Delimiter : boost::noncopyable
{ {
enum { FieldDelimiter }; StreamState< std::string > state;
enum { RecordDelimiter };
}
namespace C explicit
{ Delimiter( const std::string dflt )
const std::string defaultFieldDelimiter= "\t"; : state( [dflt] { return dflt; } ) {}
const char defaultRecordDelimiter= '\n';
}
namespace storage
{
inline StaticValue< std::optional< std::string > > globalFieldDelimiter;
inline StaticValue< std::optional< char > > globalRecordDelimiter;
}
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();
}
using FieldDelimiterState= StreamState< decltype( FieldDelimiter ), std::string, globalFieldDelimiter >;
inline std::ostream &
operator << ( std::ostream &os, decltype( FieldDelimiter ) )
{
return os << FieldDelimiterState::get( os );
}
namespace exports
{
auto
setFieldDelimiter( const std::string delim )
{
return FieldDelimiterState::Setter{ delim };
}
const auto &
getFieldDelimiter( std::ios_base &ios )
{
return FieldDelimiterState::get( ios );
}
}
inline const int recordIndex= std::ios::xalloc();
inline void
setRecordDelimiterOnIOS( std::ios &ios, const char ch )
{
ios.iword( recordIndex )= ch;
}
inline char
getRecordDelimiter( std::ios &ios )
{
if( ios.iword( recordIndex ) == 0 ) setRecordDelimiterOnIOS( ios, globalRecordDelimiter() );
return ios.iword( recordIndex );
}
inline std::ostream &
operator << ( std::ostream &os, decltype( RecordDelimiter ) )
{
return os << getRecordDelimiter( os );
}
struct RecordDelimiterSetter
{
const char ch;
friend std::ostream &
operator << ( std::ostream &os, const RecordDelimiterSetter &s )
{
setRecordDelimiterOnIOS( os, s.ch );
return os;
}
}; };
namespace exports namespace exports
{ {
auto inline Delimiter fieldDelimiter{ "\t" };
setRecordDelimiter( const char ch ) inline Delimiter recordDelimiter{ "\n" };
}
inline std::ostream &
operator << ( std::ostream &os, Delimiter &delim )
{
const auto &s= delim.state.get( os );
return os << s;
}
namespace exports
{
inline std::string
getDelimiter( Delimiter &delim, std::ios_base &ios )
{ {
return RecordDelimiterSetter{ ch }; return delim.state.get( ios );
}
inline auto
setDelimiter( Delimiter &delim, const std::string s )
{
return delim.state.makeSetter( s );
}
inline void
setGlobalDelimiter( Delimiter &delim, const std::string s )
{
return delim.state.setDefault( s );
} }
} }
} }
namespace Alepha::Hydrogen::IOStreams::inline exports::inline delimiters namespace Alepha::Hydrogen::IOStreams::inline exports::inline delimiters_m
{ {
using namespace detail::delimiters::exports; using namespace detail::delimiters_m::exports;
} }

View File

@ -31,14 +31,14 @@ namespace
std::string std::string
roundTripString( const std::string text, const std::string delim ) roundTripString( const std::string text, const std::string delim )
{ {
using namespace Alepha::IOStreams::exports::delimiters; using namespace Alepha::IOStreams::exports::delimiters_m;
std::istringstream iss{ text }; std::istringstream iss{ text };
Agg agg; Agg agg;
iss >> setFieldDelimiter( delim ) >> agg; iss >> setDelimiter( fieldDelimiter, delim ) >> agg;
std::ostringstream oss; std::ostringstream oss;
oss << setFieldDelimiter( delim ) << agg; oss << setDelimiter( fieldDelimiter, delim ) << agg;
return oss.str(); return oss.str();
} }