|
35 | 35 | #include "magic_enum.hpp" |
36 | 36 | #include "magic_enum_flags.hpp" |
37 | 37 |
|
38 | | -#if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT) |
39 | | -# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT 1 |
40 | | -# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE |
41 | | -#endif |
42 | | - |
43 | | -namespace magic_enum::customize { |
44 | | - // customize enum to enable/disable automatic std::format |
45 | | - template <typename E> |
46 | | - constexpr bool enum_format_enabled() noexcept { |
47 | | - return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT; |
| 38 | +namespace magic_enum::detail { |
| 39 | + |
| 40 | +template <typename E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, int> = 0> |
| 41 | +std::string format_as(E e) { |
| 42 | + using D = std::decay_t<E>; |
| 43 | + static_assert(std::is_same_v<char, magic_enum::string_view::value_type>, "magic_enum::formatter requires string_view::value_type type same as char."); |
| 44 | + if constexpr (magic_enum::detail::supported<D>::value) { |
| 45 | + if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) { |
| 46 | + if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) { |
| 47 | + return {name.data(), name.size()}; |
| 48 | + } |
| 49 | + } else { |
| 50 | + if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) { |
| 51 | + return {name.data(), name.size()}; |
| 52 | + } |
| 53 | + } |
48 | 54 | } |
49 | | -} // magic_enum::customize |
| 55 | + return std::to_string(magic_enum::enum_integer<D>(e)); |
| 56 | +} |
50 | 57 |
|
51 | | -#if defined(__cpp_lib_format) |
| 58 | +} // namespace magic_enum::format |
52 | 59 |
|
53 | | -#ifndef MAGIC_ENUM_USE_STD_MODULE |
54 | | -#include <format> |
55 | | -#endif |
| 60 | +#if defined(__cpp_lib_format) |
56 | 61 |
|
57 | 62 | template <typename E> |
58 | | -struct std::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>> && magic_enum::customize::enum_format_enabled<E>(), char>> : std::formatter<std::string_view, char> { |
| 63 | +struct std::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, char>> : std::formatter<std::string_view, char> { |
59 | 64 | template <class FormatContext> |
60 | 65 | auto format(E e, FormatContext& ctx) const { |
61 | | - static_assert(std::is_same_v<char, string_view::value_type>, "formatter requires string_view::value_type type same as char."); |
62 | | - using D = std::decay_t<E>; |
63 | | - |
64 | | - if constexpr (magic_enum::detail::supported<D>::value) { |
65 | | - if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) { |
66 | | - if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) { |
67 | | - return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx); |
68 | | - } |
69 | | - } else { |
70 | | - if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) { |
71 | | - return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx); |
72 | | - } |
73 | | - } |
74 | | - } |
75 | | - return formatter<std::string_view, char>::format(std::to_string(magic_enum::enum_integer<D>(e)), ctx); |
| 66 | + return std::formatter<std::string_view, char>::format(magic_enum::detail::format_as<E>(e), ctx); |
76 | 67 | } |
77 | 68 | }; |
78 | 69 |
|
79 | 70 | #endif |
80 | 71 |
|
81 | 72 | #if defined(FMT_VERSION) |
82 | 73 |
|
83 | | -#include <fmt/format.h> |
84 | | - |
85 | 74 | template <typename E> |
86 | | -struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>> && magic_enum::customize::enum_format_enabled<E>(), char>> : fmt::formatter<std::string_view> { |
| 75 | +struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, char>> : fmt::formatter<std::string_view, char> { |
87 | 76 | template <class FormatContext> |
88 | 77 | auto format(E e, FormatContext& ctx) const { |
89 | | - static_assert(std::is_same_v<char, string_view::value_type>, "formatter requires string_view::value_type type same as char."); |
90 | | - using D = std::decay_t<E>; |
91 | | - |
92 | | - if constexpr (magic_enum::detail::supported<D>::value) { |
93 | | - if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) { |
94 | | - if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) { |
95 | | - return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx); |
96 | | - } |
97 | | - } else { |
98 | | - if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) { |
99 | | - return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx); |
100 | | - } |
101 | | - } |
102 | | - } |
103 | | - return formatter<std::string_view, char>::format(std::to_string(magic_enum::enum_integer<D>(e)), ctx); |
| 78 | + return fmt::formatter<std::string_view, char>::format(magic_enum::detail::format_as<E>(e), ctx); |
104 | 79 | } |
105 | 80 | }; |
106 | 81 |
|
107 | 82 | #endif |
108 | 83 |
|
109 | | -#if defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE) |
110 | | -# undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT |
111 | | -# undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE |
112 | | -#endif |
113 | | - |
114 | 84 | #endif // NEARGYE_MAGIC_ENUM_FORMAT_HPP |
0 commit comments