magic_enum_flags.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_FLAGS_HPP
33 #define NEARGYE_MAGIC_ENUM_FLAGS_HPP
34 
35 #include "magic_enum.hpp"
36 
37 #if defined(__clang__)
38 # pragma clang diagnostic push
39 #elif defined(__GNUC__)
40 # pragma GCC diagnostic push
41 # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'.
42 #elif defined(_MSC_VER)
43 # pragma warning(push)
44 #endif
45 
46 namespace magic_enum {
47 
48 namespace detail {
49 
50 template <typename E, enum_subtype S, typename U = std::underlying_type_t<E>>
51 constexpr U values_ors() noexcept {
52  static_assert(S == enum_subtype::flags, "magic_enum::detail::values_ors requires valid subtype.");
53 
54  auto ors = U{0};
55  for (std::size_t i = 0; i < count_v<E, S>; ++i) {
56  ors |= static_cast<U>(values_v<E, S>[i]);
57  }
58 
59  return ors;
60 }
61 
62 } // namespace magic_enum::detail
63 
64 // Returns name from enum-flags value.
65 // If enum-flags value does not have name or value out of range, returns empty string.
66 template <typename E>
67 [[nodiscard]] auto enum_flags_name(E value, char_type sep = static_cast<char_type>('|')) -> detail::enable_if_t<E, string> {
68  using D = std::decay_t<E>;
69  using U = underlying_type_t<D>;
70  constexpr auto S = detail::enum_subtype::flags;
71  static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
72 
73  string name;
74  auto check_value = U{0};
75  for (std::size_t i = 0; i < detail::count_v<D, S>; ++i) {
76  if (const auto v = static_cast<U>(enum_value<D, S>(i)); (static_cast<U>(value) & v) != 0) {
77  if (const auto n = detail::names_v<D, S>[i]; !n.empty()) {
78  check_value |= v;
79  if (!name.empty()) {
80  name.append(1, sep);
81  }
82  name.append(n.data(), n.size());
83  } else {
84  return {}; // Value out of range.
85  }
86  }
87  }
88 
89  if (check_value != 0 && check_value == static_cast<U>(value)) {
90  return name;
91  }
92  return {}; // Invalid value or out of range.
93 }
94 
95 // Obtains enum-flags value from integer value.
96 // Returns optional with enum-flags value.
97 template <typename E>
99  using D = std::decay_t<E>;
100  using U = underlying_type_t<D>;
101  constexpr auto S = detail::enum_subtype::flags;
102  static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
103 
104  if constexpr (detail::count_v<D, S> == 0) {
105  static_cast<void>(value);
106  return {}; // Empty enum.
107  } else {
108  if constexpr (detail::is_sparse_v<D, S>) {
109  auto check_value = U{0};
110  for (std::size_t i = 0; i < detail::count_v<D, S>; ++i) {
111  if (const auto v = static_cast<U>(enum_value<D, S>(i)); (value & v) != 0) {
112  check_value |= v;
113  }
114  }
115 
116  if (check_value != 0 && check_value == value) {
117  return static_cast<D>(value);
118  }
119  } else {
120  constexpr auto min = detail::min_v<D, S>;
121  constexpr auto max = detail::values_ors<D, S>();
122 
123  if (value >= min && value <= max) {
124  return static_cast<D>(value);
125  }
126  }
127  return {}; // Invalid value or out of range.
128  }
129 }
130 
131 // Obtains enum-flags value from name.
132 // Returns optional with enum-flags value.
133 template <typename E, typename BinaryPredicate = std::equal_to<>>
134 [[nodiscard]] constexpr auto enum_flags_cast(string_view value, [[maybe_unused]] BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable<BinaryPredicate>()) -> detail::enable_if_t<E, optional<std::decay_t<E>>, BinaryPredicate> {
135  using D = std::decay_t<E>;
136  using U = underlying_type_t<D>;
137  constexpr auto S = detail::enum_subtype::flags;
138  static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min.");
139 
140  if constexpr (detail::count_v<D, S> == 0) {
141  static_cast<void>(value);
142  return {}; // Empty enum.
143  } else {
144  auto result = U{0};
145  while (!value.empty()) {
146  const auto d = detail::find(value, '|');
147  const auto s = (d == string_view::npos) ? value : value.substr(0, d);
148  auto f = U{0};
149  for (std::size_t i = 0; i < detail::count_v<D, S>; ++i) {
150  if (detail::cmp_equal(s, detail::names_v<D, S>[i], p)) {
151  f = static_cast<U>(enum_value<D, S>(i));
152  result |= f;
153  break;
154  }
155  }
156  if (f == U{0}) {
157  return {}; // Invalid value or out of range.
158  }
159  value.remove_prefix((d == string_view::npos) ? value.size() : d + 1);
160  }
161 
162  if (result != U{0}) {
163  return static_cast<D>(result);
164  }
165  return {}; // Invalid value or out of range.
166  }
167 }
168 
169 // Checks whether enum-flags contains value with such value.
170 template <typename E>
171 [[nodiscard]] constexpr auto enum_flags_contains(E value) noexcept -> detail::enable_if_t<E, bool> {
172  using D = std::decay_t<E>;
173  using U = underlying_type_t<D>;
174 
175  return static_cast<bool>(enum_flags_cast<D>(static_cast<U>(value)));
176 }
177 
178 // Checks whether enum-flags contains value with such integer value.
179 template <typename E>
181  using D = std::decay_t<E>;
182 
183  return static_cast<bool>(enum_flags_cast<D>(value));
184 }
185 
186 // Checks whether enum-flags contains enumerator with such name.
187 template <typename E, typename BinaryPredicate = std::equal_to<>>
188 [[nodiscard]] constexpr auto enum_flags_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable<BinaryPredicate>()) -> detail::enable_if_t<E, bool, BinaryPredicate> {
189  using D = std::decay_t<E>;
190 
191  return static_cast<bool>(enum_flags_cast<D>(value, std::move(p)));
192 }
193 
194 // Checks whether `flags set` contains `flag`.
195 // Note: If `flag` equals 0, it returns false, as 0 is not a flag.
196 template <typename E>
197 constexpr auto enum_flags_test(E flags, E flag) noexcept -> detail::enable_if_t<E, bool> {
198  using U = underlying_type_t<E>;
199 
200  return static_cast<U>(flag) && ((static_cast<U>(flags) & static_cast<U>(flag)) == static_cast<U>(flag));
201 }
202 
203 // Checks whether `lhs flags set` and `rhs flags set` have common flags.
204 // Note: If `lhs flags set` or `rhs flags set` equals 0, it returns false, as 0 is not a flag, and therfore cannot have any matching flag.
205 template <typename E>
206 constexpr auto enum_flags_test_any(E lhs, E rhs) noexcept -> detail::enable_if_t<E, bool> {
207  using U = underlying_type_t<E>;
208 
209  return (static_cast<U>(lhs) & static_cast<U>(rhs)) != 0;
210 }
211 
212 } // namespace magic_enum
213 
214 #if defined(__clang__)
215 # pragma clang diagnostic pop
216 #elif defined(__GNUC__)
217 # pragma GCC diagnostic pop
218 #elif defined(_MSC_VER)
219 # pragma warning(pop)
220 #endif
221 
222 #endif // NEARGYE_MAGIC_ENUM_FLAGS_HPP
magic_enum::detail::enum_subtype::flags
@ flags
magic_enum::enum_flags_contains
constexpr auto enum_flags_contains(E value) noexcept -> detail::enable_if_t< E, bool >
Definition: magic_enum_flags.hpp:171
magic_enum::char_type
string_view::value_type char_type
Definition: magic_enum.hpp:149
magic_enum::detail::cmp_equal
constexpr bool cmp_equal(string_view lhs, string_view rhs, [[maybe_unused]] BinaryPredicate &&p) noexcept(is_nothrow_invocable< BinaryPredicate >())
Definition: magic_enum.hpp:347
magic_enum.hpp
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_flags_name
auto enum_flags_name(E value, char_type sep=static_cast< char_type >('|')) -> detail::enable_if_t< E, string >
Definition: magic_enum_flags.hpp:67
magic_enum::enum_flags_test_any
constexpr auto enum_flags_test_any(E lhs, E rhs) noexcept -> detail::enable_if_t< E, bool >
Definition: magic_enum_flags.hpp:206
magic_enum::detail::value
constexpr E value(std::size_t i) noexcept
Definition: magic_enum.hpp:679
magic_enum::underlying_type_t
typename underlying_type< T >::type underlying_type_t
Definition: magic_enum.hpp:1168
magic_enum::enum_flags_test
constexpr auto enum_flags_test(E flags, E flag) noexcept -> detail::enable_if_t< E, bool >
Definition: magic_enum_flags.hpp:197
magic_enum::detail::n
constexpr auto n() noexcept
Definition: magic_enum.hpp:421
magic_enum::detail::find
constexpr std::size_t find(string_view str, char_type c) noexcept
Definition: magic_enum.hpp:312
magic_enum
Definition: magic_enum.hpp:126
magic_enum::enum_flags_cast
constexpr auto enum_flags_cast(underlying_type_t< E > value) noexcept -> detail::enable_if_t< E, optional< std::decay_t< E >>>
Definition: magic_enum_flags.hpp:98
magic_enum::detail::values_ors
constexpr U values_ors() noexcept
Definition: magic_enum_flags.hpp:51


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