1
0
forked from Alepha/Alepha

List delimiting primitive for output streams.

This commit is contained in:
2024-04-03 15:53:43 -04:00
parent 8bd9852bd4
commit 66e8acfd5b
5 changed files with 159 additions and 0 deletions

79
delimited_list.cc Normal file
View File

@ -0,0 +1,79 @@
static_assert( __cplusplus > 2020'99 );
#include "delimited_list.h"
#include <Alepha/error.h>
#include <Alepha/IOStreams/StreamState.h>
namespace Alepha::Hydrogen ::detail:: delimited_list_m
{
namespace
{
namespace C
{
const bool debug= false;
}
namespace impl_cc
{
struct DelimitedListStreambuf;
}
IOStreams::StreamState< impl_cc::DelimitedListStreambuf * > tracker{ [] { return nullptr; } };
}
struct impl_cc::DelimitedListStreambuf
: virtual public IOStreams::StackableStreambuf, virtual public std::streambuf
{
private:
std::string delim;
bool first= true;
public:
explicit
DelimitedListStreambuf( std::ios &is, const std::string delim )
: StackableStreambuf( is ), delim( delim )
{
tracker.get( is )= this;
if( C::debug ) error() << "Streambuf adapted." << std::endl;
}
void
doNext()
{
if( C::debug ) error() << "Do Next called" << std::endl;
if( not first ) out() << delim;
first= false;
}
private:
void writeChar( char ) override { throw "Unimpl"; }
void drain() override { throw "Unimpl"; }
int
overflow( const int ch ) override
{
if( C::debug ) error() << "Overflow called" << std::endl;
return forwardOverflow( ch );
}
};
std::ostream &
impl::operator << ( std::ostream &os, MarkItem_t )
{
if( C::debug ) error() << "Marker called" << std::endl;
auto thisTracker= tracker.get( os );
if( thisTracker != nullptr ) thisTracker->doNext();
return os;
}
void
impl::build_streambuf( std::ostream &os, StartDelimitedList &&params )
{
new impl_cc::DelimitedListStreambuf( os, params.delimiter );
}
}