1
0
forked from Alepha/Alepha

Create C++26(?) emulation of template for.

This commit is contained in:
2024-07-05 11:54:01 -04:00
parent 807311e0dd
commit b5fb8c76f2
4 changed files with 71 additions and 0 deletions

View File

@ -38,6 +38,7 @@ add_subdirectory( comparisons.test )
add_subdirectory( Exception.test ) add_subdirectory( Exception.test )
add_subdirectory( word_wrap.test ) add_subdirectory( word_wrap.test )
add_subdirectory( string_algorithms.test ) add_subdirectory( string_algorithms.test )
add_subdirectory( template_for_each.test )
add_subdirectory( tuplize_args.test ) add_subdirectory( tuplize_args.test )
add_subdirectory( Thread.test ) add_subdirectory( Thread.test )
add_subdirectory( assertion.test ) add_subdirectory( assertion.test )

View File

@ -8,6 +8,8 @@ static_assert( __cplusplus > 2020'99 );
#include <Alepha/Concepts.h> #include <Alepha/Concepts.h>
#include <Alepha/Reflection/tuplizeAggregate.h>
namespace Alepha::Hydrogen ::detail:: template_for_each_m namespace Alepha::Hydrogen ::detail:: template_for_each_m
{ {
inline namespace exports inline namespace exports
@ -79,6 +81,31 @@ namespace Alepha::Hydrogen ::detail:: template_for_each_m
{ {
return for_each_syntax_adaptor< const Tuple >{ tuple }; 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 );
}
} }
} }

View 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" },
};
};

View File

@ -0,0 +1 @@
unit_test( 0 )