mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
add subtype to enum_fuse
This commit is contained in:
parent
ed43fd5fa2
commit
85e2c5be68
4 changed files with 31 additions and 22 deletions
|
|
@ -425,7 +425,7 @@ constexpr auto type_name() noexcept {
|
|||
constexpr auto name = n<E>();
|
||||
return static_string<name.size()>{name};
|
||||
} else {
|
||||
static_assert(detail::always_false_v<E>, "magic_enum::customize invalid.");
|
||||
static_assert(always_false_v<E>, "magic_enum::customize invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -499,7 +499,7 @@ constexpr auto enum_name() noexcept {
|
|||
constexpr auto name = n<V>();
|
||||
return static_string<name.size()>{name};
|
||||
} else {
|
||||
static_assert(detail::always_false_v<E>, "magic_enum::customize invalid.");
|
||||
static_assert(always_false_v<E>, "magic_enum::customize invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ struct std::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>> && mag
|
|||
using D = std::decay_t<E>;
|
||||
|
||||
if constexpr (magic_enum::detail::supported<D>::value) {
|
||||
if constexpr (detail::subtype_v<D> == detail::enum_subtype::flags) {
|
||||
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);
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ struct fmt::formatter<E, std::enable_if_t<std::is_enum_v<std::decay_t<E>> && mag
|
|||
using D = std::decay_t<E>;
|
||||
|
||||
if constexpr (magic_enum::detail::supported<D>::value) {
|
||||
if constexpr (detail::subtype_v<D> == detail::enum_subtype::flags) {
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -38,30 +38,33 @@ namespace magic_enum {
|
|||
|
||||
namespace detail {
|
||||
|
||||
template <typename E>
|
||||
template <typename E, typename... Es>
|
||||
inline constexpr auto subtypes_v = subtype_v<std::decay_t<E>>;
|
||||
|
||||
template <enum_subtype S, typename E>
|
||||
constexpr optional<std::uintmax_t> fuse_one_enum(optional<std::uintmax_t> hash, E value) noexcept {
|
||||
if (hash) {
|
||||
if (const auto index = enum_index(value)) {
|
||||
return (*hash << log2(enum_count<E>() + 1)) | *index;
|
||||
return (*hash << log2(enum_count<E, S>() + 1)) | *index;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
template <typename E>
|
||||
template <enum_subtype S, typename E>
|
||||
constexpr optional<std::uintmax_t> fuse_enum(E value) noexcept {
|
||||
return fuse_one_enum(0, value);
|
||||
return fuse_one_enum<S>(0, value);
|
||||
}
|
||||
|
||||
template <typename E, typename... Es>
|
||||
template <enum_subtype S, typename E, typename... Es>
|
||||
constexpr optional<std::uintmax_t> fuse_enum(E head, Es... tail) noexcept {
|
||||
return fuse_one_enum(fuse_enum(tail...), head);
|
||||
return fuse_one_enum<S>(fuse_enum<S>(tail...), head);
|
||||
}
|
||||
|
||||
template <typename... Es>
|
||||
template <enum_subtype S, typename... Es>
|
||||
constexpr auto typesafe_fuse_enum(Es... values) noexcept {
|
||||
enum class enum_fuse_t : std::uintmax_t;
|
||||
const auto fuse = fuse_enum(values...);
|
||||
const auto fuse = fuse_enum<S>(values...);
|
||||
if (fuse) {
|
||||
return optional<enum_fuse_t>{static_cast<enum_fuse_t>(*fuse)};
|
||||
}
|
||||
|
|
@ -71,19 +74,25 @@ constexpr auto typesafe_fuse_enum(Es... values) noexcept {
|
|||
} // namespace magic_enum::detail
|
||||
|
||||
// Returns a bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
|
||||
template <typename... Es>
|
||||
template <detail::enum_subtype S, typename... Es>
|
||||
[[nodiscard]] constexpr auto enum_fuse(Es... values) noexcept {
|
||||
static_assert((std::is_enum_v<std::decay_t<Es>> && ...), "magic_enum::enum_fuse requires enum type.");
|
||||
static_assert(sizeof...(Es) >= 2, "magic_enum::enum_fuse requires at least 2 values.");
|
||||
static_assert((detail::log2(enum_count<std::decay_t<Es>>() + 1) + ...) <= (sizeof(std::uintmax_t) * 8), "magic_enum::enum_fuse does not work for large enums");
|
||||
static_assert((detail::log2(enum_count<std::decay_t<Es>, S>() + 1) + ...) <= (sizeof(std::uintmax_t) * 8), "magic_enum::enum_fuse does not work for large enums");
|
||||
#if defined(MAGIC_ENUM_NO_TYPESAFE_ENUM_FUSE)
|
||||
const auto fuse = detail::fuse_enum<std::decay_t<Es>...>(values...);
|
||||
const auto fuse = detail::fuse_enum<S, std::decay_t<Es>...>(values...);
|
||||
#else
|
||||
const auto fuse = detail::typesafe_fuse_enum<std::decay_t<Es>...>(values...);
|
||||
const auto fuse = detail::typesafe_fuse_enum<S, std::decay_t<Es>...>(values...);
|
||||
#endif
|
||||
return assert(fuse), fuse;
|
||||
}
|
||||
|
||||
// Returns a bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
|
||||
template <typename... Es>
|
||||
[[nodiscard]] constexpr auto enum_fuse(Es... values) noexcept {
|
||||
return enum_fuse<detail::subtypes_v<Es...>>(values...);
|
||||
}
|
||||
|
||||
} // namespace magic_enum
|
||||
|
||||
#endif // NEARGYE_MAGIC_ENUM_FUSE_HPP
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ struct invoke_result<F, V, true> : std::invoke_result<F, V> {};
|
|||
template <typename F, typename V>
|
||||
using invoke_result_t = typename invoke_result<F, V>::type;
|
||||
|
||||
template <typename E, detail::enum_subtype S, typename F, std::size_t... I>
|
||||
template <typename E, enum_subtype S, typename F, std::size_t... I>
|
||||
constexpr auto common_invocable(std::index_sequence<I...>) noexcept {
|
||||
static_assert(is_enum_v<E>, "magic_enum::detail::invocable_index requires enum type.");
|
||||
|
||||
|
|
@ -67,11 +67,11 @@ constexpr auto common_invocable(std::index_sequence<I...>) noexcept {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename E, detail::enum_subtype S, typename Result, typename F>
|
||||
template <typename E, enum_subtype S, typename Result, typename F>
|
||||
constexpr auto result_type() noexcept {
|
||||
static_assert(is_enum_v<E>, "magic_enum::detail::result_type requires enum type.");
|
||||
|
||||
constexpr auto seq = std::make_index_sequence<detail::count_v<E, S>>{};
|
||||
constexpr auto seq = std::make_index_sequence<count_v<E, S>>{};
|
||||
using R = typename decltype(common_invocable<E, S, F>(seq))::type;
|
||||
if constexpr (std::is_same_v<Result, default_result_type>) {
|
||||
if constexpr (std::is_same_v<R, nonesuch>) {
|
||||
|
|
@ -90,7 +90,7 @@ constexpr auto result_type() noexcept {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename E, detail::enum_subtype S, typename Result, typename F, typename D = std::decay_t<E>, typename R = typename decltype(result_type<D, S, Result, F>())::type>
|
||||
template <typename E, enum_subtype S, typename Result, typename F, typename D = std::decay_t<E>, typename R = typename decltype(result_type<D, S, Result, F>())::type>
|
||||
using result_t = std::enable_if_t<std::is_enum_v<D> && !std::is_same_v<R, nonesuch>, R>;
|
||||
|
||||
#if !defined(MAGIC_ENUM_ENABLE_HASH)
|
||||
|
|
@ -110,7 +110,7 @@ constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r
|
|||
}
|
||||
}
|
||||
|
||||
template <std::size_t I, std::size_t End, typename R, typename E, detail::enum_subtype S, typename F, typename Def>
|
||||
template <std::size_t I, std::size_t End, typename R, typename E, enum_subtype S, typename F, typename Def>
|
||||
constexpr decltype(auto) constexpr_switch_impl(F&& f, E value, Def&& def) {
|
||||
if constexpr(I < End) {
|
||||
constexpr auto v = enum_constant<enum_value<E, I, S>()>{};
|
||||
|
|
@ -128,7 +128,7 @@ constexpr decltype(auto) constexpr_switch_impl(F&& f, E value, Def&& def) {
|
|||
}
|
||||
}
|
||||
|
||||
template <typename R, typename E, detail::enum_subtype S, typename F, typename Def>
|
||||
template <typename R, typename E, enum_subtype S, typename F, typename Def>
|
||||
constexpr decltype(auto) constexpr_switch(F&& f, E value, Def&& def) {
|
||||
static_assert(is_enum_v<E>, "magic_enum::detail::constexpr_switch requires enum type.");
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue