magic_enum.hpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. // __ __ _ ______ _____
  2. // | \/ | (_) | ____| / ____|_ _
  3. // | \ / | __ _ __ _ _ ___ | |__ _ __ _ _ _ __ ___ | | _| |_ _| |_
  4. // | |\/| |/ _` |/ _` | |/ __| | __| | '_ \| | | | '_ ` _ \ | | |_ _|_ _|
  5. // | | | | (_| | (_| | | (__ | |____| | | | |_| | | | | | | | |____|_| |_|
  6. // |_| |_|\__,_|\__, |_|\___| |______|_| |_|\__,_|_| |_| |_| \_____|
  7. // __/ | https://github.com/Neargye/magic_enum
  8. // |___/ version 0.7.3
  9. //
  10. // Licensed under the MIT License <http://opensource.org/licenses/MIT>.
  11. // SPDX-License-Identifier: MIT
  12. // Copyright (c) 2019 - 2021 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_HPP
  32. #define NEARGYE_MAGIC_ENUM_HPP
  33. #define MAGIC_ENUM_VERSION_MAJOR 0
  34. #define MAGIC_ENUM_VERSION_MINOR 7
  35. #define MAGIC_ENUM_VERSION_PATCH 3
  36. #include <array>
  37. #include <cassert>
  38. #include <cstdint>
  39. #include <cstddef>
  40. #include <iosfwd>
  41. #include <limits>
  42. #include <type_traits>
  43. #include <utility>
  44. #if defined(MAGIC_ENUM_CONFIG_FILE)
  45. #error #include MAGIC_ENUM_CONFIG_FILE
  46. #endif
  47. #if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
  48. #include <optional>
  49. #endif
  50. #if !defined(MAGIC_ENUM_USING_ALIAS_STRING)
  51. #include <string>
  52. #endif
  53. #if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
  54. #include <string_view>
  55. #endif
  56. #if defined(__clang__)
  57. # pragma clang diagnostic push
  58. # pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
  59. #elif defined(__GNUC__)
  60. # pragma GCC diagnostic push
  61. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'.
  62. #elif defined(_MSC_VER)
  63. # pragma warning(push)
  64. # pragma warning(disable : 26495) // Variable 'static_string<N>::chars_' is uninitialized.
  65. # pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value.
  66. # pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call.
  67. #endif
  68. // Checks magic_enum compiler compatibility.
  69. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910
  70. # undef MAGIC_ENUM_SUPPORTED
  71. # define MAGIC_ENUM_SUPPORTED 1
  72. #endif
  73. // Checks magic_enum compiler aliases compatibility.
  74. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920
  75. # undef MAGIC_ENUM_SUPPORTED_ALIASES
  76. # define MAGIC_ENUM_SUPPORTED_ALIASES 1
  77. #endif
  78. // Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128.
  79. // If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN.
  80. #if !defined(MAGIC_ENUM_RANGE_MIN)
  81. # define MAGIC_ENUM_RANGE_MIN -128
  82. #endif
  83. // Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128.
  84. // If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX.
  85. #if !defined(MAGIC_ENUM_RANGE_MAX)
  86. # define MAGIC_ENUM_RANGE_MAX 128
  87. #endif
  88. namespace magic_enum {
  89. // If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_OPTIONAL.
  90. #if defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
  91. MAGIC_ENUM_USING_ALIAS_OPTIONAL
  92. #else
  93. using std::optional;
  94. #endif
  95. // If need another string_view type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW.
  96. #if defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
  97. MAGIC_ENUM_USING_ALIAS_STRING_VIEW
  98. #else
  99. using std::string_view;
  100. #endif
  101. // If need another string type, define the macro MAGIC_ENUM_USING_ALIAS_STRING.
  102. #if defined(MAGIC_ENUM_USING_ALIAS_STRING)
  103. MAGIC_ENUM_USING_ALIAS_STRING
  104. #else
  105. using std::string;
  106. #endif
  107. namespace customize {
  108. // Enum value must be in range [MAGIC_ENUM_RANGE_MIN, MAGIC_ENUM_RANGE_MAX]. By default MAGIC_ENUM_RANGE_MIN = -128, MAGIC_ENUM_RANGE_MAX = 128.
  109. // If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.
  110. // If need another range for specific enum type, add specialization enum_range for necessary enum type.
  111. template <typename E>
  112. struct enum_range {
  113. static_assert(std::is_enum_v<E>, "magic_enum::customize::enum_range requires enum type.");
  114. inline static constexpr int min = MAGIC_ENUM_RANGE_MIN;
  115. inline static constexpr int max = MAGIC_ENUM_RANGE_MAX;
  116. static_assert(max > min, "magic_enum::customize::enum_range requires max > min.");
  117. };
  118. static_assert(MAGIC_ENUM_RANGE_MIN <= 0, "MAGIC_ENUM_RANGE_MIN must be less or equals than 0.");
  119. static_assert(MAGIC_ENUM_RANGE_MIN > (std::numeric_limits<std::int16_t>::min)(), "MAGIC_ENUM_RANGE_MIN must be greater than INT16_MIN.");
  120. static_assert(MAGIC_ENUM_RANGE_MAX > 0, "MAGIC_ENUM_RANGE_MAX must be greater than 0.");
  121. static_assert(MAGIC_ENUM_RANGE_MAX < (std::numeric_limits<std::int16_t>::max)(), "MAGIC_ENUM_RANGE_MAX must be less than INT16_MAX.");
  122. static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN.");
  123. // If need custom names for enum, add specialization enum_name for necessary enum type.
  124. template <typename E>
  125. constexpr string_view enum_name(E) noexcept {
  126. static_assert(std::is_enum_v<E>, "magic_enum::customize::enum_name requires enum type.");
  127. return {};
  128. }
  129. } // namespace magic_enum::customize
  130. namespace detail {
  131. template <typename T>
  132. struct supported
  133. #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT)
  134. : std::true_type {};
  135. #else
  136. : std::false_type {};
  137. #endif
  138. struct char_equal_to {
  139. constexpr bool operator()(char lhs, char rhs) const noexcept {
  140. return lhs == rhs;
  141. }
  142. };
  143. template <std::size_t N>
  144. class static_string {
  145. public:
  146. constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_index_sequence<N>{}} {
  147. assert(str.size() == N);
  148. }
  149. constexpr const char* data() const noexcept { return chars_; }
  150. constexpr std::size_t size() const noexcept { return N; }
  151. constexpr operator string_view() const noexcept { return {data(), size()}; }
  152. private:
  153. template <std::size_t... I>
  154. constexpr static_string(string_view str, std::index_sequence<I...>) noexcept : chars_{str[I]..., '\0'} {}
  155. char chars_[N + 1];
  156. };
  157. template <>
  158. class static_string<0> {
  159. public:
  160. constexpr explicit static_string(string_view) noexcept {}
  161. constexpr const char* data() const noexcept { return nullptr; }
  162. constexpr std::size_t size() const noexcept { return 0; }
  163. constexpr operator string_view() const noexcept { return {}; }
  164. };
  165. constexpr string_view pretty_name(string_view name) noexcept {
  166. for (std::size_t i = name.size(); i > 0; --i) {
  167. if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||
  168. (name[i - 1] >= 'a' && name[i - 1] <= 'z') ||
  169. (name[i - 1] >= 'A' && name[i - 1] <= 'Z') ||
  170. (name[i - 1] == '_'))) {
  171. name.remove_prefix(i);
  172. break;
  173. }
  174. }
  175. if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') ||
  176. (name.front() >= 'A' && name.front() <= 'Z') ||
  177. (name.front() == '_'))) {
  178. return name;
  179. }
  180. return {}; // Invalid name.
  181. }
  182. constexpr std::size_t find(string_view str, char c) noexcept {
  183. #if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
  184. // https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc
  185. // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
  186. constexpr bool workaround = true;
  187. #else
  188. constexpr bool workaround = false;
  189. #endif
  190. if constexpr (workaround) {
  191. for (std::size_t i = 0; i < str.size(); ++i) {
  192. if (str[i] == c) {
  193. return i;
  194. }
  195. }
  196. return string_view::npos;
  197. } else {
  198. return str.find_first_of(c);
  199. }
  200. }
  201. template <typename T, std::size_t N, std::size_t... I>
  202. constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N], std::index_sequence<I...>) {
  203. return {{a[I]...}};
  204. }
  205. template <typename BinaryPredicate>
  206. constexpr bool cmp_equal(string_view lhs, string_view rhs, BinaryPredicate&& p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) {
  207. #if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
  208. // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
  209. // https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html
  210. constexpr bool workaround = true;
  211. #else
  212. constexpr bool workaround = false;
  213. #endif
  214. constexpr bool default_predicate = std::is_same_v<std::decay_t<BinaryPredicate>, char_equal_to>;
  215. if constexpr (default_predicate && !workaround) {
  216. static_cast<void>(p);
  217. return lhs == rhs;
  218. } else {
  219. if (lhs.size() != rhs.size()) {
  220. return false;
  221. }
  222. const auto size = lhs.size();
  223. for (std::size_t i = 0; i < size; ++i) {
  224. if (!p(lhs[i], rhs[i])) {
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. }
  231. template <typename L, typename R>
  232. constexpr bool cmp_less(L lhs, R rhs) noexcept {
  233. static_assert(std::is_integral_v<L> && std::is_integral_v<R>, "magic_enum::detail::cmp_less requires integral type.");
  234. if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
  235. // If same signedness (both signed or both unsigned).
  236. return lhs < rhs;
  237. } else if constexpr (std::is_signed_v<R>) {
  238. // If 'right' is negative, then result is 'false', otherwise cast & compare.
  239. return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs);
  240. } else {
  241. // If 'left' is negative, then result is 'true', otherwise cast & compare.
  242. return lhs < 0 || static_cast<std::make_unsigned_t<L>>(lhs) < rhs;
  243. }
  244. }
  245. template <typename I>
  246. constexpr I log2(I value) noexcept {
  247. static_assert(std::is_integral_v<I>, "magic_enum::detail::log2 requires integral type.");
  248. auto ret = I{0};
  249. for (; value > I{1}; value >>= I{1}, ++ret) {}
  250. return ret;
  251. }
  252. template <typename I>
  253. constexpr bool is_pow2(I x) noexcept {
  254. static_assert(std::is_integral_v<I>, "magic_enum::detail::is_pow2 requires integral type.");
  255. return x != 0 && (x & (x - 1)) == 0;
  256. }
  257. template <typename T>
  258. inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::decay_t<T>>;
  259. template <typename E>
  260. constexpr auto n() noexcept {
  261. static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
  262. #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
  263. # if defined(__clang__)
  264. constexpr string_view name{__PRETTY_FUNCTION__ + 34, sizeof(__PRETTY_FUNCTION__) - 36};
  265. # elif defined(__GNUC__)
  266. constexpr string_view name{__PRETTY_FUNCTION__ + 49, sizeof(__PRETTY_FUNCTION__) - 51};
  267. # elif defined(_MSC_VER)
  268. constexpr string_view name{__FUNCSIG__ + 40, sizeof(__FUNCSIG__) - 57};
  269. # endif
  270. return static_string<name.size()>{name};
  271. #else
  272. return string_view{}; // Unsupported compiler.
  273. #endif
  274. }
  275. template <typename E>
  276. inline constexpr auto type_name_v = n<E>();
  277. template <typename E, E V>
  278. constexpr auto n() noexcept {
  279. static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
  280. constexpr auto custom_name = customize::enum_name<E>(V);
  281. if constexpr (custom_name.empty()) {
  282. static_cast<void>(custom_name);
  283. #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
  284. # if defined(__clang__) || defined(__GNUC__)
  285. constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
  286. # elif defined(_MSC_VER)
  287. constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
  288. # endif
  289. return static_string<name.size()>{name};
  290. #else
  291. return string_view{}; // Unsupported compiler.
  292. #endif
  293. } else {
  294. return static_string<custom_name.size()>{custom_name};
  295. }
  296. }
  297. template <typename E, E V>
  298. inline constexpr auto enum_name_v = n<E, V>();
  299. template <typename E, auto V>
  300. constexpr bool is_valid() noexcept {
  301. static_assert(is_enum_v<E>, "magic_enum::detail::is_valid requires enum type.");
  302. return n<E, static_cast<E>(V)>().size() != 0;
  303. }
  304. template <typename E, int O, bool IsFlags = false, typename U = std::underlying_type_t<E>>
  305. constexpr E value(std::size_t i) noexcept {
  306. static_assert(is_enum_v<E>, "magic_enum::detail::value requires enum type.");
  307. if constexpr (IsFlags) {
  308. return static_cast<E>(U{1} << static_cast<U>(static_cast<int>(i) + O));
  309. } else {
  310. return static_cast<E>(static_cast<int>(i) + O);
  311. }
  312. }
  313. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  314. constexpr int reflected_min() noexcept {
  315. static_assert(is_enum_v<E>, "magic_enum::detail::reflected_min requires enum type.");
  316. if constexpr (IsFlags) {
  317. return 0;
  318. } else {
  319. constexpr auto lhs = customize::enum_range<E>::min;
  320. static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "magic_enum::enum_range requires min must be greater than INT16_MIN.");
  321. constexpr auto rhs = (std::numeric_limits<U>::min)();
  322. if constexpr (cmp_less(lhs, rhs)) {
  323. return rhs;
  324. } else {
  325. static_assert(!is_valid<E, value<E, lhs - 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value smaller than min range size.");
  326. return lhs;
  327. }
  328. }
  329. }
  330. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  331. constexpr int reflected_max() noexcept {
  332. static_assert(is_enum_v<E>, "magic_enum::detail::reflected_max requires enum type.");
  333. if constexpr (IsFlags) {
  334. return std::numeric_limits<U>::digits - 1;
  335. } else {
  336. constexpr auto lhs = customize::enum_range<E>::max;
  337. static_assert(lhs < (std::numeric_limits<std::int16_t>::max)(), "magic_enum::enum_range requires max must be less than INT16_MAX.");
  338. constexpr auto rhs = (std::numeric_limits<U>::max)();
  339. if constexpr (cmp_less(lhs, rhs)) {
  340. static_assert(!is_valid<E, value<E, lhs + 1, IsFlags>(0)>(), "magic_enum::enum_range detects enum value larger than max range size.");
  341. return lhs;
  342. } else {
  343. return rhs;
  344. }
  345. }
  346. }
  347. template <typename E, bool IsFlags = false>
  348. inline constexpr auto reflected_min_v = reflected_min<E, IsFlags>();
  349. template <typename E, bool IsFlags = false>
  350. inline constexpr auto reflected_max_v = reflected_max<E, IsFlags>();
  351. template <std::size_t N>
  352. constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
  353. auto count = std::size_t{0};
  354. for (std::size_t i = 0; i < N; ++i) {
  355. if (valid[i]) {
  356. ++count;
  357. }
  358. }
  359. return count;
  360. }
  361. template <typename E, bool IsFlags, int Min, std::size_t... I>
  362. constexpr auto values(std::index_sequence<I...>) noexcept {
  363. static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
  364. constexpr bool valid[sizeof...(I)] = {is_valid<E, value<E, Min, IsFlags>(I)>()...};
  365. constexpr std::size_t count = values_count(valid);
  366. if constexpr (count > 0) {
  367. E values[count] = {};
  368. for (std::size_t i = 0, v = 0; v < count; ++i) {
  369. if (valid[i]) {
  370. values[v++] = value<E, Min, IsFlags>(i);
  371. }
  372. }
  373. return to_array(values, std::make_index_sequence<count>{});
  374. } else {
  375. return std::array<E, 0>{};
  376. }
  377. }
  378. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  379. constexpr auto values() noexcept {
  380. static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
  381. constexpr auto min = reflected_min_v<E, IsFlags>;
  382. constexpr auto max = reflected_max_v<E, IsFlags>;
  383. constexpr auto range_size = max - min + 1;
  384. static_assert(range_size > 0, "magic_enum::enum_range requires valid size.");
  385. static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "magic_enum::enum_range requires valid size.");
  386. return values<E, IsFlags, reflected_min_v<E, IsFlags>>(std::make_index_sequence<range_size>{});
  387. }
  388. template <typename E, bool IsFlags = false>
  389. inline constexpr auto values_v = values<E, IsFlags>();
  390. template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
  391. using values_t = decltype((values_v<D, IsFlags>));
  392. template <typename E, bool IsFlags = false>
  393. inline constexpr auto count_v = values_v<E, IsFlags>.size();
  394. template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
  395. inline constexpr auto min_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.front()) : U{0};
  396. template <typename E, bool IsFlags = false, typename U = std::underlying_type_t<E>>
  397. inline constexpr auto max_v = (count_v<E, IsFlags> > 0) ? static_cast<U>(values_v<E, IsFlags>.back()) : U{0};
  398. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  399. constexpr std::size_t range_size() noexcept {
  400. static_assert(is_enum_v<E>, "magic_enum::detail::range_size requires enum type.");
  401. constexpr auto max = IsFlags ? log2(max_v<E, IsFlags>) : max_v<E, IsFlags>;
  402. constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
  403. constexpr auto range_size = max - min + U{1};
  404. static_assert(range_size > 0, "magic_enum::enum_range requires valid size.");
  405. static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "magic_enum::enum_range requires valid size.");
  406. return static_cast<std::size_t>(range_size);
  407. }
  408. template <typename E, bool IsFlags = false>
  409. inline constexpr auto range_size_v = range_size<E, IsFlags>();
  410. template <typename E, bool IsFlags = false>
  411. using index_t = std::conditional_t<range_size_v<E, IsFlags> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>;
  412. template <typename E, bool IsFlags = false>
  413. inline constexpr auto invalid_index_v = (std::numeric_limits<index_t<E, IsFlags>>::max)();
  414. template <typename E, bool IsFlags, std::size_t... I>
  415. constexpr auto indexes(std::index_sequence<I...>) noexcept {
  416. static_assert(is_enum_v<E>, "magic_enum::detail::indexes requires enum type.");
  417. constexpr auto min = IsFlags ? log2(min_v<E, IsFlags>) : min_v<E, IsFlags>;
  418. [[maybe_unused]] auto i = index_t<E, IsFlags>{0};
  419. return std::array<decltype(i), sizeof...(I)>{{(is_valid<E, value<E, min, IsFlags>(I)>() ? i++ : invalid_index_v<E, IsFlags>)...}};
  420. }
  421. template <typename E, bool IsFlags = false>
  422. inline constexpr auto indexes_v = indexes<E, IsFlags>(std::make_index_sequence<range_size_v<E, IsFlags>>{});
  423. template <typename E, bool IsFlags, std::size_t... I>
  424. constexpr auto names(std::index_sequence<I...>) noexcept {
  425. static_assert(is_enum_v<E>, "magic_enum::detail::names requires enum type.");
  426. return std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E, IsFlags>[I]>...}};
  427. }
  428. template <typename E, bool IsFlags = false>
  429. inline constexpr auto names_v = names<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
  430. template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
  431. using names_t = decltype((names_v<D, IsFlags>));
  432. template <typename E, bool IsFlags, std::size_t... I>
  433. constexpr auto entries(std::index_sequence<I...>) noexcept {
  434. static_assert(is_enum_v<E>, "magic_enum::detail::entries requires enum type.");
  435. return std::array<std::pair<E, string_view>, sizeof...(I)>{{{values_v<E, IsFlags>[I], enum_name_v<E, values_v<E, IsFlags>[I]>}...}};
  436. }
  437. template <typename E, bool IsFlags = false>
  438. inline constexpr auto entries_v = entries<E, IsFlags>(std::make_index_sequence<count_v<E, IsFlags>>{});
  439. template <typename E, bool IsFlags = false, typename D = std::decay_t<E>>
  440. using entries_t = decltype((entries_v<D, IsFlags>));
  441. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  442. constexpr bool is_sparse() noexcept {
  443. static_assert(is_enum_v<E>, "magic_enum::detail::is_sparse requires enum type.");
  444. return range_size_v<E, IsFlags> != count_v<E, IsFlags>;
  445. }
  446. template <typename E, bool IsFlags = false>
  447. inline constexpr bool is_sparse_v = is_sparse<E, IsFlags>();
  448. template <typename E, typename U = std::underlying_type_t<E>>
  449. constexpr std::size_t undex(U value) noexcept {
  450. static_assert(is_enum_v<E>, "magic_enum::detail::undex requires enum type.");
  451. if (const auto i = static_cast<std::size_t>(value - min_v<E>); value >= min_v<E> && value <= max_v<E>) {
  452. if constexpr (is_sparse_v<E>) {
  453. if (const auto idx = indexes_v<E>[i]; idx != invalid_index_v<E>) {
  454. return idx;
  455. }
  456. } else {
  457. return i;
  458. }
  459. }
  460. return invalid_index_v<E>; // Value out of range.
  461. }
  462. template <typename E, typename U = std::underlying_type_t<E>>
  463. constexpr std::size_t endex(E value) noexcept {
  464. static_assert(is_enum_v<E>, "magic_enum::detail::endex requires enum type.");
  465. return undex<E>(static_cast<U>(value));
  466. }
  467. template <typename E, typename U = std::underlying_type_t<E>>
  468. constexpr U value_ors() noexcept {
  469. static_assert(is_enum_v<E>, "magic_enum::detail::endex requires enum type.");
  470. auto value = U{0};
  471. for (std::size_t i = 0; i < count_v<E, true>; ++i) {
  472. value |= static_cast<U>(values_v<E, true>[i]);
  473. }
  474. return value;
  475. }
  476. template <bool, typename T, typename R>
  477. struct enable_if_enum {};
  478. template <typename T, typename R>
  479. struct enable_if_enum<true, T, R> {
  480. using type = R;
  481. using D = std::decay_t<T>;
  482. static_assert(supported<D>::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
  483. };
  484. template <typename T, typename R = void>
  485. using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
  486. template <typename T, typename Enable = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
  487. using enum_concept = T;
  488. template <typename T, bool = std::is_enum_v<T>>
  489. struct is_scoped_enum : std::false_type {};
  490. template <typename T>
  491. struct is_scoped_enum<T, true> : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
  492. template <typename T, bool = std::is_enum_v<T>>
  493. struct is_unscoped_enum : std::false_type {};
  494. template <typename T>
  495. struct is_unscoped_enum<T, true> : std::bool_constant<std::is_convertible_v<T, std::underlying_type_t<T>>> {};
  496. template <typename T, bool = std::is_enum_v<std::decay_t<T>>>
  497. struct underlying_type {};
  498. template <typename T>
  499. struct underlying_type<T, true> : std::underlying_type<std::decay_t<T>> {};
  500. } // namespace magic_enum::detail
  501. // Checks is magic_enum supported compiler.
  502. inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
  503. template <typename T>
  504. using Enum = detail::enum_concept<T>;
  505. // Checks whether T is an Unscoped enumeration type.
  506. // Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type. Otherwise, value is equal to false.
  507. template <typename T>
  508. struct is_unscoped_enum : detail::is_unscoped_enum<T> {};
  509. template <typename T>
  510. inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
  511. // Checks whether T is an Scoped enumeration type.
  512. // Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type. Otherwise, value is equal to false.
  513. template <typename T>
  514. struct is_scoped_enum : detail::is_scoped_enum<T> {};
  515. template <typename T>
  516. inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
  517. // If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.
  518. // Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed.
  519. template <typename T>
  520. struct underlying_type : detail::underlying_type<T> {};
  521. template <typename T>
  522. using underlying_type_t = typename underlying_type<T>::type;
  523. // Returns type name of enum.
  524. template <typename E>
  525. [[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_enum_t<E, string_view> {
  526. using D = std::decay_t<E>;
  527. constexpr string_view name = detail::type_name_v<D>;
  528. static_assert(name.size() > 0, "Enum type does not have a name.");
  529. return name;
  530. }
  531. // Returns number of enum values.
  532. template <typename E>
  533. [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t<E, std::size_t> {
  534. using D = std::decay_t<E>;
  535. return detail::count_v<D>;
  536. }
  537. // Returns enum value at specified index.
  538. // No bounds checking is performed: the behavior is undefined if index >= number of enum values.
  539. template <typename E>
  540. [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t<E, std::decay_t<E>> {
  541. using D = std::decay_t<E>;
  542. static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
  543. if constexpr (detail::is_sparse_v<D>) {
  544. return assert((index < detail::count_v<D>)), detail::values_v<D>[index];
  545. } else {
  546. return assert((index < detail::count_v<D>)), detail::value<D, detail::min_v<D>>(index);
  547. }
  548. }
  549. // Returns std::array with enum values, sorted by enum value.
  550. template <typename E>
  551. [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t<E, detail::values_t<E>> {
  552. using D = std::decay_t<E>;
  553. static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
  554. return detail::values_v<D>;
  555. }
  556. // Returns name from static storage enum variable.
  557. // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
  558. template <auto V>
  559. [[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_enum_t<decltype(V), string_view> {
  560. using D = std::decay_t<decltype(V)>;
  561. constexpr string_view name = detail::enum_name_v<D, V>;
  562. static_assert(name.size() > 0, "Enum value does not have a name.");
  563. return name;
  564. }
  565. // Returns name from enum value.
  566. // If enum value does not have name or value out of range, returns empty string.
  567. template <typename E>
  568. [[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_enum_t<E, string_view> {
  569. using D = std::decay_t<E>;
  570. if (const auto i = detail::endex<D>(value); i != detail::invalid_index_v<D>) {
  571. return detail::names_v<D>[i];
  572. }
  573. return {}; // Invalid value or out of range.
  574. }
  575. // Returns std::array with names, sorted by enum value.
  576. template <typename E>
  577. [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t<E, detail::names_t<E>> {
  578. using D = std::decay_t<E>;
  579. static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
  580. return detail::names_v<D>;
  581. }
  582. // Returns std::array with pairs (value, name), sorted by enum value.
  583. template <typename E>
  584. [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t<E, detail::entries_t<E>> {
  585. using D = std::decay_t<E>;
  586. static_assert(detail::count_v<D> > 0, "magic_enum requires enum implementation and valid max and min.");
  587. return detail::entries_v<D>;
  588. }
  589. // Obtains enum value from integer value.
  590. // Returns optional with enum value.
  591. template <typename E>
  592. [[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
  593. using D = std::decay_t<E>;
  594. if (detail::undex<D>(value) != detail::invalid_index_v<D>) {
  595. return static_cast<D>(value);
  596. }
  597. return {}; // Invalid value or out of range.
  598. }
  599. // Obtains enum value from name.
  600. // Returns optional with enum value.
  601. template <typename E, typename BinaryPredicate>
  602. [[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
  603. static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::enum_cast requires bool(char, char) invocable predicate.");
  604. using D = std::decay_t<E>;
  605. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  606. if (detail::cmp_equal(value, detail::names_v<D>[i], p)) {
  607. return enum_value<D>(i);
  608. }
  609. }
  610. return {}; // Invalid value or out of range.
  611. }
  612. // Obtains enum value from name.
  613. // Returns optional with enum value.
  614. template <typename E>
  615. [[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
  616. using D = std::decay_t<E>;
  617. return enum_cast<D>(value, detail::char_equal_to{});
  618. }
  619. // Returns integer value from enum value.
  620. template <typename E>
  621. [[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_enum_t<E, underlying_type_t<E>> {
  622. return static_cast<underlying_type_t<E>>(value);
  623. }
  624. // Obtains index in enum values from enum value.
  625. // Returns optional with index.
  626. template <typename E>
  627. [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t<E, optional<std::size_t>> {
  628. using D = std::decay_t<E>;
  629. if (const auto i = detail::endex<D>(value); i != detail::invalid_index_v<D>) {
  630. return i;
  631. }
  632. return {}; // Invalid value or out of range.
  633. }
  634. // Checks whether enum contains enumerator with such enum value.
  635. template <typename E>
  636. [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t<E, bool> {
  637. using D = std::decay_t<E>;
  638. return detail::endex<D>(value) != detail::invalid_index_v<D>;
  639. }
  640. // Checks whether enum contains enumerator with such integer value.
  641. template <typename E>
  642. [[nodiscard]] constexpr auto enum_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, bool> {
  643. using D = std::decay_t<E>;
  644. return detail::undex<D>(value) != detail::invalid_index_v<D>;
  645. }
  646. // Checks whether enum contains enumerator with such name.
  647. template <typename E, typename BinaryPredicate>
  648. [[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, bool> {
  649. static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::enum_contains requires bool(char, char) invocable predicate.");
  650. using D = std::decay_t<E>;
  651. return enum_cast<D>(value, std::move_if_noexcept(p)).has_value();
  652. }
  653. // Checks whether enum contains enumerator with such name.
  654. template <typename E>
  655. [[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t<E, bool> {
  656. using D = std::decay_t<E>;
  657. return enum_cast<D>(value).has_value();
  658. }
  659. namespace ostream_operators {
  660. template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  661. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, E value) {
  662. using D = std::decay_t<E>;
  663. using U = underlying_type_t<D>;
  664. #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
  665. if (const auto name = magic_enum::enum_name<D>(value); !name.empty()) {
  666. for (const auto c : name) {
  667. os.put(c);
  668. }
  669. return os;
  670. }
  671. #endif
  672. return (os << static_cast<U>(value));
  673. }
  674. template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  675. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, optional<E> value) {
  676. return value.has_value() ? (os << value.value()) : os;
  677. }
  678. } // namespace magic_enum::ostream_operators
  679. namespace bitwise_operators {
  680. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  681. constexpr E operator~(E rhs) noexcept {
  682. return static_cast<E>(~static_cast<underlying_type_t<E>>(rhs));
  683. }
  684. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  685. constexpr E operator|(E lhs, E rhs) noexcept {
  686. return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) | static_cast<underlying_type_t<E>>(rhs));
  687. }
  688. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  689. constexpr E operator&(E lhs, E rhs) noexcept {
  690. return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) & static_cast<underlying_type_t<E>>(rhs));
  691. }
  692. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  693. constexpr E operator^(E lhs, E rhs) noexcept {
  694. return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) ^ static_cast<underlying_type_t<E>>(rhs));
  695. }
  696. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  697. constexpr E& operator|=(E& lhs, E rhs) noexcept {
  698. return lhs = (lhs | rhs);
  699. }
  700. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  701. constexpr E& operator&=(E& lhs, E rhs) noexcept {
  702. return lhs = (lhs & rhs);
  703. }
  704. template <typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  705. constexpr E& operator^=(E& lhs, E rhs) noexcept {
  706. return lhs = (lhs ^ rhs);
  707. }
  708. } // namespace magic_enum::bitwise_operators
  709. namespace flags {
  710. // Returns type name of enum.
  711. using magic_enum::enum_type_name;
  712. // Returns number of enum-flags values.
  713. template <typename E>
  714. [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_enum_t<E, std::size_t> {
  715. using D = std::decay_t<E>;
  716. return detail::count_v<D, true>;
  717. }
  718. // Returns enum-flags value at specified index.
  719. // No bounds checking is performed: the behavior is undefined if index >= number of enum-flags values.
  720. template <typename E>
  721. [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_enum_t<E, std::decay_t<E>> {
  722. using D = std::decay_t<E>;
  723. static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
  724. if constexpr (detail::is_sparse_v<D, true>) {
  725. return assert((index < detail::count_v<D, true>)), detail::values_v<D, true>[index];
  726. } else {
  727. constexpr auto min = detail::log2(detail::min_v<D, true>);
  728. return assert((index < detail::count_v<D, true>)), detail::value<D, min, true>(index);
  729. }
  730. }
  731. // Returns std::array with enum-flags values, sorted by enum-flags value.
  732. template <typename E>
  733. [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_enum_t<E, detail::values_t<E, true>> {
  734. using D = std::decay_t<E>;
  735. static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
  736. return detail::values_v<D, true>;
  737. }
  738. // Returns name from enum-flags value.
  739. // If enum-flags value does not have name or value out of range, returns empty string.
  740. template <typename E>
  741. [[nodiscard]] auto enum_name(E value) -> detail::enable_if_enum_t<E, string> {
  742. using D = std::decay_t<E>;
  743. using U = underlying_type_t<D>;
  744. string name;
  745. auto check_value = U{0};
  746. for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
  747. if (const auto v = static_cast<U>(enum_value<D>(i)); (static_cast<U>(value) & v) != 0) {
  748. check_value |= v;
  749. const auto n = detail::names_v<D, true>[i];
  750. if (!name.empty()) {
  751. name.append(1, '|');
  752. }
  753. name.append(n.data(), n.size());
  754. }
  755. }
  756. if (check_value != 0 && check_value == static_cast<U>(value)) {
  757. return name;
  758. }
  759. return {}; // Invalid value or out of range.
  760. }
  761. // Returns std::array with string names, sorted by enum-flags value.
  762. template <typename E>
  763. [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_enum_t<E, detail::names_t<E, true>> {
  764. using D = std::decay_t<E>;
  765. static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
  766. return detail::names_v<D, true>;
  767. }
  768. // Returns std::array with pairs (value, name), sorted by enum-flags value.
  769. template <typename E>
  770. [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_enum_t<E, detail::entries_t<E, true>> {
  771. using D = std::decay_t<E>;
  772. static_assert(detail::count_v<D, true> > 0, "magic_enum::flags requires enum-flags implementation.");
  773. return detail::entries_v<D, true>;
  774. }
  775. // Obtains enum-flags value from integer value.
  776. // Returns optional with enum-flags value.
  777. template <typename E>
  778. [[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
  779. using D = std::decay_t<E>;
  780. using U = underlying_type_t<D>;
  781. if constexpr (detail::is_sparse_v<D, true>) {
  782. auto check_value = U{0};
  783. for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
  784. if (const auto v = static_cast<U>(enum_value<D>(i)); (value & v) != 0) {
  785. check_value |= v;
  786. }
  787. }
  788. if (check_value != 0 && check_value == value) {
  789. return static_cast<D>(value);
  790. }
  791. } else {
  792. constexpr auto min = detail::min_v<D, true>;
  793. constexpr auto max = detail::value_ors<D>();
  794. if (value >= min && value <= max) {
  795. return static_cast<D>(value);
  796. }
  797. }
  798. return {}; // Invalid value or out of range.
  799. }
  800. // Obtains enum-flags value from name.
  801. // Returns optional with enum-flags value.
  802. template <typename E, typename BinaryPredicate>
  803. [[nodiscard]] constexpr auto enum_cast(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
  804. static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::flags::enum_cast requires bool(char, char) invocable predicate.");
  805. using D = std::decay_t<E>;
  806. using U = underlying_type_t<D>;
  807. auto result = U{0};
  808. while (!value.empty()) {
  809. const auto d = detail::find(value, '|');
  810. const auto s = (d == string_view::npos) ? value : value.substr(0, d);
  811. auto f = U{0};
  812. for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
  813. if (detail::cmp_equal(s, detail::names_v<D, true>[i], p)) {
  814. f = static_cast<U>(enum_value<D>(i));
  815. result |= f;
  816. break;
  817. }
  818. }
  819. if (f == U{0}) {
  820. return {}; // Invalid value or out of range.
  821. }
  822. value.remove_prefix((d == string_view::npos) ? value.size() : d + 1);
  823. }
  824. if (result == U{0}) {
  825. return {}; // Invalid value or out of range.
  826. } else {
  827. return static_cast<D>(result);
  828. }
  829. }
  830. // Obtains enum-flags value from name.
  831. // Returns optional with enum-flags value.
  832. template <typename E>
  833. [[nodiscard]] constexpr auto enum_cast(string_view value) noexcept -> detail::enable_if_enum_t<E, optional<std::decay_t<E>>> {
  834. using D = std::decay_t<E>;
  835. return enum_cast<D>(value, detail::char_equal_to{});
  836. }
  837. // Returns integer value from enum value.
  838. using magic_enum::enum_integer;
  839. // Obtains index in enum-flags values from enum-flags value.
  840. // Returns optional with index.
  841. template <typename E>
  842. [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_enum_t<E, optional<std::size_t>> {
  843. using D = std::decay_t<E>;
  844. using U = underlying_type_t<D>;
  845. if (detail::is_pow2(static_cast<U>(value))) {
  846. for (std::size_t i = 0; i < detail::count_v<D, true>; ++i) {
  847. if (enum_value<D>(i) == value) {
  848. return i;
  849. }
  850. }
  851. }
  852. return {}; // Invalid value or out of range.
  853. }
  854. // Checks whether enum-flags contains enumerator with such enum-flags value.
  855. template <typename E>
  856. [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t<E, bool> {
  857. using D = std::decay_t<E>;
  858. using U = underlying_type_t<D>;
  859. return enum_cast<D>(static_cast<U>(value)).has_value();
  860. }
  861. // Checks whether enum-flags contains enumerator with such integer value.
  862. template <typename E>
  863. [[nodiscard]] constexpr auto enum_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_enum_t<E, bool> {
  864. using D = std::decay_t<E>;
  865. return enum_cast<D>(value).has_value();
  866. }
  867. // Checks whether enum-flags contains enumerator with such name.
  868. template <typename E, typename BinaryPredicate>
  869. [[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>) -> detail::enable_if_enum_t<E, bool> {
  870. static_assert(std::is_invocable_r_v<bool, BinaryPredicate, char, char>, "magic_enum::flags::enum_contains requires bool(char, char) invocable predicate.");
  871. using D = std::decay_t<E>;
  872. return enum_cast<D>(value, std::move_if_noexcept(p)).has_value();
  873. }
  874. // Checks whether enum-flags contains enumerator with such name.
  875. template <typename E>
  876. [[nodiscard]] constexpr auto enum_contains(string_view value) noexcept -> detail::enable_if_enum_t<E, bool> {
  877. using D = std::decay_t<E>;
  878. return enum_cast<D>(value).has_value();
  879. }
  880. } // namespace magic_enum::flags
  881. namespace flags::ostream_operators {
  882. template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  883. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, E value) {
  884. using D = std::decay_t<E>;
  885. using U = underlying_type_t<D>;
  886. #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED
  887. if (const auto name = magic_enum::flags::enum_name<D>(value); !name.empty()) {
  888. for (const auto c : name) {
  889. os.put(c);
  890. }
  891. return os;
  892. }
  893. #endif
  894. return (os << static_cast<U>(value));
  895. }
  896. template <typename Char, typename Traits, typename E, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  897. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, optional<E> value) {
  898. return value.has_value() ? (os << value.value()) : os;
  899. }
  900. } // namespace magic_enum::flags::ostream_operators
  901. namespace flags::bitwise_operators {
  902. using namespace magic_enum::bitwise_operators;
  903. } // namespace magic_enum::flags::bitwise_operators
  904. } // namespace magic_enum
  905. #if defined(__clang__)
  906. # pragma clang diagnostic pop
  907. #elif defined(__GNUC__)
  908. # pragma GCC diagnostic pop
  909. #elif defined(_MSC_VER)
  910. # pragma warning(pop)
  911. #endif
  912. #endif // NEARGYE_MAGIC_ENUM_HPP