Skip to content

Commit

Permalink
add enum_reflected
Browse files Browse the repository at this point in the history
  • Loading branch information
Neargye committed Jun 29, 2024
1 parent 2ac8152 commit bc2e948
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
16 changes: 16 additions & 0 deletions doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* [`enum_entries` obtains pair (value enum, string enum name) sequence.](#enum_entries)
* [`enum_index` obtains index in enum value sequence from enum value.](#enum_index)
* [`enum_contains` checks whether enum contains enumerator with such value.](#enum_contains)
* [`enum_reflected` returns true if the enum value is in the range of values that can be reflected..](#enum_reflected)
* [`enum_type_name` returns type name of enum.](#enum_type_name)
* [`enum_fuse` returns a bijective mix of enum values.](#enum_fuse)
* [`enum_switch` allows runtime enum value transformation to constexpr context.](#enum_switch)
Expand Down Expand Up @@ -316,6 +317,21 @@ constexpr bool enum_contains(string_view value, BinaryPredicate p) noexcept(is_n
magic_enum::enum_contains<Color>("fda"); // -> false
```

## `enum_reflected`

```cpp
template <typename E>
constexpr bool enum_contains(E value) noexcept;

This comment has been minimized.

Copy link
@Cleroth

Cleroth Sep 21, 2024

Looks like a copy-paste error. Should be enum_reflected

This comment has been minimized.

Copy link
@Neargye

Neargye Sep 22, 2024

Author Owner

Thanks!

This comment has been minimized.

Copy link
@Neargye

Neargye Sep 22, 2024

Author Owner

Fixed 126539e


template <typename E>
constexpr bool enum_contains(underlying_type_t<E> value) noexcept;

template <typename E>
constexpr bool enum_contains(string_view value) noexcept;
```
* Returns true if the enum value is in the range of values that can be reflected.
## `enum_type_name`
```cpp
Expand Down
29 changes: 29 additions & 0 deletions include/magic_enum/magic_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,35 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>, typename Bi
return static_cast<bool>(enum_cast<D, S>(value, std::move(p)));
}

// Returns true if the enum value is in the range of values that can be reflected.
template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
[[nodiscard]] constexpr auto enum_reflected(E value) noexcept -> detail::enable_if_t<E, bool> {
using D = std::decay_t<E>;

if constexpr (detail::is_reflected_v<D, S>) {
constexpr auto min = detail::reflected_min<E, S>();
constexpr auto max = detail::reflected_max<E, S>();
return value >= min && value <= max;
}
return false;
}

// Returns true if the enum value is in the range of values that can be reflected.
template <detail::enum_subtype S, typename E>
[[nodiscard]] constexpr auto enum_reflected(E value) noexcept -> detail::enable_if_t<E, bool> {
using D = std::decay_t<E>;

return enum_cast<D, S>(value);
}

// Returns true if the enum integer value is in the range of values that can be reflected.
template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
[[nodiscard]] constexpr auto enum_reflected(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, bool> {
using D = std::decay_t<E>;

return enum_cast<D, S>(value);
}

template <bool AsFlags = true>
inline constexpr auto as_flags = AsFlags ? detail::enum_subtype::flags : detail::enum_subtype::common;

Expand Down
10 changes: 10 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1274,3 +1274,13 @@ TEST_CASE("valid_enum") {
REQUIRE(magic_enum::detail::is_reflected_v<Empty4, as_flags<true>>);
REQUIRE(magic_enum::detail::is_reflected_v<Empty4, as_flags<false>>);
}

TEST_CASE("enum_reflected") {
REQUIRE(enum_reflected<number>(number::one));
REQUIRE(enum_reflected<number>(number::three));
REQUIRE_FALSE(enum_reflected<number>(number::four));
REQUIRE(enum_reflected<number>(1));
REQUIRE(enum_reflected<number>(234));
REQUIRE_FALSE(enum_reflected<number>(400));
REQUIRE_FALSE(enum_reflected<number>(500));
}

0 comments on commit bc2e948

Please sign in to comment.