forked from Alepha/Alepha
Create C++26(?) emulation of template for
.
This commit is contained in:
@ -38,6 +38,7 @@ add_subdirectory( comparisons.test )
|
||||
add_subdirectory( Exception.test )
|
||||
add_subdirectory( word_wrap.test )
|
||||
add_subdirectory( string_algorithms.test )
|
||||
add_subdirectory( template_for_each.test )
|
||||
add_subdirectory( tuplize_args.test )
|
||||
add_subdirectory( Thread.test )
|
||||
add_subdirectory( assertion.test )
|
||||
|
@ -8,6 +8,8 @@ static_assert( __cplusplus > 2020'99 );
|
||||
|
||||
#include <Alepha/Concepts.h>
|
||||
|
||||
#include <Alepha/Reflection/tuplizeAggregate.h>
|
||||
|
||||
namespace Alepha::Hydrogen ::detail:: template_for_each_m
|
||||
{
|
||||
inline namespace exports
|
||||
@ -79,6 +81,31 @@ namespace Alepha::Hydrogen ::detail:: template_for_each_m
|
||||
{
|
||||
return for_each_syntax_adaptor< const Tuple >{ tuple };
|
||||
}
|
||||
|
||||
template< typename Type >
|
||||
concept TemplateLoopable= false
|
||||
or Tuple< Type >
|
||||
or Aggregate< Type >
|
||||
// or Array< T > // Do we need array support? Does it matter?
|
||||
;
|
||||
|
||||
template< TemplateLoopable Type >
|
||||
[[nodiscard]]
|
||||
constexpr auto
|
||||
template_for( Type &tuple, std::optional< Reflection::aggregate_tuple_t< Type > > &&tupled= {} ) noexcept
|
||||
{
|
||||
tupled= Reflection::tuplizeAggregate( tuple );
|
||||
return tuple_for_each( *tupled );
|
||||
}
|
||||
|
||||
template< TemplateLoopable Type >
|
||||
[[nodiscard]]
|
||||
constexpr auto
|
||||
template_for( const Type &tuple, std::optional< Reflection::aggregate_tuple_t< Type > > &&tupled= {} ) noexcept
|
||||
{
|
||||
tupled= Reflection::tuplizeAggregate( tuple );
|
||||
return tuple_for_each( *tupled );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
42
template_for_each.test/0.cc
Normal file
42
template_for_each.test/0.cc
Normal file
@ -0,0 +1,42 @@
|
||||
static_assert( __cplusplus > 2023'00 );
|
||||
|
||||
#include "../template_for_each.h"
|
||||
|
||||
#include <Alepha/Testing/test.h>
|
||||
#include <Alepha/Testing/TableTest.h>
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include <Alepha/Utility/enroll.h>
|
||||
|
||||
static auto init= Alepha::Utility::enroll <=[]
|
||||
{
|
||||
using namespace Alepha::Testing::literals;
|
||||
using namespace Alepha::Testing::exports;
|
||||
|
||||
struct Example
|
||||
{
|
||||
int x;
|
||||
std::string s;
|
||||
int y;
|
||||
};
|
||||
|
||||
"Can one `template_for` each over a simple struct and get the members into a string?"_test <=TableTest
|
||||
<
|
||||
[]( Example ex )
|
||||
{
|
||||
using Alepha::template_for;
|
||||
std::ostringstream oss;
|
||||
template_for( ex ) <=[&]( const auto &element )
|
||||
{
|
||||
oss << element << std::endl;
|
||||
};
|
||||
|
||||
return oss.str();
|
||||
}
|
||||
>
|
||||
::Cases
|
||||
{
|
||||
{ "Simple case", { { 42, "Hello World", 1138 } }, "42\nHello World\n1138\n" },
|
||||
};
|
||||
};
|
1
template_for_each.test/CMakeLists.txt
Normal file
1
template_for_each.test/CMakeLists.txt
Normal file
@ -0,0 +1 @@
|
||||
unit_test( 0 )
|
Reference in New Issue
Block a user