mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
Merge branch 'Neargye:master' into module-fmt
This commit is contained in:
commit
18045d4643
5 changed files with 54 additions and 72 deletions
4
.github/workflows/scorecard.yml
vendored
4
.github/workflows/scorecard.yml
vendored
|
|
@ -47,7 +47,7 @@ jobs:
|
|||
# Upload the results as artifacts (optional). Commenting out will disable uploads of run results in SARIF
|
||||
# format to the repository Actions tab.
|
||||
- name: "Upload artifact"
|
||||
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4.4.3
|
||||
uses: actions/upload-artifact@65c4c4a1ddee5b72f698fdd19549f0f0fb45cf08 # v4.6.0
|
||||
with:
|
||||
name: SARIF file
|
||||
path: results.sarif
|
||||
|
|
@ -55,6 +55,6 @@ jobs:
|
|||
|
||||
# Upload the results to GitHub's code scanning dashboard.
|
||||
- name: "Upload to code-scanning"
|
||||
uses: github/codeql-action/upload-sarif@662472033e021d55d94146f66f6058822b0b39fd # v3.27.0
|
||||
uses: github/codeql-action/upload-sarif@dd746615b3b9d728a6a37ca2045b68ca76d4841a # v3.28.8
|
||||
with:
|
||||
sarif_file: results.sarif
|
||||
|
|
|
|||
|
|
@ -621,14 +621,22 @@ constexpr auto enum_name() noexcept {
|
|||
template <typename E, E V>
|
||||
inline constexpr auto enum_name_v = enum_name<E, V>();
|
||||
|
||||
// CWG1766: Values outside the range of the values of an enumeration
|
||||
// https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
|
||||
#if defined(__clang__) && __clang_major__ >= 16
|
||||
template <typename E, auto V, typename = void>
|
||||
inline constexpr bool is_enum_constexpr_static_cast_valid = false;
|
||||
template <typename E, auto V>
|
||||
inline constexpr bool is_enum_constexpr_static_cast_valid<E, V, std::void_t<std::integral_constant<E, static_cast<E>(V)>>> = true;
|
||||
#else
|
||||
template <typename, auto>
|
||||
inline constexpr bool is_enum_constexpr_static_cast_valid = true;
|
||||
#endif
|
||||
|
||||
template <typename E, auto V>
|
||||
constexpr bool is_valid() noexcept {
|
||||
#if defined(__clang__) && __clang_major__ >= 16
|
||||
// https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
|
||||
constexpr E v = __builtin_bit_cast(E, V);
|
||||
#else
|
||||
if constexpr (is_enum_constexpr_static_cast_valid<E, V>) {
|
||||
constexpr E v = static_cast<E>(V);
|
||||
#endif
|
||||
[[maybe_unused]] constexpr auto custom = customize::enum_name<E>(v);
|
||||
static_assert(std::is_same_v<std::decay_t<decltype(custom)>, customize::customize_t>, "magic_enum::customize requires customize_t type.");
|
||||
if constexpr (custom.first == customize::detail::customize_tag::custom_tag) {
|
||||
|
|
@ -644,6 +652,9 @@ constexpr bool is_valid() noexcept {
|
|||
} else {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
enum class enum_subtype {
|
||||
|
|
|
|||
|
|
@ -52,10 +52,10 @@ namespace magic_enum::containers {
|
|||
namespace detail {
|
||||
|
||||
template <typename T, typename = void>
|
||||
static constexpr bool is_transparent_v{};
|
||||
inline constexpr bool is_transparent_v{};
|
||||
|
||||
template <typename T>
|
||||
static constexpr bool is_transparent_v<T, std::void_t<typename T::is_transparent>>{true};
|
||||
inline constexpr bool is_transparent_v<T, std::void_t<typename T::is_transparent>>{true};
|
||||
|
||||
template <typename Eq = std::equal_to<>, typename T1, typename T2>
|
||||
constexpr bool equal(T1&& t1, T2&& t2, Eq&& eq = {}) {
|
||||
|
|
|
|||
|
|
@ -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::detail {
|
||||
|
||||
namespace magic_enum::customize {
|
||||
// customize enum to enable/disable automatic std::format
|
||||
template <typename E>
|
||||
constexpr bool enum_format_enabled() noexcept {
|
||||
return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT;
|
||||
}
|
||||
} // magic_enum::customize
|
||||
|
||||
#if defined(__cpp_lib_format)
|
||||
|
||||
#ifndef MAGIC_ENUM_USE_STD_MODULE
|
||||
#include <format>
|
||||
#endif
|
||||
|
||||
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> {
|
||||
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.");
|
||||
template <typename E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, int> = 0>
|
||||
std::string format_as(E e) {
|
||||
using D = std::decay_t<E>;
|
||||
|
||||
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.");
|
||||
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);
|
||||
return {name.data(), name.size()};
|
||||
}
|
||||
} 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 {name.data(), name.size()};
|
||||
}
|
||||
}
|
||||
}
|
||||
return formatter<std::string_view, char>::format(std::to_string(magic_enum::enum_integer<D>(e)), ctx);
|
||||
return std::to_string(magic_enum::enum_integer<D>(e));
|
||||
}
|
||||
|
||||
} // namespace magic_enum::format
|
||||
|
||||
#if defined(__cpp_lib_format)
|
||||
|
||||
template <typename E>
|
||||
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>
|
||||
auto format(E e, FormatContext& ctx) const {
|
||||
return std::formatter<std::string_view, char>::format(magic_enum::detail::format_as<E>(e), ctx);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -80,35 +71,14 @@ struct std::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>> && mag
|
|||
|
||||
#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> {
|
||||
struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>>, char>> : fmt::formatter<std::string_view, char> {
|
||||
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);
|
||||
return fmt::formatter<std::string_view, char>::format(magic_enum::detail::format_as<E>(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
|
||||
|
|
|
|||
|
|
@ -1176,6 +1176,7 @@ TEST_CASE("multdimensional-switch-case") {
|
|||
|
||||
#if defined(__cpp_lib_format)
|
||||
|
||||
#include <format>
|
||||
#include <magic_enum/magic_enum_format.hpp>
|
||||
|
||||
TEST_CASE("format-base") {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue