1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00
This commit is contained in:
neargye 2020-07-05 01:29:21 +05:00
parent 25912781ee
commit 81f2888125
4 changed files with 315 additions and 34 deletions

View file

@ -9,8 +9,13 @@ elseif(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
endif()
endif()
add_executable(example example.cpp)
set_target_properties(example PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(example PRIVATE cxx_std_17)
target_compile_options(example PRIVATE ${OPTIONS})
target_link_libraries(example PRIVATE ${CMAKE_PROJECT_NAME})
function(make_example target)
add_executable(${target} ${target}.cpp)
set_target_properties(${target} PROPERTIES CXX_EXTENSIONS OFF)
target_compile_features(${target} PRIVATE cxx_std_17)
target_compile_options(${target} PRIVATE ${OPTIONS})
target_link_libraries(${target} PRIVATE ${CMAKE_PROJECT_NAME})
endfunction()
make_example(example)
make_example(enum_flag_example)

View file

@ -0,0 +1,100 @@
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// SPDX-License-Identifier: MIT
// Copyright (c) 2019 - 2020 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <iostream>
#include <magic_enum.hpp>
enum class AnimalFlags : std::uint64_t { HasClaws = 1 << 1, CanFly = 1 << 2, EatsFish = 1 << 20, Endangered = std::uint64_t{1} << 40 };
int main() {
// Enum variable to string name.
AnimalFlags f1 = AnimalFlags::CanFly;
auto f1_name = magic_enum::flag::enum_name(f1);
std::cout << f1_name << std::endl; // CanFly
// String enum name sequence.
constexpr auto names = magic_enum::flag::enum_names<AnimalFlags>();
std::cout << "AnimalFlags names:";
for (auto n : names) {
std::cout << " " << n;
}
std::cout << std::endl;
// AnimalFlags names: HasClaws CanFly EatsFish Endangered
#if 0
// String name to enum value.
auto f2 = magic_enum::flag::enum_cast<AnimalFlags>("EatsFish");
if (f2.has_value() && f2.value() == AnimalFlags::EatsFish) {
std::cout << "EatsFish = " << magic_enum::flag::enum_integer(f2.value()) << std::endl; // EatsFish = 4
}
// Integer value to enum value.
auto f3 = magic_enum::flag::enum_cast<AnimalFlags>(8);
if (f3.has_value() && f3.value() == AnimalFlags::Endangered) {
std::cout << "Endangered = " << magic_enum::flag::enum_integer(f3.value()) << std::endl; // Endangered = 8
}
#endif
// Enum value to integer value.
auto f4_integer = magic_enum::flag::enum_integer(AnimalFlags::HasClaws);
if (f4_integer == static_cast<std::underlying_type_t<AnimalFlags>>(AnimalFlags::HasClaws)) {
std::cout << "HasClaws = " << f4_integer << std::endl; // HasClaws = 2
}
using namespace magic_enum::flag::ostream_operators; // out-of-the-box ostream operator for all enums.
#if 0
// ostream operator for enum.
std::cout << f1 << " " << f2 << " " << f3 << std::endl; // CanFly EatsFish Endangered
#endif
// Number of enum values.
std::cout << "AnimalFlags enum size: " << magic_enum::flag::enum_count<AnimalFlags>() << std::endl; // AnimalFlags enum size: 4
// Indexed access to enum value.
std::cout << "AnimalFlags[0] = " << magic_enum::flag::enum_value<AnimalFlags>(0) << std::endl; // AnimalFlags[0] = HasClaws
// Enum value sequence.
constexpr auto values = magic_enum::flag::enum_values<AnimalFlags>();
std::cout << "AnimalFlags values:";
for (AnimalFlags f : values) {
std::cout << " " << f; // ostream operator for enum.
}
std::cout << std::endl;
// AnimalFlags sequence: HasClaws CanFly EatsFish Endangered
using namespace magic_enum::flag::bitwise_operators; // out-of-the-box bitwise operators for all enums.
// Support operators: ~, |, &, ^, |=, &=, ^=.
AnimalFlags flag = AnimalFlags::HasClaws | AnimalFlags::CanFly;
std::cout << flag << std::endl; // HasClaws | CanFly
// Enum pair (value enum, string enum name) sequence.
constexpr auto entries = magic_enum::flag::enum_entries<AnimalFlags>();
std::cout << "AnimalFlags entries:";
for (auto e : entries) {
std::cout << " " << e.second << " = " << magic_enum::flag::enum_integer(e.first);
}
std::cout << std::endl;
// AnimalFlags entries: AnimalFlags entries: HasClaws = 2 CanFly = 4 EatsFish = 1048576 Endangered = 1099511627776
return 0;
}

View file

@ -39,9 +39,9 @@ int main() {
std::cout << c1_name << std::endl; // RED
// String enum name sequence.
constexpr auto color_names = magic_enum::enum_names<Color>();
constexpr auto names = magic_enum::enum_names<Color>();
std::cout << "Color names:";
for (auto n : color_names) {
for (auto n : names) {
std::cout << " " << n;
}
std::cout << std::endl;
@ -60,9 +60,9 @@ int main() {
}
// Enum value to integer value.
auto color_integer = magic_enum::enum_integer(c1);
if (color_integer == to_integer(Color::RED)) {
std::cout << "RED = " << color_integer << std::endl; // RED = -10
auto c4_integer = magic_enum::enum_integer(Color::RED);
if (c4_integer == static_cast<std::underlying_type_t<Color>>(Color::RED)) {
std::cout << "RED = " << c4_integer << std::endl; // RED = -10
}
using namespace magic_enum::ostream_operators; // out-of-the-box ostream operator for all enums.
@ -76,9 +76,9 @@ int main() {
std::cout << "Color[0] = " << magic_enum::enum_value<Color>(0) << std::endl; // Color[0] = RED
// Enum value sequence.
constexpr auto colors = magic_enum::enum_values<Color>();
std::cout << "Colors sequence:";
for (Color c : colors) {
constexpr auto values = magic_enum::enum_values<Color>();
std::cout << "Colors values:";
for (Color c : values) {
std::cout << " " << c; // ostream operator for enum.
}
std::cout << std::endl;
@ -87,8 +87,8 @@ int main() {
enum class Flags { A = 1, B = 2, C = 4, D = 8 };
using namespace magic_enum::bitwise_operators; // out-of-the-box bitwise operators for all enums.
// Support operators: ~, |, &, ^, |=, &=, ^=.
Flags flags = Flags::A | Flags::C;
std::cout << flags << std::endl;
Flags flag = Flags::A | Flags::C;
std::cout << flag << std::endl;
enum color { red, green, blue };
@ -103,9 +103,9 @@ int main() {
static_assert(magic_enum::is_scoped_enum_v<Flags>);
// Enum pair (value enum, string enum name) sequence.
constexpr auto color_entries = magic_enum::enum_entries<Color>();
constexpr auto entries = magic_enum::enum_entries<Color>();
std::cout << "Colors entries:";
for (auto e : color_entries) {
for (auto e : entries) {
std::cout << " " << e.second << " = " << static_cast<int>(e.first);
}
std::cout << std::endl;

View file

@ -277,7 +277,7 @@ constexpr auto reflected_max() noexcept {
static_assert(is_enum_v<E>, "magic_enum::detail::reflected_max requires enum type.");
if constexpr (IsFlags) {
return (std::numeric_limits<std::underlying_type_t<E>>::max)();
return (std::numeric_limits<U>::max)();
} else {
static_assert(has_max<E>::value, "magic_enum::enum_range requires max.");
constexpr auto lhs = enum_range<E>::max;
@ -299,7 +299,7 @@ constexpr auto value(std::size_t i) noexcept {
static_assert(is_enum_v<E>, "magic_enum::detail::value requires enum type.");
if constexpr (IsFlags) {
return static_cast<E>(static_cast<U>(0x1) << static_cast<U>(i));
return static_cast<E>(static_cast<U>(1) << static_cast<U>(i));
} else {
return static_cast<E>(static_cast<int>(i) + O);
}
@ -338,7 +338,7 @@ constexpr auto values() noexcept {
template <typename E, bool IsFlags = false>
inline constexpr auto values_v = values<E, IsFlags>();
template <typename T, bool IsFlags = false, typename D = std::decay_t<T>>
template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
using values_t = decltype((values_v<D, IsFlags>));
template <typename E, bool IsFlags = false>
@ -357,7 +357,7 @@ constexpr std::size_t range_size() noexcept {
if constexpr (IsFlags) {
return std::numeric_limits<U>::digits;
} else {
constexpr int range_size = max_v<E> - min_v<E> + 1;
constexpr auto range_size = max_v<E> - min_v<E> + 1;
static_assert(range_size > 0, "magic_enum::enum_range requires valid size.");
static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "magic_enum::enum_range requires valid size.");
return static_cast<std::size_t>(range_size);
@ -365,7 +365,7 @@ constexpr std::size_t range_size() noexcept {
}
template <typename E, bool IsFlags = false>
inline constexpr auto range_size_v = range_size<E,IsFlags>();
inline constexpr auto range_size_v = range_size<E, IsFlags>();
template <typename E>
using index_t = std::conditional_t<range_size_v<E> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>;
@ -392,22 +392,22 @@ constexpr auto names(std::index_sequence<I...>) noexcept {
}
template <typename E, bool IsFlags = false>
inline constexpr auto names_v = names<E, IsFlags>(std::make_index_sequence<count_v<E>>{});
inline constexpr auto names_v = names<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
template <typename T, bool IsFlags = false, typename D = std::decay_t<T>>
template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
using names_t = decltype((names_v<D, IsFlags>));
template <typename E, bool IsFlags, std::size_t... I>
constexpr auto entries(std::index_sequence<I...>) noexcept {
static_assert(is_enum_v<E>, "magic_enum::detail::entries requires enum type.");
return std::array<std::pair<E, std::string_view>, sizeof...(I)>{{{values_v<E, IsFlags>[I], enum_name_v<E, values_v<E>[I]>}...}};
return std::array<std::pair<E, std::string_view>, sizeof...(I)>{{{values_v<E, IsFlags>[I], enum_name_v<E, values_v<E, IsFlags>[I]>}...}};
}
template <typename E, bool IsFlags = false>
inline constexpr auto entries_v = entries<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
template <typename T, bool IsFlags = false, typename D = std::decay_t<T>>
template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
using entries_t = decltype((entries_v<D, IsFlags>));
template <typename E, bool IsFlags = false>
@ -430,26 +430,37 @@ constexpr int undex(U value) noexcept {
return -1; // Value out of range.
}
template <typename E>
template <typename E, typename U = std::underlying_type_t<E>>
constexpr int endex(E value) noexcept {
static_assert(is_enum_v<E>, "magic_enum::detail::endex requires enum type.");
return undex<E>(static_cast<std::underlying_type_t<E>>(value));
return undex<E>(static_cast<U>(value));
}
template <bool, typename T, typename R>
template <bool, bool, typename T, typename R>
struct enable_if_enum {};
template <typename T, typename R>
struct enable_if_enum<true, T, R> {
struct enable_if_enum<true, false, T, R> {
using type = R;
using D = std::decay_t<T>;
static_assert(supported<D>::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
static_assert(count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
static_assert(count_v<D, false> > 0, "magic_enum requires enum implementation and valid max and min.");
};
template <typename T, typename R>
struct enable_if_enum<true, true, T, R> {
using type = R;
using D = std::decay_t<T>;
static_assert(supported<D>::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
static_assert(count_v<D, true> > 0, "magic_enum::flag requires enum flag implementation.");
};
template <typename T, typename R = void>
using enable_if_enum_t = typename enable_if_enum<std::is_enum_v<std::decay_t<T>>, T, R>::type;
using enable_if_enum_t = typename enable_if_enum<std::is_enum_v<std::decay_t<T>>, false, T, R>::type;
template <typename T, typename R>
using enable_if_enum_flags_t = typename enable_if_enum<std::is_enum_v<std::decay_t<T>>, true, T, R>::type;
template <typename T, typename Enable = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
using enum_concept = T;
@ -532,7 +543,7 @@ template <typename E>
if constexpr (detail::is_sparse_v<D>) {
return assert(index < detail::count_v<D>), detail::values_v<D>[index];
} else {
return assert(index < detail::count_v<D>), static_cast<D>(index + detail::min_v<D>);
return assert(index < detail::count_v<D>), detail::value<D, false, detail::min_v<D>>(index);
}
}
@ -738,9 +749,174 @@ constexpr auto operator^=(E& lhs, E rhs) noexcept -> detail::enable_if_enum_t<E,
} // namespace magic_enum::bitwise_operators
namespace flags {
namespace flag {
// Returns number of enum flag values.
template <typename E>
[[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_flags_t<E, std::size_t> {
using D = std::decay_t<E>;
return detail::count_v<D, true>;
}
// Returns enum flag value at specified index.
// No bounds checking is performed: the behavior is undefined if index >= number of enum values.
template <typename E>
[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_flags_t<E, std::decay_t<E>> {
using D = std::decay_t<E>;
if constexpr (detail::is_sparse_v<D>) {
return assert((index < detail::count_v<D, true>)), detail::values_v<D, true>[index];
} else {
return assert((index < detail::count_v<D, true>)), detail::value<D, true, 0>(index);
}
}
// Obtains value enum flag sequence.
// Returns std::array with enum flag values, sorted by enum flag value.
template <typename E>
[[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_flags_t<E, detail::values_t<E, true>> {
using D = std::decay_t<E>;
return detail::values_v<D, true>;
}
// Returns string enum flag name from enum value.
// If enum value does not have name or value out of range, returns empty string.
template <typename E>
[[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_flags_t<E, std::string> {
using D = std::decay_t<E>;
using U = std::underlying_type_t<D>;
std::string name;
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
if (const auto v = enum_value<D>(i); (static_cast<U>(value) & static_cast<U>(v)) != 0) {
const auto n = detail::names_v<D, true>[i];
if (!name.empty()) {
name.append(1, '|');
}
name.append(n.data(), n.size());
}
}
return name;
}
// Obtains string enum flag name sequence.
// Returns std::array with string enum flag names, sorted by enum flag value.
template <typename E>
[[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_flags_t<E, detail::names_t<E, true>> {
using D = std::decay_t<E>;
return detail::names_v<D, true>;
}
// Obtains pair (value enum flag, string enum flag name) sequence.
// Returns std::array with std::pair (value enum flag, string enum flag name), sorted by enum flag value.
template <typename E>
[[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_flags_t<E, detail::entries_t<E, true>> {
using D = std::decay_t<E>;
return detail::entries_v<D, true>;
}
// TODO: enum_cast
// Obtains enum flag value from integer value.
// Returns std::optional with enum flag value.
template <typename E>
[[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_flags_t<E, std::optional<std::decay_t<E>>> {
using D = std::decay_t<E>;
using U = std::underlying_type_t<D>;
if (value < detail::min_v<D, true> || value > detail::max_v<D, true>) {
return std::nullopt; // Out of range.
}
U check_value = U{0};
for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
if (const auto v = enum_value<D>(i); (static_cast<U>(value) & static_cast<U>(v)) != 0) {
check_value |= static_cast<U>(v);
}
}
if (check_value == value) {
return static_cast<D>(value);
}
return std::nullopt; // Invalid value.
}
// Obtains enum flag value from enum flag string name.
// Returns std::optional with enum flag value.
template <typename E, typename BinaryPredicate>
[[nodiscard]] constexpr auto enum_cast(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_flags_t<E, std::optional<std::decay_t<E>>> {
static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::flag::enum_cast requires bool(char, char) invocable predicate.");
static_assert(sizeof(E) == 0, "not implemented");
return {};
}
// Obtains enum flag value from enum flag string name.
// Returns std::optional with enum flag value.
template <typename E>
[[nodiscard]] constexpr auto enum_cast(std::string_view value) noexcept -> detail::enable_if_enum_flags_t<E, std::optional<std::decay_t<E>>> {
static_assert(sizeof(E) == 0, "not implemented");
return {};
}
// Returns integer value from enum flag value.
template <typename E>
[[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_flags_t<E, underlying_type_t<E>> {
return static_cast<underlying_type_t<E>>(value);
}
// Obtains index in enum flag value sequence from enum flag value.
// Returns std::optional with index.
template <typename E>
[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t<E, std::optional<std::size_t>> {
static_assert(sizeof(E) == 0, "not implemented");
return {};
}
// Checks whether enum flag contains enumerator with such integer value.
template <typename E>
[[nodiscard]] constexpr auto enum_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_flags_t<E, bool> {
using D = std::decay_t<E>;
return enum_cast<D>(value).has_value();
}
// Checks whether enum flag contains enumerator with such value.
template <typename E>
[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_flags_t<E, bool> {
using D = std::decay_t<E>;
using U = std::underlying_type_t<D>;
return enum_cast<D>(static_cast<U>(value)).has_value();
}
// Checks whether enum flag contains enumerator with such string enum name.
template <typename E>
[[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept -> detail::enable_if_enum_flags_t<E, bool> {
static_assert(sizeof(E) == 0, "not implemented");
return {};
}
namespace ostream_operators {
// TODO: operator<<
using namespace magic_enum::ostream_operators;
} // namespace magic_enum::flag::ostream_operators
namespace bitwise_operators {
using namespace magic_enum::bitwise_operators;
} // namespace magic_enum::flag::bitwise_operators
} // namespace magic_enum::flag
} // namespace magic_enum
#if defined(__clang__)