diff --git a/include/magic_enum/magic_enum_format.hpp b/include/magic_enum/magic_enum_format.hpp index e25f15276..70c789cd4 100644 --- a/include/magic_enum/magic_enum_format.hpp +++ b/include/magic_enum/magic_enum_format.hpp @@ -35,18 +35,23 @@ #include "magic_enum.hpp" #include "magic_enum_flags.hpp" -#if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT) -# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT 1 -# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE -#endif - -namespace magic_enum::customize { - // customize enum to enable/disable automatic std::format - template - constexpr bool enum_format_enabled() noexcept { - return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT; +template >, int> = 0> +std::string format_as(E e) { + using D = std::decay_t; + + if constexpr (magic_enum::detail::supported::value) { + if constexpr (magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags) { + if (const auto name = magic_enum::enum_flags_name(e); !name.empty()) { + return {name.data(), name.size()}; + } + } else { + if (const auto name = magic_enum::enum_name(e); !name.empty()) { + return {name.data(), name.size()}; + } + } } -} // magic_enum::customize + return std::to_string(magic_enum::enum_integer(e)); +} #if defined(__cpp_lib_format) @@ -55,60 +60,13 @@ namespace magic_enum::customize { #endif template -struct std::formatter> && magic_enum::customize::enum_format_enabled(), char>> : std::formatter { +struct std::formatter>, char>> : std::formatter { template auto format(E e, FormatContext& ctx) const { - static_assert(std::is_same_v, "formatter requires string_view::value_type type same as char."); - using D = std::decay_t; - - if constexpr (magic_enum::detail::supported::value) { - if constexpr (magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags) { - if (const auto name = magic_enum::enum_flags_name(e); !name.empty()) { - return formatter::format(std::string_view{name.data(), name.size()}, ctx); - } - } else { - if (const auto name = magic_enum::enum_name(e); !name.empty()) { - return formatter::format(std::string_view{name.data(), name.size()}, ctx); - } - } - } - return formatter::format(std::to_string(magic_enum::enum_integer(e)), ctx); + return formatter::format(format_as(e), ctx); } }; #endif -#if defined(FMT_VERSION) - -#include - -template -struct fmt::formatter> && magic_enum::customize::enum_format_enabled(), char>> : fmt::formatter { - template - auto format(E e, FormatContext& ctx) const { - static_assert(std::is_same_v, "formatter requires string_view::value_type type same as char."); - using D = std::decay_t; - - if constexpr (magic_enum::detail::supported::value) { - if constexpr (magic_enum::detail::subtype_v == magic_enum::detail::enum_subtype::flags) { - if (const auto name = magic_enum::enum_flags_name(e); !name.empty()) { - return formatter::format(std::string_view{name.data(), name.size()}, ctx); - } - } else { - if (const auto name = magic_enum::enum_name(e); !name.empty()) { - return formatter::format(std::string_view{name.data(), name.size()}, ctx); - } - } - } - return formatter::format(std::to_string(magic_enum::enum_integer(e)), ctx); - } -}; - -#endif - -#if defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE) -# undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT -# undef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE -#endif - #endif // NEARGYE_MAGIC_ENUM_FORMAT_HPP