From a90a1a776eae1fdc90c1fbb66cf058f690345c4a Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 10 Jul 2024 20:28:51 -0400 Subject: [PATCH 1/3] Add a `NamedOperator` facility to Alepha. This permits naming operators via an enhanced enum and then looking them up. This is a useful component for quick scripting language functionalities. --- Utility/CMakeLists.txt | 1 + Utility/NamedOperator.h | 64 +++++++++++++++++++++++ Utility/NamedOperator.test/0.cc | 31 +++++++++++ Utility/NamedOperator.test/CMakeLists.txt | 1 + 4 files changed, 97 insertions(+) create mode 100644 Utility/NamedOperator.h create mode 100644 Utility/NamedOperator.test/0.cc create mode 100644 Utility/NamedOperator.test/CMakeLists.txt diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index 2c1bf06..6d3634f 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -4,3 +4,4 @@ target_sources( alepha PRIVATE add_subdirectory( derived_pointer_cast.test ) add_subdirectory( print_number.test ) +add_subdirectory( NamedOperator.test ) diff --git a/Utility/NamedOperator.h b/Utility/NamedOperator.h new file mode 100644 index 0000000..5d7f848 --- /dev/null +++ b/Utility/NamedOperator.h @@ -0,0 +1,64 @@ +static_assert( __cplusplus > 2023'00 ); + +#pragma once + +#include + +#include +#include +#include + +#include + +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; +} diff --git a/Utility/NamedOperator.test/0.cc b/Utility/NamedOperator.test/0.cc new file mode 100644 index 0000000..5fe2c0c --- /dev/null +++ b/Utility/NamedOperator.test/0.cc @@ -0,0 +1,31 @@ +static_assert( __cplusplus > 2023'00 ); + +#include "../NamedOperator.h" + +#include +#include + +#include + +static auto init= Alepha::Utility::enroll <=[] +{ + using namespace Alepha::Testing::exports; + using namespace Alepha::Utility::NamedOperator_m; + using namespace Alepha::literals::enum_literals; + + "Does the named operator system have the expected basic functionality?"_test <= TableTest + < + []( const int lhs, const OperatorName name, const int rhs ) + { + return getOperatorMap< int >().at( name )( lhs, rhs ); + } + > + ::Cases + { + { "addition", { 1, "add"_value, 1 }, 2 }, + { "subtraction", { 1, "sub"_value, 1 }, 0 }, + { "multiplication", { 5, "mul"_value, 5 }, 25 }, + { "modulus", { 9, "mod"_value, 5 }, 4 }, + { "division", { 5, "idiv"_value, 3 }, 1 }, + }; +}; diff --git a/Utility/NamedOperator.test/CMakeLists.txt b/Utility/NamedOperator.test/CMakeLists.txt new file mode 100644 index 0000000..b099603 --- /dev/null +++ b/Utility/NamedOperator.test/CMakeLists.txt @@ -0,0 +1 @@ +unit_test( 0 ) From 0e970762e47d8c8539115944b1a2c91fccdc498a Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Fri, 12 Jul 2024 14:47:45 -0400 Subject: [PATCH 2/3] Fix whitespace error. --- Utility/NamedOperator.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Utility/NamedOperator.h b/Utility/NamedOperator.h index 5d7f848..090fbbc 100644 --- a/Utility/NamedOperator.h +++ b/Utility/NamedOperator.h @@ -22,7 +22,6 @@ namespace Alepha::Hydrogen::Utility ::detail:: NamedOperator_m "mod"_value, "idiv"_value >; - } struct comparator From db1b0edff3649304871479e7dd33250dce381603 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Fri, 12 Jul 2024 14:55:03 -0400 Subject: [PATCH 3/3] Add named comparator support. --- Utility/CMakeLists.txt | 1 + Utility/NamedComparator.h | 61 +++++++++++++++++++++ Utility/NamedComparator.test/0.cc | 46 ++++++++++++++++ Utility/NamedComparator.test/CMakeLists.txt | 1 + 4 files changed, 109 insertions(+) create mode 100644 Utility/NamedComparator.h create mode 100644 Utility/NamedComparator.test/0.cc create mode 100644 Utility/NamedComparator.test/CMakeLists.txt diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index 6d3634f..6410db3 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -5,3 +5,4 @@ target_sources( alepha PRIVATE add_subdirectory( derived_pointer_cast.test ) add_subdirectory( print_number.test ) add_subdirectory( NamedOperator.test ) +add_subdirectory( NamedComparator.test ) diff --git a/Utility/NamedComparator.h b/Utility/NamedComparator.h new file mode 100644 index 0000000..2de3bf3 --- /dev/null +++ b/Utility/NamedComparator.h @@ -0,0 +1,61 @@ +static_assert( __cplusplus > 2023'00 ); + +#pragma once + +#include + +#include +#include +#include + +#include + +namespace Alepha::Hydrogen::Utility ::detail:: NamedComparator_m +{ + inline namespace exports + { + using ComparatorName= Enum + < + "eq"_value, + "ne"_value, + "lt"_value, + "gt"_value, + "le"_value, + "ge"_value + >; + } + + struct comparator + { + constexpr auto + operator () ( const ComparatorName &lhs, const ComparatorName &rhs ) const + { + return lhs.get_index() < rhs.get_index(); + } + }; + + namespace exports + { + template< typename Type > + auto + getComparatorMap() + { + std::map< ComparatorName, std::function< bool ( Type, Type ) >, comparator > rv + { + { "eq"_value, std::equal_to<>{} }, + { "ne"_value, std::not_equal_to<>{} }, + { "lt"_value, std::less<>{} }, + { "gt"_value, std::greater<>{} }, + { "le"_value, std::less_equal<>{} }, + { "ge"_value, std::greater_equal<>{} }, + }; + + return rv; + }; + } +} + +namespace Alepha::Hydrogen::Utility::inline exports::inline NamedComparator_m +{ + using namespace detail::NamedComparator_m::exports; +} diff --git a/Utility/NamedComparator.test/0.cc b/Utility/NamedComparator.test/0.cc new file mode 100644 index 0000000..2773236 --- /dev/null +++ b/Utility/NamedComparator.test/0.cc @@ -0,0 +1,46 @@ +static_assert( __cplusplus > 2023'00 ); + +#include "../NamedComparator.h" + +#include +#include + +#include + +static auto init= Alepha::Utility::enroll <=[] +{ + using namespace Alepha::Testing::exports; + using namespace Alepha::Utility::NamedComparator_m; + using namespace Alepha::literals::enum_literals; + + "Does the named comparator system have the expected basic functionality?"_test <= TableTest + < + []( const int lhs, const ComparatorName name, const int rhs ) + { + return getComparatorMap< int >().at( name )( lhs, rhs ); + } + > + ::Cases + { + { "equals (true)", { 1, "eq"_value, 1 }, true }, + { "equals (false)", { 1, "eq"_value, 2 }, false }, + + { "not equals (true)", { 1, "ne"_value, 2 }, true }, + { "not equals (false)", { 1, "ne"_value, 1 }, false }, + + { "less (true)", { 1, "lt"_value, 2 }, true }, + { "less (false)", { 1, "lt"_value, 1 }, false }, + + { "greater (true)", { 2, "gt"_value, 1 }, true }, + { "greater (false)", { 1, "gt"_value, 1 }, false }, + + + { "less equal (true)", { 1, "le"_value, 2 }, true }, + { "less equal (true)", { 1, "le"_value, 1 }, true }, + { "less equal (false)", { 1, "le"_value, 0 }, false }, + + { "greater equal (true)", { 2, "ge"_value, 1 }, true }, + { "greater equal (true)", { 1, "ge"_value, 1 }, true }, + { "greater equal (false)", { 0, "ge"_value, 1 }, false }, + }; +}; diff --git a/Utility/NamedComparator.test/CMakeLists.txt b/Utility/NamedComparator.test/CMakeLists.txt new file mode 100644 index 0000000..b099603 --- /dev/null +++ b/Utility/NamedComparator.test/CMakeLists.txt @@ -0,0 +1 @@ +unit_test( 0 )