mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
fix 379
This commit is contained in:
parent
a72a0536c7
commit
e77ff3bdd6
1 changed files with 17 additions and 59 deletions
|
|
@ -35,18 +35,23 @@
|
||||||
#include "magic_enum.hpp"
|
#include "magic_enum.hpp"
|
||||||
#include "magic_enum_flags.hpp"
|
#include "magic_enum_flags.hpp"
|
||||||
|
|
||||||
#if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT)
|
template <typename E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, int> = 0>
|
||||||
# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT 1
|
std::string format_as(E e) {
|
||||||
# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
|
using D = std::decay_t<E>;
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace magic_enum::customize {
|
if constexpr (magic_enum::detail::supported<D>::value) {
|
||||||
// customize enum to enable/disable automatic std::format
|
if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) {
|
||||||
template <typename E>
|
if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) {
|
||||||
constexpr bool enum_format_enabled() noexcept {
|
return {name.data(), name.size()};
|
||||||
return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT;
|
}
|
||||||
|
} else {
|
||||||
|
if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) {
|
||||||
|
return {name.data(), name.size()};
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} // magic_enum::customize
|
return std::to_string(magic_enum::enum_integer<D>(e));
|
||||||
|
}
|
||||||
|
|
||||||
#if defined(__cpp_lib_format)
|
#if defined(__cpp_lib_format)
|
||||||
|
|
||||||
|
|
@ -55,60 +60,13 @@ namespace magic_enum::customize {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
template <typename E>
|
template <typename E>
|
||||||
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> {
|
struct std::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, char>> : std::formatter<std::string_view, char> {
|
||||||
template <class FormatContext>
|
template <class FormatContext>
|
||||||
auto format(E e, FormatContext& ctx) const {
|
auto format(E e, FormatContext& ctx) const {
|
||||||
static_assert(std::is_same_v<char, string_view::value_type>, "formatter requires string_view::value_type type same as char.");
|
return formatter<std::string_view, char>::format(format_as<E>(e), ctx);
|
||||||
using D = std::decay_t<E>;
|
|
||||||
|
|
||||||
if constexpr (magic_enum::detail::supported<D>::value) {
|
|
||||||
if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) {
|
|
||||||
if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) {
|
|
||||||
return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) {
|
|
||||||
return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return formatter<std::string_view, char>::format(std::to_string(magic_enum::enum_integer<D>(e)), ctx);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(FMT_VERSION)
|
|
||||||
|
|
||||||
#include <fmt/format.h>
|
|
||||||
|
|
||||||
template <typename E>
|
|
||||||
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> {
|
|
||||||
template <class FormatContext>
|
|
||||||
auto format(E e, FormatContext& ctx) const {
|
|
||||||
static_assert(std::is_same_v<char, string_view::value_type>, "formatter requires string_view::value_type type same as char.");
|
|
||||||
using D = std::decay_t<E>;
|
|
||||||
|
|
||||||
if constexpr (magic_enum::detail::supported<D>::value) {
|
|
||||||
if constexpr (magic_enum::detail::subtype_v<D> == magic_enum::detail::enum_subtype::flags) {
|
|
||||||
if (const auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) {
|
|
||||||
return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (const auto name = magic_enum::enum_name<D>(e); !name.empty()) {
|
|
||||||
return formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return formatter<std::string_view, char>::format(std::to_string(magic_enum::enum_integer<D>(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
|
#endif // NEARGYE_MAGIC_ENUM_FORMAT_HPP
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue