1
0
forked from Alepha/Alepha

I started a weird rewrite, but I'll likely abandon it.

But I'll keep this commit in my history, in case I want to try again.
This commit is contained in:
2023-10-31 01:44:34 -04:00
parent 3a5f6367b9
commit 4a74921cb8
2 changed files with 97 additions and 51 deletions

View File

@ -15,43 +15,42 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: stream_state
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()
{
static const auto rv= std::ios::xalloc();
return rv;
}
static Type *& static Type *&
get_ptr( std::ios_base &ios, const int idx )
{
return reinterpret_cast< Type *& >( ios.pword( idx ) );
}
Type *&
get_ptr( std::ios_base &ios ) get_ptr( std::ios_base &ios )
{ {
return reinterpret_cast< Type *& >( ios.pword( index() ) ); 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( ios, idx ) };
} }
} }
@ -61,20 +60,20 @@ 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( idx ) )
{ {
ios.iword( index() )= 1; ios.iword( index() )= 1;
ios.register_callback( callback, index() ); ios.register_callback( callback, idx );
} }
auto *&ptr= get_ptr( ios ); auto *&ptr= get_ptr( ios, idx );
if( not ptr ) ptr= new Type{ Default() }; if( not ptr ) ptr= new Type{ Default() };
} }
public: public:
static Type & Type &
get( std::ios_base &ios ) get( std::ios_base &ios )
{ {
init( ios ); init( ios );
@ -83,19 +82,20 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: stream_state
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.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.get( is )= s.val;
return is; return is;
} }
}; };

View File

@ -12,55 +12,101 @@ 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 {}
// Syntax I want to work:
//
// std::cout << someDelimiter;
// auto value= getDelimiter( someDelimiter, std::cin );
// std::cout << setDelimiter( someDelimiter, value );
struct Delimiter : boost::noncopyable
{
std::string current;
StreamState state;
explicit
Delimiter( const std::string current )
: current( current )
{
}
};
template< ConstexprString s >
using CreateDelimiter= void ( const DelimiterParamType< s > & );
template< typename T >
constexpr is_delimiter_v= false;
template< ConstexprString s >
constexpr is_delimiter_v< CreateDelimiter< s > >;
inline StaticValue< std::map< void (*)( const DelimiterBase & ), StreamState< Delim, std::string, globalDelimiter<
template< typename Delimiter >
using DelimiterStateByValue= StreamState< Delim, std::string, globalDelimiter< Delim > >;
inline namespace exports inline namespace exports
{ {
enum { FieldDelimiter }; Delimiter< "\t"_cs > FieldDelimiter;
enum { RecordDelimiter }; Delimiter< "\t"_cs > FieldDelimiter2;
}
namespace C static_assert( FieldDelimiter != FieldDelimiter2 );
{
const std::string defaultFieldDelimiter= "\t"; Delimiter< "\n"_cs > RecordDelimiter;
const char defaultRecordDelimiter= '\n';
} }
namespace storage namespace storage
{ {
inline StaticValue< std::optional< std::string > > globalFieldDelimiter; template< auto Delim >
inline StaticValue< std::optional< char > > globalRecordDelimiter; inline StaticValue< std::optional< std::string > > globalDelimiter;
} }
inline std::string template< auto Delim >
globalFieldDelimiter() std::string &
globalDelimiter()
{ {
if( not storage::globalFieldDelimiter().has_value() ) storage::globalFieldDelimiter()= C::defaultFieldDelimiter; if( not storage::globalDelimiter< Delim >().has_value() )
{
storage::globalDelimiter()= std::string{} + static_cast< char >( Delim );
}
assert( storage::globalDelimiter< Delim >().has_value() );
return storage::globalFieldDelimiter().value(); return storage::globalFieldDelimiter().value();
} }
namespace exports namespace exports
{ {
inline void template< auto Delim >
setGlobalFieldDelimiter( const std::string delim ) void
setGlobaDelimiter( const std::string delim )
{ {
storage::globalFieldDelimiter()= delim; storage::globalDelimiter< Delim >()= delim;
} }
} }
inline char template< auto Delim >
globalRecordDelimiter() using DelimiterStateByValue= StreamState< Delim, std::string, globalDelimiter< Delim > >;
{
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 ) ) template< typename DelimType >
using DelimiterState= StreamState< Delim, std::string, globalDelimiter >;
struct DelimWrap
{ {
return os << FieldDelimiterState::get( os ); template< typename Delim >
Delim val;
};
std::ostream &
operator << ( std::ostream &os, auto DelimVal )
{
return os << DelimiterState< decltype( DelimVal ) >::get( os );
} }
namespace exports namespace exports
@ -122,7 +168,7 @@ namespace Alepha::Hydrogen::IOStreams ::detail:: delimiters
} }
} }
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;
} }