magic_enum_switch.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // __ __ _ ______ _____
  2. // | \/ | (_) | ____| / ____|_ _
  3. // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
  4. // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
  5. // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
  6. // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
  7. // __/ | https://github.com/Neargye/magic_enum
  8. // |___/ version 0.8.2
  9. //
  10. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  11. // SPDX-License-Identifier: MIT
  12. // Copyright (c) 2019 - 2022 Daniil Goncharov <neargye@gmail.com>.
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining a copy
  15. // of this software and associated documentation files (the "Software"), to deal
  16. // in the Software without restriction, including without limitation the rights
  17. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  18. // copies of the Software, and to permit persons to whom the Software is
  19. // furnished to do so, subject to the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be included in all
  22. // copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  29. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. // SOFTWARE.
  31. #ifndef NEARGYE_MAGIC_ENUM_SWITCH_HPP
  32. #define NEARGYE_MAGIC_ENUM_SWITCH_HPP
  33. #include "magic_enum.hpp"
  34. namespace magic_enum {
  35. namespace detail {
  36. struct default_result_type {};
  37. template <typename T>
  38. struct identity {
  39. using type = T;
  40. };
  41. struct nonesuch {};
  42. template <typename F, typename V, bool = std::is_invocable_v<F, V>>
  43. struct invoke_result : identity<nonesuch> {};
  44. template <typename F, typename V>
  45. struct invoke_result<F, V, true> : std::invoke_result<F, V> {};
  46. template <typename F, typename V>
  47. using invoke_result_t = typename invoke_result<F, V>::type;
  48. template <typename E, typename F, std::size_t... I>
  49. constexpr auto common_invocable(std::index_sequence<I...>) noexcept {
  50. static_assert(is_enum_v<E>, "magic_enum::detail::invocable_index requires enum type.");
  51. if constexpr (count_v<E> == 0) {
  52. return identity<nonesuch>{};
  53. } else {
  54. return std::common_type<invoke_result_t<F, enum_constant<values_v<E>[I]>>...>{};
  55. }
  56. }
  57. template <typename E, typename Result, typename F>
  58. constexpr auto result_type() noexcept {
  59. static_assert(is_enum_v<E>, "magic_enum::detail::result_type requires enum type.");
  60. constexpr auto seq = std::make_index_sequence<detail::count_v<E>>{};
  61. using R = typename decltype(common_invocable<E, F>(seq))::type;
  62. if constexpr (std::is_same_v<Result, default_result_type>) {
  63. if constexpr (std::is_same_v<R, nonesuch>) {
  64. return identity<void>{};
  65. } else {
  66. return identity<R>{};
  67. }
  68. } else {
  69. if constexpr (std::is_convertible_v<R, Result>) {
  70. return identity<Result>{};
  71. } else if constexpr (std::is_convertible_v<Result, R>) {
  72. return identity<R>{};
  73. } else {
  74. return identity<nonesuch>{};
  75. }
  76. }
  77. }
  78. template <typename T, typename Result, typename F, typename D = std::decay_t<T>, typename R = typename decltype(result_type<D, Result, F>())::type>
  79. using result_t = std::enable_if_t<std::is_enum_v<D> && !std::is_same_v<R, nonesuch>, R>;
  80. #if !defined(MAGIC_ENUM_ENABLE_HASH)
  81. template <typename T = void>
  82. inline constexpr auto default_result_type_lambda = []() noexcept(std::is_nothrow_default_constructible_v<T>) { return T{}; };
  83. template <>
  84. inline constexpr auto default_result_type_lambda<void> = []() noexcept {};
  85. template <typename R, typename F, typename... Args>
  86. constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r_v<R, F, Args...>) {
  87. if constexpr (std::is_void_v<R>) {
  88. std::forward<F>(f)(std::forward<Args>(args)...);
  89. } else {
  90. return static_cast<R>(std::forward<F>(f)(std::forward<Args>(args)...));
  91. }
  92. }
  93. template <std::size_t I, std::size_t End, typename R, typename E, typename F, typename Def>
  94. constexpr decltype(auto) constexpr_switch_impl(F&& f, E value, Def&& def) {
  95. if constexpr(I < End) {
  96. constexpr auto v = enum_constant<enum_value<E, I>()>{};
  97. if (value == v) {
  98. if constexpr (std::is_invocable_r_v<R, F, decltype(v)>) {
  99. return invoke_r<R>(std::forward<F>(f), v);
  100. } else {
  101. return def();
  102. }
  103. } else {
  104. return constexpr_switch_impl<I + 1, End, R>(std::forward<F>(f), value, std::forward<Def>(def));
  105. }
  106. } else {
  107. return def();
  108. }
  109. }
  110. template <typename R, typename E, typename F, typename Def>
  111. constexpr decltype(auto) constexpr_switch(F&& f, E value, Def&& def) {
  112. static_assert(is_enum_v<E>, "magic_enum::detail::constexpr_switch requires enum type.");
  113. if constexpr (count_v<E> == 0) {
  114. return def();
  115. } else {
  116. return constexpr_switch_impl<0, count_v<E>, R>(std::forward<F>(f), value, std::forward<Def>(def));
  117. }
  118. }
  119. #endif
  120. } // namespace magic_enum::detail
  121. template <typename Result = detail::default_result_type, typename E, typename F, typename R = detail::result_t<E, Result, F>>
  122. constexpr decltype(auto) enum_switch(F&& f, E value) {
  123. using D = std::decay_t<E>;
  124. static_assert(std::is_enum_v<D>, "magic_enum::enum_switch requires enum type.");
  125. #if defined(MAGIC_ENUM_ENABLE_HASH)
  126. return detail::constexpr_switch<&detail::values_v<D>, detail::case_call_t::value>(
  127. std::forward<F>(f),
  128. value,
  129. detail::default_result_type_lambda<R>);
  130. #else
  131. return detail::constexpr_switch<R, D>(
  132. std::forward<F>(f),
  133. value,
  134. detail::default_result_type_lambda<R>);
  135. #endif
  136. }
  137. template <typename Result, typename E, typename F, typename R = detail::result_t<E, Result, F>>
  138. constexpr decltype(auto) enum_switch(F&& f, E value, Result&& result) {
  139. using D = std::decay_t<E>;
  140. static_assert(std::is_enum_v<D>, "magic_enum::enum_switch requires enum type.");
  141. #if defined(MAGIC_ENUM_ENABLE_HASH)
  142. return detail::constexpr_switch<&detail::values_v<D>, detail::case_call_t::value>(
  143. std::forward<F>(f),
  144. value,
  145. [&result]() -> R { return std::forward<Result>(result); });
  146. #else
  147. return detail::constexpr_switch<R, D>(
  148. std::forward<F>(f),
  149. value,
  150. [&result]() -> R { return std::forward<Result>(result); });
  151. #endif
  152. }
  153. } // namespace magic_enum
  154. template <>
  155. struct std::common_type<magic_enum::detail::nonesuch, magic_enum::detail::nonesuch> : magic_enum::detail::identity<magic_enum::detail::nonesuch> {};
  156. template <typename T>
  157. struct std::common_type<T, magic_enum::detail::nonesuch> : magic_enum::detail::identity<T> {};
  158. template <typename T>
  159. struct std::common_type<magic_enum::detail::nonesuch, T> : magic_enum::detail::identity<T> {};
  160. #endif // NEARGYE_MAGIC_ENUM_SWITCH_HPP