mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
add suport enum_switch without defined MAGIC_ENUM_ENABLE_HASH
This commit is contained in:
parent
c588771875
commit
612033ab5d
2 changed files with 56 additions and 2 deletions
|
|
@ -732,6 +732,7 @@ template <typename T>
|
|||
struct underlying_type<T, true> : std::underlying_type<std::decay_t<T>> {};
|
||||
|
||||
#if defined(MAGIC_ENUM_ENABLE_HASH)
|
||||
|
||||
template <typename T>
|
||||
inline constexpr bool has_hash = true;
|
||||
|
||||
|
|
|
|||
|
|
@ -93,6 +93,53 @@ constexpr auto result_type() noexcept {
|
|||
template <typename T, typename Result, typename F, typename D = std::decay_t<T>, typename R = typename decltype(result_type<D, 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)
|
||||
|
||||
template <typename T = void>
|
||||
inline constexpr auto default_result_type_lambda = []() noexcept(std::is_nothrow_default_constructible_v<T>) { return T{}; };
|
||||
|
||||
template <>
|
||||
inline constexpr auto default_result_type_lambda<void> = []() noexcept {};
|
||||
|
||||
template <typename R, typename F, typename... Args>
|
||||
constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r_v<R, F, Args...>) {
|
||||
if constexpr (std::is_void_v<R>) {
|
||||
std::forward<F>(f)(std::forward<Args>(args)...);
|
||||
} else {
|
||||
return static_cast<R>(std::forward<F>(f)(std::forward<Args>(args)...));
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t I, std::size_t End, typename R, typename E, 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>()>{};
|
||||
if (value == v) {
|
||||
if constexpr (std::is_invocable_r_v<R, F, decltype(v)>) {
|
||||
return invoke_r<R>(std::forward<F>(f), v);
|
||||
} else {
|
||||
return def();
|
||||
}
|
||||
} else {
|
||||
return constexpr_switch_impl<I + 1, End, R>(std::forward<F>(f), value, std::forward<Def>(def));
|
||||
}
|
||||
} else {
|
||||
return def();
|
||||
}
|
||||
}
|
||||
|
||||
template <typename R, typename E, 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_impl requires enum type.");
|
||||
|
||||
if constexpr (count_v<E> == 0) {
|
||||
return def();
|
||||
} else {
|
||||
return constexpr_switch_impl<0, count_v<E>, R>(std::forward<F>(f), value, std::forward<Def>(def));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace magic_enum::detail
|
||||
|
||||
template <typename Result = detail::default_result_type, typename E, typename F, typename R = detail::result_t<E, Result, F>>
|
||||
|
|
@ -106,7 +153,10 @@ constexpr decltype(auto) enum_switch(F&& f, E value) {
|
|||
value,
|
||||
detail::default_result_type_lambda<R>);
|
||||
#else
|
||||
static_assert(detail::has_hash<D>, "magic_enum::enum_switch requires defined MAGIC_ENUM_ENABLE_HASH");
|
||||
return detail::constexpr_switch<R, D>(
|
||||
std::forward<F>(f),
|
||||
value,
|
||||
detail::default_result_type_lambda<R>);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
@ -121,7 +171,10 @@ constexpr decltype(auto) enum_switch(F&& f, E value, Result&& result) {
|
|||
value,
|
||||
[&result]() -> R { return std::forward<Result>(result); });
|
||||
#else
|
||||
static_assert(detail::has_hash<D>, "magic_enum::enum_switch requires defined MAGIC_ENUM_ENABLE_HASH");
|
||||
return detail::constexpr_switch<R, D>(
|
||||
std::forward<F>(f),
|
||||
value,
|
||||
[&result]() -> R { return std::forward<Result>(result); });
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue