magic_enum_switch.hpp
Go to the documentation of this file.
1 // __ __ _ ______ _____
2 // | \/ | (_) | ____| / ____|_ _
3 // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
4 // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
5 // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
6 // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
7 // __/ | https://github.com/Neargye/magic_enum
8 // |___/ version 0.9.7
9 //
10 // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
11 // SPDX-License-Identifier: MIT
12 // Copyright (c) 2019 - 2024 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 
32 #ifndef NEARGYE_MAGIC_ENUM_SWITCH_HPP
33 #define NEARGYE_MAGIC_ENUM_SWITCH_HPP
34 
35 #include "magic_enum.hpp"
36 
37 namespace magic_enum {
38 
39 namespace detail {
40 
42 
43 template <typename T>
44 struct identity {
45  using type = T;
46 };
47 
48 struct nonesuch {};
49 
50 template <typename F, typename V, bool = std::is_invocable_v<F, V>>
51 struct invoke_result : identity<nonesuch> {};
52 
53 template <typename F, typename V>
54 struct invoke_result<F, V, true> : std::invoke_result<F, V> {};
55 
56 template <typename F, typename V>
58 
59 template <typename E, enum_subtype S, typename F, std::size_t... I>
60 constexpr auto common_invocable(std::index_sequence<I...>) noexcept {
61  static_assert(std::is_enum_v<E>, "magic_enum::detail::invocable_index requires enum type.");
62 
63  if constexpr (count_v<E, S> == 0) {
64  return identity<nonesuch>{};
65  } else {
66  return std::common_type<invoke_result_t<F, enum_constant<values_v<E, S>[I]>>...>{};
67  }
68 }
69 
70 template <typename E, enum_subtype S, typename Result, typename F>
71 constexpr auto result_type() noexcept {
72  static_assert(std::is_enum_v<E>, "magic_enum::detail::result_type requires enum type.");
73 
74  constexpr auto seq = std::make_index_sequence<count_v<E, S>>{};
75  using R = typename decltype(common_invocable<E, S, F>(seq))::type;
76  if constexpr (std::is_same_v<Result, default_result_type>) {
77  if constexpr (std::is_same_v<R, nonesuch>) {
78  return identity<void>{};
79  } else {
80  return identity<R>{};
81  }
82  } else {
83  if constexpr (std::is_convertible_v<R, Result>) {
84  return identity<Result>{};
85  } else if constexpr (std::is_convertible_v<Result, R>) {
86  return identity<R>{};
87  } else {
88  return identity<nonesuch>{};
89  }
90  }
91 }
92 
93 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>
94 using result_t = std::enable_if_t<std::is_enum_v<D> && !std::is_same_v<R, nonesuch>, R>;
95 
96 #if !defined(MAGIC_ENUM_ENABLE_HASH) && !defined(MAGIC_ENUM_ENABLE_HASH_SWITCH)
97 
98 template <typename T = void>
99 inline constexpr auto default_result_type_lambda = []() noexcept(std::is_nothrow_default_constructible_v<T>) { return T{}; };
100 
101 template <>
102 inline constexpr auto default_result_type_lambda<void> = []() noexcept {};
103 
104 template <std::size_t I, std::size_t End, typename R, typename E, enum_subtype S, typename F, typename Def>
105 constexpr decltype(auto) constexpr_switch_impl(F&& f, E value, Def&& def) {
106  if constexpr(I < End) {
107  constexpr auto v = enum_constant<enum_value<E, I, S>()>{};
108  if (value == v) {
109  if constexpr (std::is_invocable_r_v<R, F, decltype(v)>) {
110  return static_cast<R>(std::forward<F>(f)(v));
111  } else {
112  return def();
113  }
114  } else {
115  return constexpr_switch_impl<I + 1, End, R, E, S>(std::forward<F>(f), value, std::forward<Def>(def));
116  }
117  } else {
118  return def();
119  }
120 }
121 
122 template <typename R, typename E, enum_subtype S, typename F, typename Def>
123 constexpr decltype(auto) constexpr_switch(F&& f, E value, Def&& def) {
124  static_assert(is_enum_v<E>, "magic_enum::detail::constexpr_switch requires enum type.");
125 
126  if constexpr (count_v<E, S> == 0) {
127  return def();
128  } else {
129  return constexpr_switch_impl<0, count_v<E, S>, R, E, S>(std::forward<F>(f), value, std::forward<Def>(def));
130  }
131 }
132 #endif
133 
134 } // namespace magic_enum::detail
135 
136 template <typename Result = detail::default_result_type, typename E, detail::enum_subtype S = detail::subtype_v<E>, typename F, typename R = detail::result_t<E, S, Result, F>>
137 constexpr decltype(auto) enum_switch(F&& f, E value) {
138  using D = std::decay_t<E>;
139  static_assert(std::is_enum_v<D>, "magic_enum::enum_switch requires enum type.");
140  static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
141 
142 #if defined(MAGIC_ENUM_ENABLE_HASH) || defined(MAGIC_ENUM_ENABLE_HASH_SWITCH)
143  return detail::constexpr_switch<&detail::values_v<D, S>, detail::case_call_t::value>(
144  std::forward<F>(f),
145  value,
146  detail::default_result_type_lambda<R>);
147 #else
148  return detail::constexpr_switch<R, D, S>(
149  std::forward<F>(f),
150  value,
151  detail::default_result_type_lambda<R>);
152 #endif
153 }
154 
155 template <typename Result = detail::default_result_type, detail::enum_subtype S, typename E, typename F, typename R = detail::result_t<E, S, Result, F>>
156 constexpr decltype(auto) enum_switch(F&& f, E value) {
157  return enum_switch<Result, E, S>(std::forward<F>(f), value);
158 }
159 
160 template <typename Result, typename E, detail::enum_subtype S = detail::subtype_v<E>, typename F, typename R = detail::result_t<E, S, Result, F>>
161 constexpr decltype(auto) enum_switch(F&& f, E value, Result&& result) {
162  using D = std::decay_t<E>;
163  static_assert(std::is_enum_v<D>, "magic_enum::enum_switch requires enum type.");
164  static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
165 
166 #if defined(MAGIC_ENUM_ENABLE_HASH) || defined(MAGIC_ENUM_ENABLE_HASH_SWITCH)
167  return detail::constexpr_switch<&detail::values_v<D, S>, detail::case_call_t::value>(
168  std::forward<F>(f),
169  value,
170  [&result]() -> R { return std::forward<Result>(result); });
171 #else
172  return detail::constexpr_switch<R, D, S>(
173  std::forward<F>(f),
174  value,
175  [&result]() -> R { return std::forward<Result>(result); });
176 #endif
177 }
178 
179 template <typename Result, detail::enum_subtype S, typename E, typename F, typename R = detail::result_t<E, S, Result, F>>
180 constexpr decltype(auto) enum_switch(F&& f, E value, Result&& result) {
181  return enum_switch<Result, E, S>(std::forward<F>(f), value, std::forward<Result>(result));
182 }
183 
184 } // namespace magic_enum
185 
186 template <>
187 struct std::common_type<magic_enum::detail::nonesuch, magic_enum::detail::nonesuch> : magic_enum::detail::identity<magic_enum::detail::nonesuch> {};
188 
189 template <typename T>
190 struct std::common_type<T, magic_enum::detail::nonesuch> : magic_enum::detail::identity<T> {};
191 
192 template <typename T>
193 struct std::common_type<magic_enum::detail::nonesuch, T> : magic_enum::detail::identity<T> {};
194 
195 #endif // NEARGYE_MAGIC_ENUM_SWITCH_HPP
magic_enum::detail::identity
Definition: magic_enum_switch.hpp:44
magic_enum.hpp
magic_enum::detail::result_type
constexpr auto result_type() noexcept
Definition: magic_enum_switch.hpp:71
magic_enum::detail::default_result_type_lambda
constexpr auto default_result_type_lambda
Definition: magic_enum_switch.hpp:99
magic_enum::detail::common_invocable
constexpr auto common_invocable(std::index_sequence< I... >) noexcept
Definition: magic_enum_switch.hpp:60
magic_enum::detail::result_t
std::enable_if_t< std::is_enum_v< D > &&!std::is_same_v< R, nonesuch >, R > result_t
Definition: magic_enum_switch.hpp:94
magic_enum::detail::constexpr_switch_impl
constexpr decltype(auto) constexpr_switch_impl(F &&f, E value, Def &&def)
Definition: magic_enum_switch.hpp:105
magic_enum::detail::invoke_result
Definition: magic_enum_switch.hpp:51
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum::detail::default_result_type_lambda< void >
constexpr auto default_result_type_lambda< void >
Definition: magic_enum_switch.hpp:102
magic_enum::detail::default_result_type
Definition: magic_enum_switch.hpp:41
magic_enum::detail::invoke_result_t
typename invoke_result< F, V >::type invoke_result_t
Definition: magic_enum_switch.hpp:57
magic_enum::enum_switch
constexpr decltype(auto) enum_switch(F &&f, E value)
Definition: magic_enum_switch.hpp:137
magic_enum::detail::nonesuch
Definition: magic_enum_switch.hpp:48
magic_enum::detail::enum_constant
std::integral_constant< E, V > enum_constant
Definition: magic_enum.hpp:228
magic_enum
Definition: magic_enum.hpp:126
magic_enum::detail::enum_subtype
enum_subtype
Definition: magic_enum.hpp:660
magic_enum::detail::constexpr_switch
constexpr decltype(auto) constexpr_switch(F &&f, E value, Def &&def)
Definition: magic_enum_switch.hpp:123


magic_enum
Author(s):
autogenerated on Fri Feb 21 2025 03:20:19