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>
  • Loading branch information
alexkaratarakis committed Sep 16, 2024
1 parent bd09021 commit 5f3ba4a
Show file tree
Hide file tree
Showing 2 changed files with 9 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
9 changes: 3 additions & 6 deletions test/enum_utils_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,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 @@ -256,8 +255,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 @@ -273,8 +271,7 @@ 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());
}
}
Expand Down

0 comments on commit 5f3ba4a

Please sign in to comment.