From ad053e91750fd081ed2dc998b4cabf95e9907201 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Sat, 13 Jul 2024 05:32:57 -0400 Subject: [PATCH] Negation support for binary `Enum` --- Enum.h | 9 +++++++++ Enum.test/0.cc | 27 +++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/Enum.h b/Enum.h index ac3d31a..584c482 100644 --- a/Enum.h +++ b/Enum.h @@ -224,6 +224,15 @@ namespace Alepha::Hydrogen ::detail:: Enum_m } }; + template< EnumValueString no, EnumValueString yes > + constexpr Enum< no, yes > + operator not ( Enum< no, yes > enumeration ) + { + enumeration.set_index( 1 - enumeration.get_index() ); + + return enumeration; + } + namespace exports { template< auto ... values > diff --git a/Enum.test/0.cc b/Enum.test/0.cc index a327809..f4f0a0f 100644 --- a/Enum.test/0.cc +++ b/Enum.test/0.cc @@ -9,6 +9,16 @@ static_assert( __cplusplus > 2020'99 ); #include +namespace +{ + template< typename T > + concept CanNegate= + requires( const T &t ) + { + { not t }; + }; +} + static auto init= Alepha::Utility::enroll <=[] { using namespace Alepha::literals::enum_literals; @@ -58,4 +68,21 @@ static auto init= Alepha::Utility::enroll <=[] { { "Wrong", { "Wrong" }, { "Adam", "Baker", "Charlie", "David" } }, }; + + static_assert( not CanNegate< Alepha::Enum< "one"_value > > ); + static_assert( CanNegate< Alepha::Enum< "one"_value, "two"_value > > ); + static_assert( not CanNegate< Alepha::Enum< "one"_value, "two"_value, "three"_value > > ); + + "Does negation of a two form enum work?"_test <=TableTest + < + []( const Alepha::Enum< "No"_value, "Yes"_value > e ) + { + return not e; + } + > + ::Cases + { + { "No -> Yes", { "No"_value }, "Yes"_value }, + { "Yes -> No", { "Yes"_value }, "No"_value }, + }; };