forked from Alepha/Alepha
This permits creation of custom filtration stages in the IOStream streambuf stack.
129 lines
2.6 KiB
C++
129 lines
2.6 KiB
C++
static_assert( __cplusplus > 2020'99 );
|
|
|
|
#include "../Filter.h"
|
|
|
|
#include <Alepha/Testing/test.h>
|
|
#include <Alepha/Testing/TableTest.h>
|
|
|
|
#include <sstream>
|
|
#include <iostream>
|
|
|
|
static auto init= Alepha::Utility::enroll <=[]
|
|
{
|
|
using namespace std::literals::string_literals;
|
|
using namespace Alepha::Testing::literals;
|
|
|
|
using namespace Alepha::Testing::exports;
|
|
|
|
[]( const std::string &s )
|
|
{
|
|
std::stringstream iss{ s };
|
|
|
|
//iss >> Filter{ std::make_unique< CapitalizationFilter >() };
|
|
|
|
std::string rv;
|
|
iss << "bob";
|
|
int x;
|
|
iss >> x;
|
|
};
|
|
|
|
"Simple Filter"_test <=TableTest
|
|
<
|
|
[]( const std::string &s )
|
|
-> std::string
|
|
{
|
|
using namespace Alepha::IOStreams::exports::Filter_m;
|
|
struct CapitalizationFilter : StreamFilter
|
|
{
|
|
public:
|
|
Emitter
|
|
nextChar( const char ch ) override
|
|
{
|
|
struct EmitterImpl
|
|
: public Emitter::Impl
|
|
{
|
|
char ch;
|
|
bool read= false;
|
|
|
|
explicit EmitterImpl( const char ch ) : ch( ch ) {}
|
|
|
|
bool empty() const final { return read; }
|
|
char
|
|
next() final
|
|
{
|
|
read= true;
|
|
std::cerr << "Emitted `" << ch << "`" << std::endl;
|
|
return ch;
|
|
}
|
|
};
|
|
|
|
std::cerr << "Emitter built for `" << ch << "`" << std::endl;
|
|
return Emitter{ std::make_unique< EmitterImpl >( ::toupper( ch ) ) };
|
|
}
|
|
};
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << Filter{ std::make_unique< CapitalizationFilter >() };
|
|
|
|
oss << s;
|
|
|
|
oss << Alepha::IOStreams::PopStack{};
|
|
|
|
return oss.str();
|
|
}
|
|
>
|
|
::Cases
|
|
{
|
|
{ "Hello", { "hello" }, "HELLO" },
|
|
};
|
|
|
|
auto streamWriter= []( const std::string &s )
|
|
{
|
|
using namespace Alepha::IOStreams::exports::Filter_m;
|
|
struct CapitalizationFilter : StreamFilter
|
|
{
|
|
public:
|
|
Emitter
|
|
nextChar( const char ch ) override
|
|
{
|
|
struct EmitterImpl
|
|
: public Emitter::Impl
|
|
{
|
|
char ch;
|
|
bool read= false;
|
|
|
|
explicit EmitterImpl( const char ch ) : ch( ch ) {}
|
|
|
|
bool empty() const final { return read; }
|
|
char
|
|
next() final
|
|
{
|
|
read= true;
|
|
std::cerr << "Emitted `" << ch << "`" << std::endl;
|
|
return ch;
|
|
}
|
|
};
|
|
std::cerr << "Emitter built for `" << ch << "`" << std::endl;
|
|
return Emitter{ std::make_unique< EmitterImpl >( ::toupper( ch ) ) };
|
|
}
|
|
};
|
|
|
|
std::stringstream iss{ s };
|
|
|
|
iss >> Filter{ std::make_unique< CapitalizationFilter >() };
|
|
|
|
std::string rv;
|
|
iss >> rv;
|
|
|
|
iss >> Alepha::IOStreams::PopStack{};
|
|
|
|
return rv;
|
|
};
|
|
|
|
"Simple Input Filter"_test <=TableTest< streamWriter >::Cases
|
|
{
|
|
{ "Hello", { "hello" }, "HELLO" },
|
|
};
|
|
};
|