1
0
forked from Alepha/Alepha
Files
Alepha/Utility/NamedOperator.h

64 lines
1.1 KiB
C++

static_assert( __cplusplus > 2023'00 );
#pragma once
#include <Alepha/Alepha.h>
#include <map>
#include <functional>
#include <type_traits>
#include <Alepha/Enum.h>
namespace Alepha::Hydrogen::Utility ::detail:: NamedOperator_m
{
inline namespace exports
{
using OperatorName= Enum
<
"add"_value,
"sub"_value,
"mul"_value,
"mod"_value,
"idiv"_value
>;
}
struct comparator
{
constexpr auto
operator () ( const OperatorName &lhs, const OperatorName &rhs ) const
{
return lhs.get_index() < rhs.get_index();
}
};
namespace exports
{
template< typename Type >
auto
getOperatorMap()
{
std::map< OperatorName, std::function< Type ( Type, Type ) >, comparator > rv
{
{ "add"_value, std::plus<>{} },
{ "sub"_value, std::minus<>{} },
{ "mul"_value, std::multiplies<>{} },
{ "mod"_value, std::modulus<>{} },
};
if constexpr( std::is_integral_v< Type > )
{
rv[ "idiv"_value ]= std::divides<>{};
}
return rv;
};
}
}
namespace Alepha::Hydrogen::Utility::inline exports::inline NamedOperator_m
{
using namespace detail::NamedOperator_m::exports;
}