Skip to content

Commit ff6e5dd

Browse files
authored
fix 379 (#382)
1 parent 61a4f20 commit ff6e5dd

File tree

2 files changed

+25
-54
lines changed

2 files changed

+25
-54
lines changed

include/magic_enum/magic_enum_format.hpp

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -35,80 +35,50 @@
3535
#include "magic_enum.hpp"
3636
#include "magic_enum_flags.hpp"
3737

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+
}
4854
}
49-
} // magic_enum::customize
55+
return std::to_string(magic_enum::enum_integer<D>(e));
56+
}
5057

51-
#if defined(__cpp_lib_format)
58+
} // namespace magic_enum::format
5259

53-
#ifndef MAGIC_ENUM_USE_STD_MODULE
54-
#include <format>
55-
#endif
60+
#if defined(__cpp_lib_format)
5661

5762
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> {
5964
template <class FormatContext>
6065
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);
7667
}
7768
};
7869

7970
#endif
8071

8172
#if defined(FMT_VERSION)
8273

83-
#include <fmt/format.h>
84-
8574
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> {
8776
template <class FormatContext>
8877
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);
10479
}
10580
};
10681

10782
#endif
10883

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-
11484
#endif // NEARGYE_MAGIC_ENUM_FORMAT_HPP

test/test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,6 +1176,7 @@ TEST_CASE("multdimensional-switch-case") {
11761176

11771177
#if defined(__cpp_lib_format)
11781178

1179+
#include <format>
11791180
#include <magic_enum/magic_enum_format.hpp>
11801181

11811182
TEST_CASE("format-base") {

0 commit comments

Comments
 (0)