Skip to content

Commit

Permalink
RichEnum::value_of() - return std::optional<V>
Browse files Browse the repository at this point in the history
... instead of std::optional<std::reference_wrapper<const V>

This also matches magic_enum::enum_cast<>()
  • Loading branch information
alexkaratarakis committed Sep 17, 2024
1 parent 97a9bdf commit 1ecb8ed
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
16 changes: 6 additions & 10 deletions include/fixed_containers/enum_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ struct RichEnumAdapter
};

template <class RichEnum>
constexpr std::optional<std::reference_wrapper<const RichEnum>> value_of(
const std::string_view& name)
constexpr std::optional<RichEnum> value_of(const std::string_view& name)
{
for (const RichEnum& rich_enum_val : RichEnum::values())
{
Expand All @@ -182,8 +181,7 @@ constexpr std::optional<std::reference_wrapper<const RichEnum>> value_of(
}

template <class RichEnum>
constexpr std::optional<std::reference_wrapper<const RichEnum>> value_of(
const typename RichEnum::BackingEnum& backing_enum)
constexpr std::optional<RichEnum> value_of(const typename RichEnum::BackingEnum& backing_enum)
{
const auto& rich_enum_values = RichEnum::values();

Expand Down Expand Up @@ -215,7 +213,7 @@ constexpr std::optional<std::reference_wrapper<const RichEnum>> value_of(
}

template <class RichEnum>
constexpr std::optional<std::reference_wrapper<const RichEnum>> value_of(
constexpr std::optional<RichEnum> value_of(
std::underlying_type_t<typename RichEnum::BackingEnum> enum_integer)
{
return value_of<RichEnum>(typename RichEnum::BackingEnum(enum_integer));
Expand Down Expand Up @@ -420,19 +418,17 @@ class SkeletalRichEnumLite
using ValuesFriend = SkeletalRichEnumValues<RichEnumType>;

public:
static constexpr std::optional<std::reference_wrapper<const RichEnumType>> value_of(
const std::string_view& name)
static constexpr std::optional<RichEnumType> value_of(const std::string_view& name)
{
return rich_enums_detail::value_of<RichEnumType>(name);
}

static constexpr std::optional<std::reference_wrapper<const RichEnumType>> value_of(
const BackingEnum& backing_enum)
static constexpr std::optional<RichEnumType> value_of(const BackingEnum& backing_enum)
{
return rich_enums_detail::value_of<RichEnumType>(backing_enum);
}

static constexpr std::optional<std::reference_wrapper<const RichEnumType>> value_of(
static constexpr std::optional<RichEnumType> value_of(
std::underlying_type_t<BackingEnum> enum_integer)
{
return rich_enums_detail::value_of<RichEnumType>(enum_integer);
Expand Down
20 changes: 14 additions & 6 deletions test/enum_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ TEST(RichEnum, ValueOfName)
}

{
constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of("C_ONE").value();

constexpr TestRichEnum1 MY_VALUE = TestRichEnum1::value_of("C_ONE").value();
static_assert(MY_VALUE == TestRichEnum1::C_ONE());
}
}
Expand All @@ -315,8 +314,7 @@ TEST(RichEnum, ValueOfBackingEnum)

{
using BE = detail::TestRichEnum1BackingEnum;
constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of(BE::C_ONE).value();

constexpr TestRichEnum1 MY_VALUE = TestRichEnum1::value_of(BE::C_ONE).value();
static_assert(MY_VALUE == TestRichEnum1::C_ONE());
}
}
Expand All @@ -332,12 +330,22 @@ TEST(RichEnum, ValueOfUnderlyingInt)
}

{
constexpr const TestRichEnum1& MY_VALUE = TestRichEnum1::value_of(19).value();

constexpr TestRichEnum1 MY_VALUE = TestRichEnum1::value_of(19).value();
static_assert(MY_VALUE == TestRichEnum1::C_ONE());
}
}

TEST(RichEnum, ValueOfParityWithBuiltinEnums)
{
// Optional values
{
static_assert(std::same_as<std::optional<TestEnum1>,
decltype(magic_enum::enum_cast<TestEnum1>("ONE"))>);
static_assert(
std::same_as<std::optional<TestRichEnum1>, decltype(TestRichEnum1::value_of("C_ONE"))>);
}
}

TEST(RichEnum, UniqueValuesArrays)
{
constexpr const auto& ENUM1_VALUES = TestRichEnum1::values();
Expand Down

0 comments on commit 1ecb8ed

Please sign in to comment.