From ff6e5dd1c82a0c8cf8f774759933cc0a353fa5f9 Mon Sep 17 00:00:00 2001 From: Daniil Goncharov Date: Thu, 20 Feb 2025 12:37:47 +0400 Subject: [PATCH] fix 379 (#382) --- include/magic_enum/magic_enum_format.hpp | 78 ++++++++---------------- test/test.cpp | 1 + 2 files changed, 25 insertions(+), 54 deletions(-) diff --git a/include/magic_enum/magic_enum_format.hpp b/include/magic_enum/magic_enum_format.hpp index 499758942..eadceecbb 100644 --- a/include/magic_enum/magic_enum_format.hpp +++ b/include/magic_enum/magic_enum_format.hpp @@ -35,44 +35,35 @@ #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; +namespace magic_enum::detail { + +template >, int> = 0> +std::string format_as(E e) { + using D = std::decay_t; + static_assert(std::is_same_v, "magic_enum::formatter requires string_view::value_type type same as char."); + 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) +} // namespace magic_enum::format -#ifndef MAGIC_ENUM_USE_STD_MODULE -#include -#endif +#if defined(__cpp_lib_format) 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 std::formatter::format(magic_enum::detail::format_as(e), ctx); } }; @@ -80,35 +71,14 @@ struct std::formatter> && mag #if defined(FMT_VERSION) -#include - template -struct fmt::formatter> && magic_enum::customize::enum_format_enabled(), char>> : fmt::formatter { +struct fmt::formatter>, 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); + return fmt::formatter::format(magic_enum::detail::format_as(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 diff --git a/test/test.cpp b/test/test.cpp index ef0459dae..9d26eab8b 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1176,6 +1176,7 @@ TEST_CASE("multdimensional-switch-case") { #if defined(__cpp_lib_format) +#include #include TEST_CASE("format-base") {