mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
update enum_entries
This commit is contained in:
parent
eef1ac9339
commit
158c736a32
3 changed files with 29 additions and 6 deletions
|
|
@ -95,5 +95,14 @@ int main() {
|
|||
static_assert(!magic_enum::is_scoped_enum_v<color>);
|
||||
static_assert(magic_enum::is_scoped_enum_v<direction>);
|
||||
|
||||
// Enum pair (value enum, string enum name) sequence.
|
||||
constexpr auto color_entries = magic_enum::enum_entries<Color>();
|
||||
std::cout << "Colors entries:";
|
||||
for (auto& e : color_entries) {
|
||||
std::cout << " " << e.second << " = " << static_cast<int>(e.first);
|
||||
}
|
||||
std::cout << std::endl;
|
||||
// Color entries: RED = -10 BLUE = 0 GREEN = 10
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -170,12 +170,12 @@ template <typename E, std::size_t... I>
|
|||
}
|
||||
|
||||
template <typename E, std::size_t... I>
|
||||
[[nodiscard]] constexpr decltype(auto) pairs_impl(std::integer_sequence<std::size_t, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::pairs_impl requires enum type.");
|
||||
[[nodiscard]] constexpr decltype(auto) entries_impl(std::integer_sequence<std::size_t, I...>) noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::detail::entries_impl requires enum type.");
|
||||
constexpr auto values = values_impl<E>(range_impl<E>());
|
||||
constexpr std::array<std::pair<E, std::string_view>, sizeof...(I)> pairs{{{values[I], name_impl<E, values[I]>()}...}};
|
||||
constexpr std::array<std::pair<E, std::string_view>, sizeof...(I)> entries{{{values[I], name_impl<E, values[I]>()}...}};
|
||||
|
||||
return pairs;
|
||||
return entries;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
|
@ -316,9 +316,9 @@ template <typename E, typename = detail::enable_if_enum_t<E>>
|
|||
[[nodiscard]] constexpr decltype(auto) enum_entries() noexcept {
|
||||
static_assert(std::is_enum_v<E>, "magic_enum::enum_entries requires enum type.");
|
||||
constexpr auto count = detail::values_impl<E>(detail::range_impl<E>()).size();
|
||||
constexpr auto pairs = detail::pairs_impl<E>(std::make_index_sequence<count>{});
|
||||
constexpr auto entries = detail::entries_impl<E>(std::make_index_sequence<count>{});
|
||||
|
||||
return pairs;
|
||||
return entries;
|
||||
}
|
||||
|
||||
namespace ops {
|
||||
|
|
|
|||
|
|
@ -214,6 +214,20 @@ TEST_CASE("enum_names") {
|
|||
REQUIRE(s4 == std::array<std::string_view, 3>{{"one", "two", "three"}});
|
||||
}
|
||||
|
||||
TEST_CASE("enum_entries") {
|
||||
constexpr auto s1 = magic_enum::enum_entries<Color>();
|
||||
REQUIRE(s1 == std::array<std::pair<Color, std::string_view>, 3>{{{Color::RED, "RED"}, {Color::GREEN, "GREEN"}, {Color::BLUE, "BLUE"}}});
|
||||
|
||||
constexpr auto s2 = magic_enum::enum_entries<Numbers>();
|
||||
REQUIRE(s2 == std::array<std::pair<Numbers, std::string_view>, 3>{{{Numbers::one, "one"}, {Numbers::two, "two"}, {Numbers::three, "three"}}});
|
||||
|
||||
constexpr auto s3 = magic_enum::enum_entries<Directions>();
|
||||
REQUIRE(s3 == std::array<std::pair<Directions, std::string_view>, 4>{{{Directions::Left, "Left"}, {Directions::Down, "Down"}, {Directions::Up, "Up"}, {Directions::Right, "Right"}}});
|
||||
|
||||
constexpr auto s4 = magic_enum::enum_entries<number>();
|
||||
REQUIRE(s4 == std::array<std::pair<number, std::string_view>, 3>{{{number::one, "one"}, {number::two, "two"}, {number::three, "three"}}});
|
||||
}
|
||||
|
||||
TEST_CASE("operator<<") {
|
||||
auto test_ostream = [](auto e, std::string_view name) {
|
||||
using namespace magic_enum::ops;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue