1
0
forked from Alepha/Alepha

OStream automatic generation for aggregates.

I still have to sort out the extended capabilities system and
implement the ostream_lens system.
This commit is contained in:
2023-10-23 01:50:47 -04:00
parent 23c71c8d27
commit 3a92d2b788
3 changed files with 66 additions and 9 deletions

View File

@ -9,25 +9,22 @@ static_assert( __cplusplus > 2020'00 );
#include <Alepha/auto_comparable.h>
#include <Alepha/IOStreams/OStreamable.h>
namespace
{
template< typename= Alepha::Capabilities< Alepha::auto_comparable, Alepha::IOStreams::IStreamable > >
template< typename= Alepha::Capabilities< Alepha::auto_comparable, Alepha::IOStreams::IStreamable, Alepha::IOStreams::OStreamable > >
struct Agg_core
{
int x;
int y;
int z;
friend std::ostream &
operator << ( std::ostream &os, const Agg_core &agg )
{
return os << "{ " << agg.x << ", " << agg.y << ", " << agg.z << " }";
}
};
using Agg= Agg_core<>;
static_assert( Alepha::Aggregate< Agg > );
static_assert( Alepha::Capability< Agg, Alepha::IOStreams::IStreamable > );
static_assert( Alepha::Capability< Agg, Alepha::IOStreams::OStreamable > );
}

60
IOStreams/OStreamable.h Normal file
View File

@ -0,0 +1,60 @@
static_assert( __cplusplus > 2020'00 );
#pragma once
#include <Alepha/Alepha.h>
#include <ostream>
#include <Alepha/Capabilities.h>
#include <Alepha/template_for_each.h>
#include <Alepha/string_algorithms.h>
#include <Alepha/Concepts.h>
#include <Alepha/Reflection/tuplizeAggregate.h>
namespace Alepha::Hydrogen::IOStreams ::detail:: ostreamable_module
{
inline namespace exports
{
struct OStreamable {};
}
template< typename T >
concept OStreamableAggregate= Aggregate< T >
// For now, this part seems to have problems, since the `has_capability` function
// is either broken... or gcc has a bug which prevents it from functioning
// correctly.
//
// Therefore, we just allow the aggregate case, for the moment. Which is the
// most common case, anyhow.
//and Capability< T, OStreamable >
;
template< OStreamableAggregate Agg >
std::ostream &
operator << ( std::ostream &os, Agg &ostreamable )
{
const auto decomposed= Alepha::Reflection::tuplizeAggregate( ostreamable );
//static_assert( Capability< Agg, exports::OStreamable > );
bool first= true;
// TODO: Consider the lens system here... but the basic use case seems to be for
// aggregates, so we'll go with this simple case for now...
tuple_for_each( decomposed ) <=[&]( const auto &element )
{
if( not first ) os << '\t';
first= false;
os << element;
};
os << '\n';
return os;
}
}
namespace Alepha::Hydrogen::IOStreams::inline exports::inline ostreamable_module
{
using namespace detail::ostreamable_module::exports;
}