Skip to content

Commit

Permalink
Rich Enum CONSTANT_NAME()s now return by value
Browse files Browse the repository at this point in the history
Now that rich enums are copyable, there is no need to enforce
instance control or address equality.
Note also that you can't take address of normal enum constants,
for example `&TestEnum::ONE` fails to compile.
  • Loading branch information
alexkaratarakis committed Sep 16, 2024
1 parent c0222d4 commit bd09021
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
8 changes: 4 additions & 4 deletions include/fixed_containers/enum_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ concept has_member_sizet_ordinal_void_const = requires(T instance) {
template <typename T>
concept has_backing_enum_typename_and_member_backing_enum_void_const = requires(T instance) {
typename T::BackingEnum;
{ instance.backing_enum() } -> std::same_as<const typename T::BackingEnum&>;
{ instance.backing_enum() } -> std::same_as<typename T::BackingEnum>;
};

template <typename T>
Expand Down Expand Up @@ -338,7 +338,7 @@ using RichEnumStorage = std::conditional_t<std::is_same_v<std::underlying_type_t
// BackingEnum::ENUM_CONSTANT and the rich enum ENUM_CONSTANT()
// Must be used after the values() static function is declared in the rich enum.
#define FIXED_CONTAINERS_RICH_ENUM_CONSTANT_GEN_HELPER(RichEnumName, CONSTANT_NAME) \
static constexpr const RichEnumName& CONSTANT_NAME() \
static constexpr RichEnumName CONSTANT_NAME() \
{ \
return RichEnumName::value_of(BackingEnum::CONSTANT_NAME).value(); \
}
Expand Down Expand Up @@ -459,7 +459,7 @@ class SkeletalRichEnumLite
constexpr SkeletalRichEnumLite& operator=(const SkeletalRichEnumLite&) noexcept = default;
constexpr SkeletalRichEnumLite& operator=(SkeletalRichEnumLite&&) noexcept = default;

[[nodiscard]] constexpr const BackingEnum& backing_enum() const
[[nodiscard]] constexpr BackingEnum backing_enum() const
{
return this->detail_backing_enum.value();
}
Expand All @@ -473,7 +473,7 @@ class SkeletalRichEnumLite
return this->detail_backing_enum == other.detail_backing_enum;
}

constexpr const RichEnumType& operator!() const
constexpr RichEnumType operator!() const
requires std::is_same_v<bool, std::underlying_type_t<BackingEnum>>
{
if (*this == RichEnumType::values()[0])
Expand Down
7 changes: 2 additions & 5 deletions test/enum_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,6 @@ TEST(RichEnum, ValueOfName)
constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of("C_ONE").value();

static_assert(MY_VALUE == TestRichEnum1::C_ONE());
static_assert(&MY_VALUE == &TestRichEnum1::C_ONE());
}
}

Expand All @@ -260,7 +259,6 @@ TEST(RichEnum, ValueOfBackingEnum)
constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of(BE::C_ONE).value();

static_assert(MY_VALUE == TestRichEnum1::C_ONE());
static_assert(&MY_VALUE == &TestRichEnum1::C_ONE());
}
}

Expand All @@ -278,7 +276,6 @@ TEST(RichEnum, ValueOfUnderlyingInt)
constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of(19).value();

static_assert(MY_VALUE == TestRichEnum1::C_ONE());
static_assert(&MY_VALUE == &TestRichEnum1::C_ONE());
}
}

Expand All @@ -305,12 +302,12 @@ TEST(RichEnum, HasValue)
TEST(RichEnum, BoolNegate)
{
{
constexpr const TestRichEnumBool& F_VALUE = TestRichEnumBool::FALSE_VALUE();
constexpr TestRichEnumBool F_VALUE = TestRichEnumBool::FALSE_VALUE();
static_assert(F_VALUE.has_value());
static_assert((!F_VALUE) == TestRichEnumBool::TRUE_VALUE());
}
{
constexpr const TestRichEnumBool& T_VALUE = TestRichEnumBool::TRUE_VALUE();
constexpr TestRichEnumBool T_VALUE = TestRichEnumBool::TRUE_VALUE();
static_assert(T_VALUE.has_value());
static_assert((!T_VALUE) == TestRichEnumBool::FALSE_VALUE());
}
Expand Down

0 comments on commit bd09021

Please sign in to comment.