1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00
This commit is contained in:
neargye 2022-06-28 14:57:34 +04:00
parent b45b470bea
commit 4802f793bb
4 changed files with 14 additions and 14 deletions

View file

@ -115,4 +115,4 @@
* Intellisense Visual Studio may have some problems analyzing `magic_enum`.
* Enums in template may work incorrectly (especially on Сlang)
See (#164)[https://github.com/Neargye/magic_enum/issues/164], (#65)[https://github.com/Neargye/magic_enum/issues/65]
See (#164)[https://github.com/Neargye/magic_enum/issues/164], (#65)[https://github.com/Neargye/magic_enum/issues/65]

View file

@ -32,13 +32,13 @@
#ifndef NEARGYE_MAGIC_ENUM_FORMAT_HPP
#define NEARGYE_MAGIC_ENUM_FORMAT_HPP
#ifndef __cpp_lib_format
#if !defined(__cpp_lib_format)
# error "Format is not supported"
#endif
#include "magic_enum.hpp"
#ifndef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT
#if !defined(MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT)
# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT true
# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
#endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT
@ -53,25 +53,25 @@ namespace magic_enum::customize {
#include <format>
template<typename E>
template <typename E>
struct std::formatter<E, std::enable_if_t<std::is_enum_v<E> && magic_enum::customize::enum_format_enabled<E>(), char>> : std::formatter<std::string_view, char> {
auto format(E e, format_context& ctx) {
if constexpr (magic_enum::detail::is_flags_v<E>) {
if (auto name = magic_enum::enum_flags_name(e); !name.empty()) {
using D = std::decay_t<E>;
if constexpr (magic_enum::detail::is_flags_v<D>) {
if (auto name = magic_enum::enum_flags_name<D>(e); !name.empty()) {
return this->std::formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
}
} else {
if (auto name = magic_enum::enum_name(e); !name.empty()) {
if (auto name = magic_enum::enum_name<D>(e); !name.empty()) {
return this->std::formatter<std::string_view, char>::format(std::string_view{name.data(), name.size()}, ctx);
}
}
constexpr auto type_name = magic_enum::enum_type_name<E>();
throw std::format_error("Type of " + std::string{type_name.data(), type_name.size()} + " enum value: " +
std::to_string(magic_enum::enum_integer(e)) + " is not exists.");
throw std::format_error("Type of " + std::string{type_name.data(), type_name.size()} + " enum value: " + std::to_string(magic_enum::enum_integer<D>(e)) + " is not exists.");
}
};
#ifdef MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
#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 // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE

View file

@ -1202,7 +1202,7 @@ TEST_CASE("multdimensional-switch-case") {
#endif
#ifdef __cpp_lib_format
#if defined(__cpp_lib_format)
#include <magic_enum_format.hpp>
@ -1210,4 +1210,4 @@ TEST_CASE("format-base") {
REQUIRE(std::format("Test-{:~^10}.", Color::RED) == "Test-~~~red~~~~.");
}
#endif
#endif

View file

@ -823,7 +823,7 @@ TEST_CASE("constexpr_for") {
#endif
#ifdef __cpp_lib_format
#if defined(__cpp_lib_format)
#include <magic_enum_format.hpp>
@ -831,4 +831,4 @@ TEST_CASE("format-base") {
REQUIRE(std::format("Test-{:~^11}.", Color::RED | Color::GREEN) == "Test-~RED|GREEN~.");
}
#endif
#endif