forked from Alepha/Alepha
Stream builder notation for strings.
This should be less boilerplate than repeated `lexical_cast` or `stringify` calls when building strings.
This commit is contained in:
@ -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,7 +28,7 @@ namespace
|
||||
|
||||
|
||||
auto
|
||||
stringify( const Agg &agg, const std::string delim )
|
||||
stringify_specific( const Agg &agg, const std::string delim )
|
||||
{
|
||||
std::ostringstream oss;
|
||||
Alepha::IOStreams::setGlobalFieldDelimiter( "YOU SHOULD NOT SEE THIS" );
|
||||
@ -58,6 +59,7 @@ 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 (default delimiter)"_test <=TableTest< stringify_default >
|
||||
::Cases
|
||||
@ -65,7 +67,24 @@ static auto init= Alepha::Utility::enroll <=[]
|
||||
{ "smoke test", { { 1, 2, 3 } }, { "1\t2\t3" } },
|
||||
};
|
||||
|
||||
"Simple OStream (specific delimiter)"_test <=TableTest< stringify >
|
||||
"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" } },
|
||||
|
61
IOStreams/Stream.h
Normal file
61
IOStreams/Stream.h
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user