From 56dcb011c237390abf80fb1342d0cce291ed603f Mon Sep 17 00:00:00 2001 From: Arkhipov Ivan Date: Wed, 15 Nov 2023 21:25:49 +0300 Subject: [PATCH] Put `get` to `magic_enum::containers` namespace (#316) --- README.md | 2 +- doc/reference.md | 2 +- example/example_containers_array.cpp | 12 ++++---- include/magic_enum/magic_enum_containers.hpp | 6 +--- test/test_containers.cpp | 30 ++++++++++---------- 5 files changed, 24 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index af9d0e3..cdee6ba 100644 --- a/README.md +++ b/README.md @@ -255,7 +255,7 @@ Header-only C++17 library provides static reflection for enums, work with any en color_rgb_array[Color::RED] = {255, 0, 0}; color_rgb_array[Color::GREEN] = {0, 255, 0}; color_rgb_array[Color::BLUE] = {0, 0, 255}; - std::get(color_rgb_array) // -> RGB{0, 0, 255} + magic_enum::containers::get(color_rgb_array) // -> RGB{0, 0, 255} ``` * `containers::bitset` bitset container for enums. diff --git a/doc/reference.md b/doc/reference.md index d8c0eb8..ff00731 100644 --- a/doc/reference.md +++ b/doc/reference.md @@ -696,7 +696,7 @@ struct array { color_rgb_array[Color::RED] = {255, 0, 0}; color_rgb_array[Color::GREEN] = {0, 255, 0}; color_rgb_array[Color::BLUE] = {0, 0, 255}; - std::get(color_rgb_array) // -> RGB{0, 0, 255} + magic_enum::containers::get(color_rgb_array) // -> RGB{0, 0, 255} ``` ## `containers::bitset` diff --git a/example/example_containers_array.cpp b/example/example_containers_array.cpp index 98a4e03..94a1280 100644 --- a/example/example_containers_array.cpp +++ b/example/example_containers_array.cpp @@ -53,13 +53,13 @@ int main() { constexpr magic_enum::containers::array color_rgb_initializer {{{{color_max, 0, 0}, {0, color_max, 0}, {0, 0, color_max}}}}; - std::cout << std::get<0>(color_rgb_initializer) << std::endl; // R=255 G=0 B=0 - std::cout << std::get<1>(color_rgb_initializer) << std::endl; // R=0 G=255 B=0 - std::cout << std::get<2>(color_rgb_initializer) << std::endl; // R=0 G=0 B=255 + std::cout << magic_enum::containers::get<0>(color_rgb_initializer) << std::endl; // R=255 G=0 B=0 + std::cout << magic_enum::containers::get<1>(color_rgb_initializer) << std::endl; // R=0 G=255 B=0 + std::cout << magic_enum::containers::get<2>(color_rgb_initializer) << std::endl; // R=0 G=0 B=255 - std::cout << std::get(color_rgb_initializer) << std::endl; // R=255 G=0 B=0 - std::cout << std::get(color_rgb_initializer) << std::endl; // R=0 G=255 B=0 - std::cout << std::get(color_rgb_initializer) << std::endl; // R=0 G=0 B=255 + std::cout << magic_enum::containers::get(color_rgb_initializer) << std::endl; // R=255 G=0 B=0 + std::cout << magic_enum::containers::get(color_rgb_initializer) << std::endl; // R=0 G=255 B=0 + std::cout << magic_enum::containers::get(color_rgb_initializer) << std::endl; // R=0 G=0 B=255 return 0; } diff --git a/include/magic_enum/magic_enum_containers.hpp b/include/magic_enum/magic_enum_containers.hpp index ccf0f0e..155e497 100644 --- a/include/magic_enum/magic_enum_containers.hpp +++ b/include/magic_enum/magic_enum_containers.hpp @@ -1125,10 +1125,6 @@ class set { template explicit set(V starter) -> set; -} // namespace magic_enum::containers - -namespace std { - template constexpr std::enable_if_t<(std::is_integral_v && I < magic_enum::enum_count()), V&> get(magic_enum::containers::array& a) noexcept { return a.a[I]; @@ -1169,6 +1165,6 @@ constexpr std::enable_if_t && magic_enum::enum return std::move(a[Enum]); } -} // namespace std +} // namespace magic_enum::containers #endif // NEARGYE_MAGIC_ENUM_CONTAINERS_HPP diff --git a/test/test_containers.cpp b/test/test_containers.cpp index 1250bda..167306d 100644 --- a/test/test_containers.cpp +++ b/test/test_containers.cpp @@ -88,13 +88,13 @@ TEST_CASE("containers_array") { constexpr auto colors = magic_enum::enum_values(); - std::ignore = std::get<0>(compare_before); - std::ignore = std::get<1>(compare_before); - std::ignore = std::get<2>(compare_before); + std::ignore = magic_enum::containers::get<0>(compare_before); + std::ignore = magic_enum::containers::get<1>(compare_before); + std::ignore = magic_enum::containers::get<2>(compare_before); - std::ignore = std::get(compare_before); - std::ignore = std::get(compare_before); - std::ignore = std::get(compare_before); + std::ignore = magic_enum::containers::get(compare_before); + std::ignore = magic_enum::containers::get(compare_before); + std::ignore = magic_enum::containers::get(compare_before); REQUIRE(std::make_pair(colors[0], color_rgb_container_int[colors[0]]) == std::make_pair(Color::RED, 1U)); REQUIRE(std::make_pair(colors[1], color_rgb_container_int[colors[1]]) == std::make_pair(Color::GREEN, 4U)); @@ -107,13 +107,13 @@ TEST_CASE("containers_array") { constexpr magic_enum::containers::array compare_after{{1U, 2U, 4U}}; REQUIRE(color_rgb_container_int == compare_after); - std::ignore = std::get<0>(compare_after); - std::ignore = std::get<1>(compare_after); - std::ignore = std::get<2>(compare_after); + std::ignore = magic_enum::containers::get<0>(compare_after); + std::ignore = magic_enum::containers::get<1>(compare_after); + std::ignore = magic_enum::containers::get<2>(compare_after); - std::ignore = std::get(compare_after); - std::ignore = std::get(compare_after); - std::ignore = std::get(compare_after); + std::ignore = magic_enum::containers::get(compare_after); + std::ignore = magic_enum::containers::get(compare_after); + std::ignore = magic_enum::containers::get(compare_after); REQUIRE(std::make_pair(colors[0], color_rgb_container_int[colors[0]]) == std::make_pair(Color::RED, 1U)); REQUIRE(std::make_pair(colors[1], color_rgb_container_int[colors[1]]) == std::make_pair(Color::GREEN, 2U)); @@ -145,9 +145,9 @@ TEST_CASE("containers_array") { REQUIRE(color_rgb_container.front() == RGB{color_max, 0, 0}); REQUIRE(color_rgb_container.back() == RGB{0, 0, color_max}); - REQUIRE(std::get(color_rgb_container) == RGB{color_max, 0, 0}); - REQUIRE(std::get(color_rgb_container) == RGB{0, color_max, 0}); - REQUIRE(std::get(color_rgb_container) == RGB{0, 0, color_max}); + REQUIRE(magic_enum::containers::get(color_rgb_container) == RGB{color_max, 0, 0}); + REQUIRE(magic_enum::containers::get(color_rgb_container) == RGB{0, color_max, 0}); + REQUIRE(magic_enum::containers::get(color_rgb_container) == RGB{0, 0, color_max}); auto iterator = color_rgb_container.begin(); REQUIRE_FALSE(check_const(iterator));