magic_enum_utility.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_UTILITY_HPP
33 #define NEARGYE_MAGIC_ENUM_UTILITY_HPP
34 
35 #include "magic_enum.hpp"
36 
37 namespace magic_enum {
38 
39 namespace detail {
40 
41 template <typename E, enum_subtype S, typename F, std::size_t... I>
42 constexpr auto for_each(F&& f, std::index_sequence<I...>) {
43  constexpr bool has_void_return = (std::is_void_v<std::invoke_result_t<F, enum_constant<values_v<E, S>[I]>>> || ...);
44  constexpr bool all_same_return = (std::is_same_v<std::invoke_result_t<F, enum_constant<values_v<E, S>[0]>>, std::invoke_result_t<F, enum_constant<values_v<E, S>[I]>>> && ...);
45 
46  if constexpr (has_void_return) {
47  (f(enum_constant<values_v<E, S>[I]>{}), ...);
48  } else if constexpr (all_same_return) {
49  return std::array{f(enum_constant<values_v<E, S>[I]>{})...};
50  } else {
51  return std::tuple{f(enum_constant<values_v<E, S>[I]>{})...};
52  }
53 }
54 
55 template <typename E, enum_subtype S, typename F,std::size_t... I>
56 constexpr bool all_invocable(std::index_sequence<I...>) {
57  if constexpr (count_v<E, S> == 0) {
58  return false;
59  } else {
60  return (std::is_invocable_v<F, enum_constant<values_v<E, S>[I]>> && ...);
61  }
62 }
63 
64 } // namespace magic_enum::detail
65 
66 template <typename E, detail::enum_subtype S = detail::subtype_v<E>, typename F, detail::enable_if_t<E, int> = 0>
67 constexpr auto enum_for_each(F&& f) {
68  using D = std::decay_t<E>;
69  static_assert(std::is_enum_v<D>, "magic_enum::enum_for_each requires enum type.");
70  static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
71  constexpr auto sep = std::make_index_sequence<detail::count_v<D, S>>{};
72 
73  if constexpr (detail::all_invocable<D, S, F>(sep)) {
74  return detail::for_each<D, S>(std::forward<F>(f), sep);
75  } else {
76  static_assert(detail::always_false_v<D>, "magic_enum::enum_for_each requires invocable of all enum value.");
77  }
78 }
79 
80 template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
81 [[nodiscard]] constexpr auto enum_next_value(E value, std::ptrdiff_t n = 1) noexcept -> detail::enable_if_t<E, optional<std::decay_t<E>>> {
82  using D = std::decay_t<E>;
83  constexpr std::ptrdiff_t count = detail::count_v<D, S>;
84 
85  if (const auto i = enum_index<D, S>(value)) {
86  const std::ptrdiff_t index = (static_cast<std::ptrdiff_t>(*i) + n);
87  if (index >= 0 && index < count) {
88  return enum_value<D, S>(static_cast<std::size_t>(index));
89  }
90  }
91  return {};
92 }
93 
94 template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
95 [[nodiscard]] constexpr auto enum_next_value_circular(E value, std::ptrdiff_t n = 1) noexcept -> detail::enable_if_t<E, std::decay_t<E>> {
96  using D = std::decay_t<E>;
97  constexpr std::ptrdiff_t count = detail::count_v<D, S>;
98 
99  if (const auto i = enum_index<D, S>(value)) {
100  const std::ptrdiff_t index = ((((static_cast<std::ptrdiff_t>(*i) + n) % count) + count) % count);
101  if (index >= 0 && index < count) {
102  return enum_value<D, S>(static_cast<std::size_t>(index));
103  }
104  }
105  return MAGIC_ENUM_ASSERT(false), value;
106 }
107 
108 template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
109 [[nodiscard]] constexpr auto enum_prev_value(E value, std::ptrdiff_t n = 1) noexcept -> detail::enable_if_t<E, optional<std::decay_t<E>>> {
110  using D = std::decay_t<E>;
111  constexpr std::ptrdiff_t count = detail::count_v<D, S>;
112 
113  if (const auto i = enum_index<D, S>(value)) {
114  const std::ptrdiff_t index = (static_cast<std::ptrdiff_t>(*i) - n);
115  if (index >= 0 && index < count) {
116  return enum_value<D, S>(static_cast<std::size_t>(index));
117  }
118  }
119  return {};
120 }
121 
122 template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
123 [[nodiscard]] constexpr auto enum_prev_value_circular(E value, std::ptrdiff_t n = 1) noexcept -> detail::enable_if_t<E, std::decay_t<E>> {
124  using D = std::decay_t<E>;
125  constexpr std::ptrdiff_t count = detail::count_v<D, S>;
126 
127  if (const auto i = enum_index<D, S>(value)) {
128  const std::ptrdiff_t index = ((((static_cast<std::ptrdiff_t>(*i) - n) % count) + count) % count);
129  if (index >= 0 && index < count) {
130  return enum_value<D, S>(static_cast<std::size_t>(index));
131  }
132  }
133  return MAGIC_ENUM_ASSERT(false), value;
134 }
135 
136 } // namespace magic_enum
137 
138 #endif // NEARGYE_MAGIC_ENUM_UTILITY_HPP
magic_enum.hpp
magic_enum::enum_for_each
constexpr auto enum_for_each(F &&f)
Definition: magic_enum_utility.hpp:67
magic_enum::detail::enable_if_t
typename enable_if_enum< std::is_enum_v< D > &&std::is_invocable_r_v< bool, BinaryPredicate, char_type, char_type >, R >::type enable_if_t
Definition: magic_enum.hpp:904
magic_enum::enum_next_value_circular
constexpr auto enum_next_value_circular(E value, std::ptrdiff_t n=1) noexcept -> detail::enable_if_t< E, std::decay_t< E >>
Definition: magic_enum_utility.hpp:95
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum::detail::for_each
constexpr auto for_each(F &&f, std::index_sequence< I... >)
Definition: magic_enum_utility.hpp:42
magic_enum::enum_prev_value_circular
constexpr auto enum_prev_value_circular(E value, std::ptrdiff_t n=1) noexcept -> detail::enable_if_t< E, std::decay_t< E >>
Definition: magic_enum_utility.hpp:123
MAGIC_ENUM_ASSERT
#define MAGIC_ENUM_ASSERT(...)
Definition: magic_enum.hpp:69
magic_enum::detail::n
constexpr auto n() noexcept
Definition: magic_enum.hpp:421
magic_enum::detail::enum_constant
std::integral_constant< E, V > enum_constant
Definition: magic_enum.hpp:228
std
magic_enum::enum_prev_value
constexpr auto enum_prev_value(E value, std::ptrdiff_t n=1) noexcept -> detail::enable_if_t< E, optional< std::decay_t< E >>>
Definition: magic_enum_utility.hpp:109
magic_enum::enum_next_value
constexpr auto enum_next_value(E value, std::ptrdiff_t n=1) noexcept -> detail::enable_if_t< E, optional< std::decay_t< E >>>
Definition: magic_enum_utility.hpp:81
magic_enum
Definition: magic_enum.hpp:126
magic_enum::detail::enum_subtype
enum_subtype
Definition: magic_enum.hpp:660
magic_enum::detail::all_invocable
constexpr bool all_invocable(std::index_sequence< I... >)
Definition: magic_enum_utility.hpp:56


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