magic_enum.hpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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_HPP
  32. #define NEARGYE_MAGIC_ENUM_HPP
  33. #define MAGIC_ENUM_VERSION_MAJOR 0
  34. #define MAGIC_ENUM_VERSION_MINOR 8
  35. #define MAGIC_ENUM_VERSION_PATCH 2
  36. #include <array>
  37. #include <cassert>
  38. #include <cstdint>
  39. #include <cstddef>
  40. #include <limits>
  41. #include <type_traits>
  42. #include <utility>
  43. #if defined(MAGIC_ENUM_CONFIG_FILE)
  44. #include MAGIC_ENUM_CONFIG_FILE
  45. #endif
  46. #if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
  47. #include <optional>
  48. #endif
  49. #if !defined(MAGIC_ENUM_USING_ALIAS_STRING)
  50. #include <string>
  51. #endif
  52. #if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
  53. #include <string_view>
  54. #endif
  55. #if !defined(MAGIC_ENUM_NO_STREAMS)
  56. #include <iosfwd>
  57. #endif
  58. #if defined(__clang__)
  59. # pragma clang diagnostic push
  60. # pragma clang diagnostic ignored "-Wunknown-warning-option"
  61. # pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
  62. #elif defined(__GNUC__)
  63. # pragma GCC diagnostic push
  64. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized" // May be used uninitialized 'return {};'.
  65. #elif defined(_MSC_VER)
  66. # pragma warning(push)
  67. # pragma warning(disable : 26495) // Variable 'static_string<N>::chars_' is uninitialized.
  68. # pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value.
  69. # pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call.
  70. # pragma warning(disable : 4514) // Unreferenced inline function has been removed.
  71. #endif
  72. // Checks magic_enum compiler compatibility.
  73. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910
  74. # undef MAGIC_ENUM_SUPPORTED
  75. # define MAGIC_ENUM_SUPPORTED 1
  76. #endif
  77. // Checks magic_enum compiler aliases compatibility.
  78. #if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920
  79. # undef MAGIC_ENUM_SUPPORTED_ALIASES
  80. # define MAGIC_ENUM_SUPPORTED_ALIASES 1
  81. #endif
  82. // Enum value must be greater or equals than MAGIC_ENUM_RANGE_MIN. By default MAGIC_ENUM_RANGE_MIN = -128.
  83. // If need another min range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN.
  84. #if !defined(MAGIC_ENUM_RANGE_MIN)
  85. # define MAGIC_ENUM_RANGE_MIN -128
  86. #endif
  87. // Enum value must be less or equals than MAGIC_ENUM_RANGE_MAX. By default MAGIC_ENUM_RANGE_MAX = 128.
  88. // If need another max range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MAX.
  89. #if !defined(MAGIC_ENUM_RANGE_MAX)
  90. # define MAGIC_ENUM_RANGE_MAX 128
  91. #endif
  92. namespace magic_enum {
  93. // If need another optional type, define the macro MAGIC_ENUM_USING_ALIAS_OPTIONAL.
  94. #if defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
  95. MAGIC_ENUM_USING_ALIAS_OPTIONAL
  96. #else
  97. using std::optional;
  98. #endif
  99. // If need another string_view type, define the macro MAGIC_ENUM_USING_ALIAS_STRING_VIEW.
  100. #if defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
  101. MAGIC_ENUM_USING_ALIAS_STRING_VIEW
  102. #else
  103. using std::string_view;
  104. #endif
  105. // If need another string type, define the macro MAGIC_ENUM_USING_ALIAS_STRING.
  106. #if defined(MAGIC_ENUM_USING_ALIAS_STRING)
  107. MAGIC_ENUM_USING_ALIAS_STRING
  108. #else
  109. using std::string;
  110. #endif
  111. namespace customize {
  112. // 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.
  113. // If need another range for all enum types by default, redefine the macro MAGIC_ENUM_RANGE_MIN and MAGIC_ENUM_RANGE_MAX.
  114. // If need another range for specific enum type, add specialization enum_range for necessary enum type.
  115. template <typename E>
  116. struct enum_range {
  117. static_assert(std::is_enum_v<E>, "magic_enum::customize::enum_range requires enum type.");
  118. static constexpr int min = MAGIC_ENUM_RANGE_MIN;
  119. static constexpr int max = MAGIC_ENUM_RANGE_MAX;
  120. static_assert(max > min, "magic_enum::customize::enum_range requires max > min.");
  121. };
  122. static_assert(MAGIC_ENUM_RANGE_MAX > MAGIC_ENUM_RANGE_MIN, "MAGIC_ENUM_RANGE_MAX must be greater than MAGIC_ENUM_RANGE_MIN.");
  123. static_assert((MAGIC_ENUM_RANGE_MAX - MAGIC_ENUM_RANGE_MIN) < (std::numeric_limits<std::uint16_t>::max)(), "MAGIC_ENUM_RANGE must be less than UINT16_MAX.");
  124. namespace detail {
  125. enum class customize_tag {
  126. default_tag,
  127. invalid_tag,
  128. custom_tag
  129. };
  130. } // namespace magic_enum::customize::detail
  131. class customize_t : public std::pair<detail::customize_tag, string_view> {
  132. public:
  133. constexpr customize_t(string_view srt) : std::pair<detail::customize_tag, string_view>{detail::customize_tag::custom_tag, srt} {}
  134. constexpr customize_t(const char* srt) : customize_t{string_view{srt}} {}
  135. constexpr customize_t(detail::customize_tag tag) : std::pair<detail::customize_tag, string_view>{tag, string_view{}} {
  136. assert(tag != detail::customize_tag::custom_tag);
  137. }
  138. };
  139. // Default customize.
  140. inline constexpr auto default_tag = customize_t{detail::customize_tag::default_tag};
  141. // Invalid customize.
  142. inline constexpr auto invalid_tag = customize_t{detail::customize_tag::invalid_tag};
  143. // If need custom names for enum, add specialization enum_name for necessary enum type.
  144. template <typename E>
  145. constexpr customize_t enum_name(E) noexcept {
  146. return default_tag;
  147. }
  148. // If need custom type name for enum, add specialization enum_type_name for necessary enum type.
  149. template <typename E>
  150. constexpr customize_t enum_type_name() noexcept {
  151. return default_tag;
  152. }
  153. } // namespace magic_enum::customize
  154. namespace detail {
  155. template <auto V, typename E = std::decay_t<decltype(V)>, std::enable_if_t<std::is_enum_v<E>, int> = 0>
  156. using enum_constant = std::integral_constant<E, V>;
  157. enum class value_type {
  158. default_value,
  159. flags_value
  160. };
  161. template <typename... T>
  162. inline constexpr bool always_false_v = false;
  163. template <typename T>
  164. struct supported
  165. #if defined(MAGIC_ENUM_SUPPORTED) && MAGIC_ENUM_SUPPORTED || defined(MAGIC_ENUM_NO_CHECK_SUPPORT)
  166. : std::true_type {};
  167. #else
  168. : std::false_type {};
  169. #endif
  170. template <typename T, typename = void>
  171. struct has_is_flags : std::false_type {};
  172. template <typename T>
  173. struct has_is_flags<T, std::void_t<decltype(customize::enum_range<T>::is_flags)>> : std::bool_constant<std::is_same_v<bool, std::decay_t<decltype(customize::enum_range<T>::is_flags)>>> {};
  174. template <typename T, typename = void>
  175. struct range_min : std::integral_constant<int, MAGIC_ENUM_RANGE_MIN> {};
  176. template <typename T>
  177. struct range_min<T, std::void_t<decltype(customize::enum_range<T>::min)>> : std::integral_constant<decltype(customize::enum_range<T>::min), customize::enum_range<T>::min> {};
  178. template <typename T, typename = void>
  179. struct range_max : std::integral_constant<int, MAGIC_ENUM_RANGE_MAX> {};
  180. template <typename T>
  181. struct range_max<T, std::void_t<decltype(customize::enum_range<T>::max)>> : std::integral_constant<decltype(customize::enum_range<T>::max), customize::enum_range<T>::max> {};
  182. template <std::uint16_t N>
  183. class static_string {
  184. public:
  185. constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_integer_sequence<std::uint16_t, N>{}} {
  186. assert(str.size() == N);
  187. }
  188. constexpr const char* data() const noexcept { return chars_; }
  189. constexpr std::uint16_t size() const noexcept { return N; }
  190. constexpr operator string_view() const noexcept { return {data(), size()}; }
  191. private:
  192. template <std::uint16_t... I>
  193. constexpr static_string(string_view str, std::integer_sequence<std::uint16_t, I...>) noexcept : chars_{str[I]..., '\0'} {}
  194. char chars_[static_cast<std::size_t>(N) + 1];
  195. };
  196. template <>
  197. class static_string<0> {
  198. public:
  199. constexpr explicit static_string() = default;
  200. constexpr explicit static_string(string_view) noexcept {}
  201. constexpr const char* data() const noexcept { return nullptr; }
  202. constexpr std::uint16_t size() const noexcept { return 0; }
  203. constexpr operator string_view() const noexcept { return {}; }
  204. };
  205. constexpr string_view pretty_name(string_view name) noexcept {
  206. for (std::size_t i = name.size(); i > 0; --i) {
  207. if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||
  208. (name[i - 1] >= 'a' && name[i - 1] <= 'z') ||
  209. (name[i - 1] >= 'A' && name[i - 1] <= 'Z') ||
  210. #if defined(MAGIC_ENUM_ENABLE_NONASCII)
  211. (name[i - 1] & 0x80) ||
  212. #endif
  213. (name[i - 1] == '_'))) {
  214. name.remove_prefix(i);
  215. break;
  216. }
  217. }
  218. if (name.size() > 0 && ((name[0] >= 'a' && name[0] <= 'z') ||
  219. (name[0] >= 'A' && name[0] <= 'Z') ||
  220. #if defined(MAGIC_ENUM_ENABLE_NONASCII)
  221. (name[0]) & 0x80) ||
  222. #endif
  223. (name[0] == '_'))) {
  224. return name;
  225. }
  226. return {}; // Invalid name.
  227. }
  228. class case_insensitive {
  229. static constexpr char to_lower(char c) noexcept {
  230. return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + ('a' - 'A')) : c;
  231. }
  232. public:
  233. template <typename L, typename R>
  234. constexpr auto operator()([[maybe_unused]] L lhs, [[maybe_unused]] R rhs) const noexcept -> std::enable_if_t<std::is_same_v<std::decay_t<L>, char> && std::is_same_v<std::decay_t<R>, char>, bool> {
  235. #if defined(MAGIC_ENUM_ENABLE_NONASCII)
  236. static_assert(always_false_v<L, R>, "magic_enum::case_insensitive not supported Non-ASCII feature.");
  237. return false;
  238. #else
  239. return to_lower(lhs) == to_lower(rhs);
  240. #endif
  241. }
  242. };
  243. constexpr std::size_t find(string_view str, char c) noexcept {
  244. #if defined(__clang__) && __clang_major__ < 9 && defined(__GLIBCXX__) || defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
  245. // https://stackoverflow.com/questions/56484834/constexpr-stdstring-viewfind-last-of-doesnt-work-on-clang-8-with-libstdc
  246. // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
  247. constexpr bool workaround = true;
  248. #else
  249. constexpr bool workaround = false;
  250. #endif
  251. if constexpr (workaround) {
  252. for (std::size_t i = 0; i < str.size(); ++i) {
  253. if (str[i] == c) {
  254. return i;
  255. }
  256. }
  257. return string_view::npos;
  258. } else {
  259. return str.find(c);
  260. }
  261. }
  262. template <typename T, std::size_t N, std::size_t... I>
  263. constexpr std::array<std::remove_cv_t<T>, N> to_array(T (&a)[N], std::index_sequence<I...>) noexcept {
  264. return {{a[I]...}};
  265. }
  266. template <typename BinaryPredicate>
  267. constexpr bool is_default_predicate() noexcept {
  268. return std::is_same_v<std::decay_t<BinaryPredicate>, std::equal_to<string_view::value_type>> ||
  269. std::is_same_v<std::decay_t<BinaryPredicate>, std::equal_to<>>;
  270. }
  271. template <typename BinaryPredicate>
  272. constexpr bool is_nothrow_invocable() {
  273. return is_default_predicate<BinaryPredicate>() ||
  274. std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>;
  275. }
  276. template <typename BinaryPredicate>
  277. constexpr bool cmp_equal(string_view lhs, string_view rhs, [[maybe_unused]] BinaryPredicate&& p) noexcept(is_nothrow_invocable<BinaryPredicate>()) {
  278. #if defined(_MSC_VER) && _MSC_VER < 1920 && !defined(__clang__)
  279. // https://developercommunity.visualstudio.com/content/problem/360432/vs20178-regression-c-failed-in-test.html
  280. // https://developercommunity.visualstudio.com/content/problem/232218/c-constexpr-string-view.html
  281. constexpr bool workaround = true;
  282. #else
  283. constexpr bool workaround = false;
  284. #endif
  285. if constexpr (!is_default_predicate<BinaryPredicate>() || workaround) {
  286. if (lhs.size() != rhs.size()) {
  287. return false;
  288. }
  289. const auto size = lhs.size();
  290. for (std::size_t i = 0; i < size; ++i) {
  291. if (!p(lhs[i], rhs[i])) {
  292. return false;
  293. }
  294. }
  295. return true;
  296. } else {
  297. return lhs == rhs;
  298. }
  299. }
  300. template <typename L, typename R>
  301. constexpr bool cmp_less(L lhs, R rhs) noexcept {
  302. static_assert(std::is_integral_v<L> && std::is_integral_v<R>, "magic_enum::detail::cmp_less requires integral type.");
  303. if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
  304. // If same signedness (both signed or both unsigned).
  305. return lhs < rhs;
  306. } else if constexpr (std::is_same_v<L, bool>) { // bool special case
  307. return static_cast<R>(lhs) < rhs;
  308. } else if constexpr (std::is_same_v<R, bool>) { // bool special case
  309. return lhs < static_cast<L>(rhs);
  310. } else if constexpr (std::is_signed_v<R>) {
  311. // If 'right' is negative, then result is 'false', otherwise cast & compare.
  312. return rhs > 0 && lhs < static_cast<std::make_unsigned_t<R>>(rhs);
  313. } else {
  314. // If 'left' is negative, then result is 'true', otherwise cast & compare.
  315. return lhs < 0 || static_cast<std::make_unsigned_t<L>>(lhs) < rhs;
  316. }
  317. }
  318. template <typename I>
  319. constexpr I log2(I value) noexcept {
  320. static_assert(std::is_integral_v<I>, "magic_enum::detail::log2 requires integral type.");
  321. if constexpr (std::is_same_v<I, bool>) { // bool special case
  322. return assert(false), value;
  323. } else {
  324. auto ret = I{0};
  325. for (; value > I{1}; value >>= I{1}, ++ret) {}
  326. return ret;
  327. }
  328. }
  329. template <typename T>
  330. inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::decay_t<T>>;
  331. template <typename E>
  332. constexpr auto n() noexcept {
  333. static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
  334. if constexpr (supported<E>::value) {
  335. #if defined(__clang__) || defined(__GNUC__)
  336. constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
  337. #elif defined(_MSC_VER)
  338. constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
  339. #else
  340. constexpr auto name = string_view{};
  341. #endif
  342. return name;
  343. } else {
  344. return string_view{}; // Unsupported compiler or Invalid customize.
  345. }
  346. }
  347. template <typename E>
  348. constexpr auto type_name() noexcept {
  349. static_assert(is_enum_v<E>, "magic_enum::detail::type_name requires enum type.");
  350. [[maybe_unused]] constexpr auto custom = customize::enum_type_name<E>();
  351. static_assert(std::is_same_v<std::decay_t<decltype(custom)>, customize::customize_t>, "magic_enum::customize requires customize_t type.");
  352. if constexpr (custom.first == customize::detail::customize_tag::custom_tag) {
  353. constexpr auto name = custom.second;
  354. static_assert(!name.empty(), "magic_enum::customize requires not empty string.");
  355. return static_string<name.size()>{name};
  356. } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) {
  357. return static_string<0>{};
  358. } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) {
  359. constexpr auto name = n<E>();
  360. return static_string<name.size()>{name};
  361. } else {
  362. static_assert(detail::always_false_v<E>, "magic_enum::customize invalid.");
  363. }
  364. }
  365. template <typename E>
  366. inline constexpr auto type_name_v = type_name<E>();
  367. template <typename E, E V>
  368. constexpr auto n() noexcept {
  369. static_assert(is_enum_v<E>, "magic_enum::detail::n requires enum type.");
  370. if constexpr (supported<E>::value) {
  371. #if defined(__clang__) || defined(__GNUC__)
  372. constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
  373. #elif defined(_MSC_VER)
  374. constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
  375. #else
  376. constexpr auto name = string_view{};
  377. #endif
  378. return name;
  379. } else {
  380. return string_view{}; // Unsupported compiler or Invalid customize.
  381. }
  382. }
  383. template <typename E, E V>
  384. constexpr auto enum_name() noexcept {
  385. static_assert(is_enum_v<E>, "magic_enum::detail::enum_name requires enum type.");
  386. [[maybe_unused]] constexpr auto custom = customize::enum_name<E>(V);
  387. static_assert(std::is_same_v<std::decay_t<decltype(custom)>, customize::customize_t>, "magic_enum::customize requires customize_t type.");
  388. if constexpr (custom.first == customize::detail::customize_tag::custom_tag) {
  389. constexpr auto name = custom.second;
  390. static_assert(!name.empty(), "magic_enum::customize requires not empty string.");
  391. return static_string<name.size()>{name};
  392. } else if constexpr (custom.first == customize::detail::customize_tag::invalid_tag) {
  393. return static_string<0>{};
  394. } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) {
  395. constexpr auto name = n<E, V>();
  396. return static_string<name.size()>{name};
  397. } else {
  398. static_assert(detail::always_false_v<E>, "magic_enum::customize invalid.");
  399. }
  400. }
  401. template <typename E, E V>
  402. inline constexpr auto enum_name_v = enum_name<E, V>();
  403. template <typename E, auto V>
  404. constexpr bool is_valid() noexcept {
  405. static_assert(is_enum_v<E>, "magic_enum::detail::is_valid requires enum type.");
  406. #if defined(__clang__) && __clang_major__ >= 16
  407. // https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
  408. constexpr E v = __builtin_bit_cast(E, V);
  409. #else
  410. constexpr E v = static_cast<E>(V);
  411. #endif
  412. [[maybe_unused]] constexpr auto custom = customize::enum_name<E>(v);
  413. static_assert(std::is_same_v<std::decay_t<decltype(custom)>, customize::customize_t>, "magic_enum::customize requires customize_t type.");
  414. if constexpr (custom.first == customize::detail::customize_tag::custom_tag) {
  415. constexpr auto name = custom.second;
  416. static_assert(!name.empty(), "magic_enum::customize requires not empty string.");
  417. return name.size() != 0;
  418. } else if constexpr (custom.first == customize::detail::customize_tag::default_tag) {
  419. return n<E, v>().size() != 0;
  420. } else {
  421. return false;
  422. }
  423. }
  424. template <typename E, int O, bool IsFlags, typename U = std::underlying_type_t<E>>
  425. constexpr U ualue(std::size_t i) noexcept {
  426. static_assert(is_enum_v<E>, "magic_enum::detail::ualue requires enum type.");
  427. if constexpr (std::is_same_v<U, bool>) { // bool special case
  428. static_assert(O == 0, "magic_enum::detail::ualue requires valid offset.");
  429. return static_cast<U>(i);
  430. } else if constexpr (IsFlags) {
  431. return static_cast<U>(U{1} << static_cast<U>(static_cast<int>(i) + O));
  432. } else {
  433. return static_cast<U>(static_cast<int>(i) + O);
  434. }
  435. }
  436. template <typename E, int O, bool IsFlags, typename U = std::underlying_type_t<E>>
  437. constexpr E value(std::size_t i) noexcept {
  438. static_assert(is_enum_v<E>, "magic_enum::detail::value requires enum type.");
  439. return static_cast<E>(ualue<E, O, IsFlags>(i));
  440. }
  441. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  442. constexpr int reflected_min() noexcept {
  443. static_assert(is_enum_v<E>, "magic_enum::detail::reflected_min requires enum type.");
  444. if constexpr (IsFlags) {
  445. return 0;
  446. } else {
  447. constexpr auto lhs = range_min<E>::value;
  448. constexpr auto rhs = (std::numeric_limits<U>::min)();
  449. if constexpr (cmp_less(rhs, lhs)) {
  450. return lhs;
  451. } else {
  452. return rhs;
  453. }
  454. }
  455. }
  456. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  457. constexpr int reflected_max() noexcept {
  458. static_assert(is_enum_v<E>, "magic_enum::detail::reflected_max requires enum type.");
  459. if constexpr (IsFlags) {
  460. return std::numeric_limits<U>::digits - 1;
  461. } else {
  462. constexpr auto lhs = range_max<E>::value;
  463. constexpr auto rhs = (std::numeric_limits<U>::max)();
  464. if constexpr (cmp_less(lhs, rhs)) {
  465. return lhs;
  466. } else {
  467. return rhs;
  468. }
  469. }
  470. }
  471. template <typename E, bool IsFlags>
  472. inline constexpr auto reflected_min_v = reflected_min<E, IsFlags>();
  473. template <typename E, bool IsFlags>
  474. inline constexpr auto reflected_max_v = reflected_max<E, IsFlags>();
  475. template <std::size_t N>
  476. constexpr std::size_t values_count(const bool (&valid)[N]) noexcept {
  477. auto count = std::size_t{0};
  478. for (std::size_t i = 0; i < N; ++i) {
  479. if (valid[i]) {
  480. ++count;
  481. }
  482. }
  483. return count;
  484. }
  485. template <typename E, bool IsFlags, int Min, std::size_t... I>
  486. constexpr auto values(std::index_sequence<I...>) noexcept {
  487. static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
  488. constexpr bool valid[sizeof...(I)] = {is_valid<E, ualue<E, Min, IsFlags>(I)>()...};
  489. constexpr std::size_t count = values_count(valid);
  490. if constexpr (count > 0) {
  491. E values[count] = {};
  492. for (std::size_t i = 0, v = 0; v < count; ++i) {
  493. if (valid[i]) {
  494. values[v++] = value<E, Min, IsFlags>(i);
  495. }
  496. }
  497. return to_array(values, std::make_index_sequence<count>{});
  498. } else {
  499. return std::array<E, 0>{};
  500. }
  501. }
  502. template <typename E, bool IsFlags, typename U = std::underlying_type_t<E>>
  503. constexpr auto values() noexcept {
  504. static_assert(is_enum_v<E>, "magic_enum::detail::values requires enum type.");
  505. constexpr auto min = reflected_min_v<E, IsFlags>;
  506. constexpr auto max = reflected_max_v<E, IsFlags>;
  507. constexpr auto range_size = max - min + 1;
  508. static_assert(range_size > 0, "magic_enum::enum_range requires valid size.");
  509. static_assert(range_size < (std::numeric_limits<std::uint16_t>::max)(), "magic_enum::enum_range requires valid size.");
  510. return values<E, IsFlags, min>(std::make_index_sequence<range_size>{});
  511. }
  512. template <typename E, typename U = std::underlying_type_t<E>>
  513. constexpr bool is_flags_enum() noexcept {
  514. static_assert(is_enum_v<E>, "magic_enum::detail::is_flags_enum requires enum type.");
  515. if constexpr (has_is_flags<E>::value) {
  516. return customize::enum_range<E>::is_flags;
  517. } else if constexpr (std::is_same_v<U, bool>) { // bool special case
  518. return false;
  519. } else {
  520. #if defined(MAGIC_ENUM_NO_CHECK_FLAGS)
  521. return false;
  522. #else
  523. constexpr auto flags_values = values<E, true>();
  524. constexpr auto default_values = values<E, false>();
  525. if (flags_values.size() == 0 || default_values.size() > flags_values.size()) {
  526. return false;
  527. }
  528. for (std::size_t i = 0; i < default_values.size(); ++i) {
  529. const auto v = static_cast<U>(default_values[i]);
  530. if (v != 0 && (v & (v - 1)) != 0) {
  531. return false;
  532. }
  533. }
  534. return flags_values.size() > 0;
  535. #endif
  536. }
  537. }
  538. template <typename E>
  539. inline constexpr bool is_flags_v = is_flags_enum<E>();
  540. template <typename E>
  541. inline constexpr std::array values_v = values<E, is_flags_v<E>>();
  542. template <typename E, typename D = std::decay_t<E>>
  543. using values_t = decltype((values_v<D>));
  544. template <typename E>
  545. inline constexpr auto count_v = values_v<E>.size();
  546. template <typename E, typename U = std::underlying_type_t<E>>
  547. inline constexpr auto min_v = (count_v<E> > 0) ? static_cast<U>(values_v<E>.front()) : U{0};
  548. template <typename E, typename U = std::underlying_type_t<E>>
  549. inline constexpr auto max_v = (count_v<E> > 0) ? static_cast<U>(values_v<E>.back()) : U{0};
  550. template <typename E, std::size_t... I>
  551. constexpr auto names(std::index_sequence<I...>) noexcept {
  552. static_assert(is_enum_v<E>, "magic_enum::detail::names requires enum type.");
  553. return std::array<string_view, sizeof...(I)>{{enum_name_v<E, values_v<E>[I]>...}};
  554. }
  555. template <typename E>
  556. inline constexpr std::array names_v = names<E>(std::make_index_sequence<count_v<E>>{});
  557. template <typename E, typename D = std::decay_t<E>>
  558. using names_t = decltype((names_v<D>));
  559. template <typename E, std::size_t... I>
  560. constexpr auto entries(std::index_sequence<I...>) noexcept {
  561. static_assert(is_enum_v<E>, "magic_enum::detail::entries requires enum type.");
  562. return std::array<std::pair<E, string_view>, sizeof...(I)>{{{values_v<E>[I], enum_name_v<E, values_v<E>[I]>}...}};
  563. }
  564. template <typename E>
  565. inline constexpr std::array entries_v = entries<E>(std::make_index_sequence<count_v<E>>{});
  566. template <typename E, typename D = std::decay_t<E>>
  567. using entries_t = decltype((entries_v<D>));
  568. template <typename E, typename U = std::underlying_type_t<E>>
  569. constexpr bool is_sparse() noexcept {
  570. static_assert(is_enum_v<E>, "magic_enum::detail::is_sparse requires enum type.");
  571. if constexpr (count_v<E> == 0) {
  572. return false;
  573. } else if constexpr (std::is_same_v<U, bool>) { // bool special case
  574. return false;
  575. } else {
  576. constexpr auto max = is_flags_v<E> ? log2(max_v<E>) : max_v<E>;
  577. constexpr auto min = is_flags_v<E> ? log2(min_v<E>) : min_v<E>;
  578. constexpr auto range_size = max - min + 1;
  579. return range_size != count_v<E>;
  580. }
  581. }
  582. template <typename E>
  583. inline constexpr bool is_sparse_v = is_sparse<E>();
  584. template <typename E, typename U = std::underlying_type_t<E>>
  585. constexpr U values_ors() noexcept {
  586. static_assert(is_enum_v<E>, "magic_enum::detail::values_ors requires enum type.");
  587. auto ors = U{0};
  588. for (std::size_t i = 0; i < count_v<E>; ++i) {
  589. ors |= static_cast<U>(values_v<E>[i]);
  590. }
  591. return ors;
  592. }
  593. template <bool, typename R>
  594. struct enable_if_enum {};
  595. template <typename R>
  596. struct enable_if_enum<true, R> {
  597. using type = R;
  598. static_assert(supported<R>::value, "magic_enum unsupported compiler (https://github.com/Neargye/magic_enum#compiler-compatibility).");
  599. };
  600. template <typename T, typename R, typename BinaryPredicate = std::equal_to<>, typename D = std::decay_t<T>>
  601. using enable_if_t = typename enable_if_enum<std::is_enum_v<D> && std::is_invocable_r_v<bool, BinaryPredicate, char, char>, R>::type;
  602. template <typename T, typename R, value_type VT, typename BinaryPredicate = std::equal_to<>, typename D = std::decay_t<T>>
  603. using enable_if_default_t = typename enable_if_enum<std::is_enum_v<D> && VT == value_type::default_value && std::is_invocable_r_v<bool, BinaryPredicate, char, char>, R>::type;
  604. template <typename T, typename R, value_type VT, typename BinaryPredicate = std::equal_to<>, typename D = std::decay_t<T>>
  605. using enable_if_flags_t = typename enable_if_enum<std::is_enum_v<D> && VT == value_type::flags_value && std::is_invocable_r_v<bool, BinaryPredicate, char, char>, R>::type;
  606. template <typename T, std::enable_if_t<std::is_enum_v<std::decay_t<T>>, int> = 0>
  607. using enum_concept = T;
  608. template <typename T, bool = std::is_enum_v<T>>
  609. struct is_scoped_enum : std::false_type {};
  610. template <typename T>
  611. struct is_scoped_enum<T, true> : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
  612. template <typename T, bool = std::is_enum_v<T>>
  613. struct is_unscoped_enum : std::false_type {};
  614. template <typename T>
  615. struct is_unscoped_enum<T, true> : std::bool_constant<std::is_convertible_v<T, std::underlying_type_t<T>>> {};
  616. template <typename T, bool = std::is_enum_v<std::decay_t<T>>>
  617. struct underlying_type {};
  618. template <typename T>
  619. struct underlying_type<T, true> : std::underlying_type<std::decay_t<T>> {};
  620. #if defined(MAGIC_ENUM_ENABLE_HASH)
  621. template <typename T>
  622. inline constexpr bool has_hash = true;
  623. template <typename Value, typename = void>
  624. struct constexpr_hash_t;
  625. template <typename Value>
  626. struct constexpr_hash_t<Value, std::enable_if_t<is_enum_v<Value>>> {
  627. constexpr auto operator()(Value value) const noexcept {
  628. using U = typename underlying_type<Value>::type;
  629. if constexpr (std::is_same_v<U, bool>) { // bool special case
  630. return static_cast<std::size_t>(value);
  631. } else {
  632. return static_cast<U>(value);
  633. }
  634. }
  635. using secondary_hash = constexpr_hash_t;
  636. };
  637. template <typename Value>
  638. struct constexpr_hash_t<Value, std::enable_if_t<std::is_same_v<Value, string_view>>> {
  639. static constexpr std::uint32_t crc_table[256] {
  640. 0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L, 0x706af48fL, 0xe963a535L, 0x9e6495a3L,
  641. 0x0edb8832L, 0x79dcb8a4L, 0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L, 0x90bf1d91L,
  642. 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL, 0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L,
  643. 0x136c9856L, 0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L, 0xfa0f3d63L, 0x8d080df5L,
  644. 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L, 0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
  645. 0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L, 0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L,
  646. 0x26d930acL, 0x51de003aL, 0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L, 0xb8bda50fL,
  647. 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L, 0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL,
  648. 0x76dc4190L, 0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL, 0x9fbfe4a5L, 0xe8b8d433L,
  649. 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL, 0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
  650. 0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL, 0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L,
  651. 0x65b0d9c6L, 0x12b7e950L, 0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L, 0xfbd44c65L,
  652. 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L, 0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL,
  653. 0x4369e96aL, 0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L, 0xaa0a4c5fL, 0xdd0d7cc9L,
  654. 0x5005713cL, 0x270241aaL, 0xbe0b1010L, 0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
  655. 0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L, 0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL,
  656. 0xedb88320L, 0x9abfb3b6L, 0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L, 0x73dc1683L,
  657. 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L, 0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L,
  658. 0xf00f9344L, 0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL, 0x196c3671L, 0x6e6b06e7L,
  659. 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL, 0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
  660. 0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L, 0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL,
  661. 0xd80d2bdaL, 0xaf0a1b4cL, 0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL, 0x4669be79L,
  662. 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L, 0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL,
  663. 0xc5ba3bbeL, 0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L, 0x2cd99e8bL, 0x5bdeae1dL,
  664. 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL, 0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
  665. 0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL, 0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L,
  666. 0x86d3d2d4L, 0xf1d4e242L, 0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L, 0x18b74777L,
  667. 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL, 0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L,
  668. 0xa00ae278L, 0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L, 0x4969474dL, 0x3e6e77dbL,
  669. 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L, 0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
  670. 0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L, 0xcdd70693L, 0x54de5729L, 0x23d967bfL,
  671. 0xb3667a2eL, 0xc4614ab8L, 0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL, 0x2d02ef8dL
  672. };
  673. constexpr std::uint32_t operator()(string_view value) const noexcept {
  674. auto crc = static_cast<std::uint32_t>(0xffffffffL);
  675. for (const auto c : value) {
  676. crc = (crc >> 8) ^ crc_table[(crc ^ static_cast<std::uint32_t>(c)) & 0xff];
  677. }
  678. return crc ^ 0xffffffffL;
  679. }
  680. struct secondary_hash {
  681. constexpr std::uint32_t operator()(string_view value) const noexcept {
  682. auto acc = static_cast<std::uint64_t>(2166136261ULL);
  683. for (const auto c : value) {
  684. acc = ((acc ^ static_cast<std::uint64_t>(c)) * static_cast<std::uint64_t>(16777619ULL)) & (std::numeric_limits<std::uint32_t>::max)();
  685. }
  686. return static_cast<std::uint32_t>(acc);
  687. }
  688. };
  689. };
  690. template <typename Hash>
  691. inline constexpr Hash hash_v{};
  692. template <auto* GlobValues, typename Hash>
  693. constexpr auto calculate_cases(std::size_t Page) noexcept {
  694. constexpr std::array values = *GlobValues;
  695. constexpr std::size_t size = values.size();
  696. using switch_t = std::invoke_result_t<Hash, typename decltype(values)::value_type>;
  697. static_assert(std::is_integral_v<switch_t> && !std::is_same_v<switch_t, bool>);
  698. const std::size_t values_to = (std::min)(static_cast<std::size_t>(256), size - Page);
  699. std::array<switch_t, 256> result{};
  700. auto fill = result.begin();
  701. {
  702. auto first = values.begin() + static_cast<std::ptrdiff_t>(Page);
  703. auto last = values.begin() + static_cast<std::ptrdiff_t>(Page + values_to);
  704. while (first != last) {
  705. *fill++ = hash_v<Hash>(*first++);
  706. }
  707. }
  708. // dead cases, try to avoid case collisions
  709. for (switch_t last_value = result[values_to - 1]; fill != result.end() && last_value != (std::numeric_limits<switch_t>::max)(); *fill++ = ++last_value) {
  710. }
  711. {
  712. auto it = result.begin();
  713. auto last_value = (std::numeric_limits<switch_t>::min)();
  714. for (; fill != result.end(); *fill++ = last_value++) {
  715. while (last_value == *it) {
  716. ++last_value, ++it;
  717. }
  718. }
  719. }
  720. return result;
  721. }
  722. template <typename R, typename F, typename... Args>
  723. constexpr R invoke_r(F&& f, Args&&... args) noexcept(std::is_nothrow_invocable_r_v<R, F, Args...>) {
  724. if constexpr (std::is_void_v<R>) {
  725. std::forward<F>(f)(std::forward<Args>(args)...);
  726. } else {
  727. return static_cast<R>(std::forward<F>(f)(std::forward<Args>(args)...));
  728. }
  729. }
  730. enum class case_call_t {
  731. index,
  732. value
  733. };
  734. template <typename T = void>
  735. inline constexpr auto default_result_type_lambda = []() noexcept(std::is_nothrow_default_constructible_v<T>) { return T{}; };
  736. template <>
  737. inline constexpr auto default_result_type_lambda<void> = []() noexcept {};
  738. template <auto* Arr, typename Hash>
  739. constexpr bool has_duplicate() noexcept {
  740. using value_t = std::decay_t<decltype((*Arr)[0])>;
  741. using hash_value_t = std::invoke_result_t<Hash, value_t>;
  742. std::array<hash_value_t, Arr->size()> hashes{};
  743. std::size_t size = 0;
  744. for (auto elem : *Arr) {
  745. hashes[size] = hash_v<Hash>(elem);
  746. for (auto i = size++; i > 0; --i) {
  747. if (hashes[i] < hashes[i - 1]) {
  748. auto tmp = hashes[i];
  749. hashes[i] = hashes[i - 1];
  750. hashes[i - 1] = tmp;
  751. } else if (hashes[i] == hashes[i - 1]) {
  752. return false;
  753. } else {
  754. break;
  755. }
  756. }
  757. }
  758. return true;
  759. }
  760. #define MAGIC_ENUM_FOR_EACH_256(T) T(0)T(1)T(2)T(3)T(4)T(5)T(6)T(7)T(8)T(9)T(10)T(11)T(12)T(13)T(14)T(15)T(16)T(17)T(18)T(19)T(20)T(21)T(22)T(23)T(24)T(25)T(26)T(27)T(28)T(29)T(30)T(31) \
  761. T(32)T(33)T(34)T(35)T(36)T(37)T(38)T(39)T(40)T(41)T(42)T(43)T(44)T(45)T(46)T(47)T(48)T(49)T(50)T(51)T(52)T(53)T(54)T(55)T(56)T(57)T(58)T(59)T(60)T(61)T(62)T(63) \
  762. T(64)T(65)T(66)T(67)T(68)T(69)T(70)T(71)T(72)T(73)T(74)T(75)T(76)T(77)T(78)T(79)T(80)T(81)T(82)T(83)T(84)T(85)T(86)T(87)T(88)T(89)T(90)T(91)T(92)T(93)T(94)T(95) \
  763. T(96)T(97)T(98)T(99)T(100)T(101)T(102)T(103)T(104)T(105)T(106)T(107)T(108)T(109)T(110)T(111)T(112)T(113)T(114)T(115)T(116)T(117)T(118)T(119)T(120)T(121)T(122)T(123)T(124)T(125)T(126)T(127) \
  764. T(128)T(129)T(130)T(131)T(132)T(133)T(134)T(135)T(136)T(137)T(138)T(139)T(140)T(141)T(142)T(143)T(144)T(145)T(146)T(147)T(148)T(149)T(150)T(151)T(152)T(153)T(154)T(155)T(156)T(157)T(158)T(159) \
  765. T(160)T(161)T(162)T(163)T(164)T(165)T(166)T(167)T(168)T(169)T(170)T(171)T(172)T(173)T(174)T(175)T(176)T(177)T(178)T(179)T(180)T(181)T(182)T(183)T(184)T(185)T(186)T(187)T(188)T(189)T(190)T(191) \
  766. T(192)T(193)T(194)T(195)T(196)T(197)T(198)T(199)T(200)T(201)T(202)T(203)T(204)T(205)T(206)T(207)T(208)T(209)T(210)T(211)T(212)T(213)T(214)T(215)T(216)T(217)T(218)T(219)T(220)T(221)T(222)T(223) \
  767. T(224)T(225)T(226)T(227)T(228)T(229)T(230)T(231)T(232)T(233)T(234)T(235)T(236)T(237)T(238)T(239)T(240)T(241)T(242)T(243)T(244)T(245)T(246)T(247)T(248)T(249)T(250)T(251)T(252)T(253)T(254)T(255)
  768. #define MAGIC_ENUM_CASE(val) \
  769. case cases[val]: \
  770. if constexpr ((val) + Page < size) { \
  771. if (!pred(values[val + Page], searched)) { \
  772. break; \
  773. } \
  774. if constexpr (CallValue == case_call_t::index) { \
  775. if constexpr (std::is_invocable_r_v<result_t, Lambda, std::integral_constant<std::size_t, val + Page>>) { \
  776. return detail::invoke_r<result_t>(std::forward<Lambda>(lambda), std::integral_constant<std::size_t, val + Page>{}); \
  777. } else if constexpr (std::is_invocable_v<Lambda, std::integral_constant<std::size_t, val + Page>>) { \
  778. assert(false && "magic_enum::detail::constexpr_switch wrong result type."); \
  779. } \
  780. } else if constexpr (CallValue == case_call_t::value) { \
  781. if constexpr (std::is_invocable_r_v<result_t, Lambda, enum_constant<values[val + Page]>>) { \
  782. return detail::invoke_r<result_t>(std::forward<Lambda>(lambda), enum_constant<values[val + Page]>{}); \
  783. } else if constexpr (std::is_invocable_r_v<result_t, Lambda, enum_constant<values[val + Page]>>) { \
  784. assert(false && "magic_enum::detail::constexpr_switch wrong result type."); \
  785. } \
  786. } \
  787. break; \
  788. } else [[fallthrough]];
  789. template <auto* GlobValues,
  790. case_call_t CallValue,
  791. std::size_t Page = 0,
  792. typename Hash = constexpr_hash_t<typename std::decay_t<decltype(*GlobValues)>::value_type>,
  793. typename BinaryPredicate = std::equal_to<>,
  794. typename Lambda,
  795. typename ResultGetterType>
  796. constexpr decltype(auto) constexpr_switch(
  797. Lambda&& lambda,
  798. typename std::decay_t<decltype(*GlobValues)>::value_type searched,
  799. ResultGetterType&& def,
  800. BinaryPredicate&& pred = {}) {
  801. using result_t = std::invoke_result_t<ResultGetterType>;
  802. using hash_t = std::conditional_t<has_duplicate<GlobValues, Hash>(), Hash, typename Hash::secondary_hash>;
  803. static_assert(has_duplicate<GlobValues, hash_t>(), "magic_enum::detail::constexpr_switch duplicated hash found, please report it: https://github.com/Neargye/magic_enum/issues.");
  804. constexpr std::array values = *GlobValues;
  805. constexpr std::size_t size = values.size();
  806. constexpr std::array cases = calculate_cases<GlobValues, hash_t>(Page);
  807. switch (hash_v<hash_t>(searched)) {
  808. MAGIC_ENUM_FOR_EACH_256(MAGIC_ENUM_CASE)
  809. default:
  810. if constexpr (size > 256 + Page) {
  811. return constexpr_switch<GlobValues, CallValue, Page + 256, Hash>(std::forward<Lambda>(lambda), searched, std::forward<ResultGetterType>(def));
  812. }
  813. break;
  814. }
  815. return def();
  816. }
  817. #undef MAGIC_ENUM_FOR_EACH_256
  818. #undef MAGIC_ENUM_CASE
  819. #else
  820. template <typename T>
  821. inline constexpr bool has_hash = false;
  822. #endif
  823. template <typename E, typename F, std::size_t... I>
  824. constexpr auto for_each(F&& f, std::index_sequence<I...>) {
  825. static_assert(is_enum_v<E>, "magic_enum::detail::for_each requires enum type.");
  826. constexpr bool has_void_return = (std::is_void_v<std::invoke_result_t<F, enum_constant<values_v<E>[I]>>> || ...);
  827. constexpr bool all_same_return = (std::is_same_v<std::invoke_result_t<F, enum_constant<values_v<E>[0]>>, std::invoke_result_t<F, enum_constant<values_v<E>[I]>>> && ...);
  828. if constexpr (has_void_return) {
  829. (f(enum_constant<values_v<E>[I]>{}), ...);
  830. } else if constexpr (all_same_return) {
  831. return std::array{f(enum_constant<values_v<E>[I]>{})...};
  832. } else {
  833. return std::tuple{f(enum_constant<values_v<E>[I]>{})...};
  834. }
  835. }
  836. template <typename E, typename F, std::size_t... I>
  837. constexpr bool all_invocable(std::index_sequence<I...>) {
  838. static_assert(is_enum_v<E>, "magic_enum::detail::all_invocable requires enum type.");
  839. if constexpr (count_v<E> == 0) {
  840. return false;
  841. } else {
  842. return (std::is_invocable_v<F, enum_constant<values_v<E>[I]>> && ...);
  843. }
  844. }
  845. } // namespace magic_enum::detail
  846. // Checks is magic_enum supported compiler.
  847. inline constexpr bool is_magic_enum_supported = detail::supported<void>::value;
  848. template <typename T>
  849. using Enum = detail::enum_concept<T>;
  850. // Checks whether T is an Unscoped enumeration type.
  851. // 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.
  852. template <typename T>
  853. struct is_unscoped_enum : detail::is_unscoped_enum<T> {};
  854. template <typename T>
  855. inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
  856. // Checks whether T is an Scoped enumeration type.
  857. // 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.
  858. template <typename T>
  859. struct is_scoped_enum : detail::is_scoped_enum<T> {};
  860. template <typename T>
  861. inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
  862. // If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.
  863. // 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.
  864. template <typename T>
  865. struct underlying_type : detail::underlying_type<T> {};
  866. template <typename T>
  867. using underlying_type_t = typename underlying_type<T>::type;
  868. template <auto V>
  869. using enum_constant = detail::enum_constant<V>;
  870. // Returns type name of enum.
  871. template <typename E>
  872. [[nodiscard]] constexpr auto enum_type_name() noexcept -> detail::enable_if_t<E, string_view> {
  873. constexpr string_view name = detail::type_name_v<std::decay_t<E>>;
  874. static_assert(!name.empty(), "magic_enum::enum_type_name enum type does not have a name.");
  875. return name;
  876. }
  877. // Returns number of enum values.
  878. template <typename E>
  879. [[nodiscard]] constexpr auto enum_count() noexcept -> detail::enable_if_t<E, std::size_t> {
  880. return detail::count_v<std::decay_t<E>>;
  881. }
  882. // Returns enum value at specified index.
  883. // No bounds checking is performed: the behavior is undefined if index >= number of enum values.
  884. template <typename E>
  885. [[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept -> detail::enable_if_t<E, std::decay_t<E>> {
  886. using D = std::decay_t<E>;
  887. if constexpr (detail::is_sparse_v<D>) {
  888. return assert((index < detail::count_v<D>)), detail::values_v<D>[index];
  889. } else {
  890. constexpr bool is_flag = detail::is_flags_v<D>;
  891. constexpr auto min = is_flag ? detail::log2(detail::min_v<D>) : detail::min_v<D>;
  892. return assert((index < detail::count_v<D>)), detail::value<D, min, is_flag>(index);
  893. }
  894. }
  895. // Returns enum value at specified index.
  896. template <typename E, std::size_t I>
  897. [[nodiscard]] constexpr auto enum_value() noexcept -> detail::enable_if_t<E, std::decay_t<E>> {
  898. using D = std::decay_t<E>;
  899. static_assert(I < detail::count_v<D>, "magic_enum::enum_value out of range.");
  900. return enum_value<D>(I);
  901. }
  902. // Returns std::array with enum values, sorted by enum value.
  903. template <typename E>
  904. [[nodiscard]] constexpr auto enum_values() noexcept -> detail::enable_if_t<E, detail::values_t<E>> {
  905. return detail::values_v<std::decay_t<E>>;
  906. }
  907. // Returns integer value from enum value.
  908. template <typename E>
  909. [[nodiscard]] constexpr auto enum_integer(E value) noexcept -> detail::enable_if_t<E, underlying_type_t<E>> {
  910. return static_cast<underlying_type_t<E>>(value);
  911. }
  912. // Returns underlying value from enum value.
  913. template <typename E>
  914. [[nodiscard]] constexpr auto enum_underlying(E value) noexcept -> detail::enable_if_t<E, underlying_type_t<E>> {
  915. return static_cast<underlying_type_t<E>>(value);
  916. }
  917. // Obtains index in enum values from enum value.
  918. // Returns optional with index.
  919. template <typename E>
  920. [[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t<E, optional<std::size_t>> {
  921. using D = std::decay_t<E>;
  922. using U = underlying_type_t<D>;
  923. if constexpr (detail::count_v<D> == 0) {
  924. return {}; // Empty enum.
  925. } else if constexpr (detail::is_sparse_v<D> || detail::is_flags_v<D>) {
  926. #if defined(MAGIC_ENUM_ENABLE_HASH)
  927. return detail::constexpr_switch<&detail::values_v<D>, detail::case_call_t::index>(
  928. [](std::size_t i) { return optional<std::size_t>{i}; },
  929. value,
  930. detail::default_result_type_lambda<optional<std::size_t>>);
  931. #else
  932. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  933. if (enum_value<D>(i) == value) {
  934. return i;
  935. }
  936. }
  937. return {}; // Invalid value or out of range.
  938. #endif
  939. } else {
  940. const auto v = static_cast<U>(value);
  941. if (v >= detail::min_v<D> && v <= detail::max_v<D>) {
  942. return static_cast<std::size_t>(v - detail::min_v<D>);
  943. }
  944. return {}; // Invalid value or out of range.
  945. }
  946. }
  947. // Obtains index in enum values from static storage enum variable.
  948. template <auto V>
  949. [[nodiscard]] constexpr auto enum_index() noexcept -> detail::enable_if_t<decltype(V), std::size_t> {
  950. constexpr auto index = enum_index<std::decay_t<decltype(V)>>(V);
  951. static_assert(index, "magic_enum::enum_index enum value does not have a index.");
  952. return *index;
  953. }
  954. // Returns name from static storage enum variable.
  955. // This version is much lighter on the compile times and is not restricted to the enum_range limitation.
  956. template <auto V>
  957. [[nodiscard]] constexpr auto enum_name() noexcept -> detail::enable_if_t<decltype(V), string_view> {
  958. constexpr string_view name = detail::enum_name_v<std::decay_t<decltype(V)>, V>;
  959. static_assert(!name.empty(), "magic_enum::enum_name enum value does not have a name.");
  960. return name;
  961. }
  962. // Returns name from enum value.
  963. // If enum value does not have name or value out of range, returns empty string.
  964. template <typename E, detail::value_type VT = detail::value_type::default_value>
  965. [[nodiscard]] constexpr auto enum_name(E value) noexcept -> detail::enable_if_default_t<E, string_view, VT> {
  966. using D = std::decay_t<E>;
  967. if (const auto i = enum_index<D>(value)) {
  968. return detail::names_v<D>[*i];
  969. }
  970. return {};
  971. }
  972. // Returns name from enum value.
  973. // If enum value does not have name or value out of range, returns empty string.
  974. template <detail::value_type VT, typename E>
  975. [[nodiscard]] constexpr auto enum_name(E value) -> detail::enable_if_default_t<E, string_view, VT> {
  976. using D = std::decay_t<E>;
  977. return enum_name<D, VT>(value);
  978. }
  979. // Returns name from enum value.
  980. // If enum value does not have name or value out of range, returns empty string.
  981. template <typename E, detail::value_type VT>
  982. [[nodiscard]] auto enum_name(E value) -> detail::enable_if_flags_t<E, string, VT> {
  983. using D = std::decay_t<E>;
  984. using U = underlying_type_t<D>;
  985. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_name requires enum-flags type.");
  986. string name;
  987. auto check_value = U{0};
  988. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  989. if (const auto v = static_cast<U>(enum_value<D>(i)); (static_cast<U>(value) & v) != 0) {
  990. check_value |= v;
  991. const auto n = detail::names_v<D>[i];
  992. if (!name.empty()) {
  993. name.append(1, '|');
  994. }
  995. name.append(n.data(), n.size());
  996. }
  997. }
  998. if (check_value != 0 && check_value == static_cast<U>(value)) {
  999. return name;
  1000. }
  1001. return {}; // Invalid value or out of range.
  1002. }
  1003. // Returns name from enum value.
  1004. // If enum value does not have name or value out of range, returns empty string.
  1005. template <detail::value_type VT, typename E>
  1006. [[nodiscard]] auto enum_name(E value) -> detail::enable_if_flags_t<E, string, VT> {
  1007. using D = std::decay_t<E>;
  1008. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_name requires enum-flags type.");
  1009. return enum_name<D, VT>(value);
  1010. }
  1011. // Returns name from enum-flags value.
  1012. // If enum-flags value does not have name or value out of range, returns empty string.
  1013. template <typename E>
  1014. [[nodiscard]] auto enum_flags_name(E value) -> detail::enable_if_t<E, string> {
  1015. using D = std::decay_t<E>;
  1016. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_name requires enum-flags type.");
  1017. return enum_name<D, detail::value_type::flags_value>(value);
  1018. }
  1019. // Returns std::array with names, sorted by enum value.
  1020. template <typename E>
  1021. [[nodiscard]] constexpr auto enum_names() noexcept -> detail::enable_if_t<E, detail::names_t<E>> {
  1022. return detail::names_v<std::decay_t<E>>;
  1023. }
  1024. // Returns std::array with pairs (value, name), sorted by enum value.
  1025. template <typename E>
  1026. [[nodiscard]] constexpr auto enum_entries() noexcept -> detail::enable_if_t<E, detail::entries_t<E>> {
  1027. return detail::entries_v<std::decay_t<E>>;
  1028. }
  1029. // Allows you to write magic_enum::enum_cast<foo>("bar", magic_enum::case_insensitive);
  1030. inline constexpr auto case_insensitive = detail::case_insensitive{};
  1031. // Obtains enum value from integer value.
  1032. // Returns optional with enum value.
  1033. template <typename E, detail::value_type VT = detail::value_type::default_value>
  1034. [[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, optional<std::decay_t<E>>> {
  1035. using D = std::decay_t<E>;
  1036. using U = underlying_type_t<D>;
  1037. if constexpr (detail::count_v<D> == 0) {
  1038. return {}; // Empty enum.
  1039. } else if constexpr (VT == detail::value_type::flags_value && detail::is_flags_v<D>) {
  1040. if constexpr (detail::is_sparse_v<D>) {
  1041. auto check_value = U{0};
  1042. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  1043. if (const auto v = static_cast<U>(enum_value<D>(i)); (value & v) != 0) {
  1044. check_value |= v;
  1045. }
  1046. }
  1047. if (check_value != 0 && check_value == value) {
  1048. return static_cast<D>(value);
  1049. }
  1050. } else {
  1051. constexpr auto min = detail::min_v<D>;
  1052. constexpr auto max = detail::values_ors<D>();
  1053. if (value >= min && value <= max) {
  1054. return static_cast<D>(value);
  1055. }
  1056. }
  1057. return {}; // Invalid value or out of range.
  1058. } else {
  1059. #if defined(MAGIC_ENUM_ENABLE_HASH)
  1060. return detail::constexpr_switch<&detail::values_v<D>, detail::case_call_t::value>(
  1061. [](D v) { return optional<D>{v}; },
  1062. static_cast<D>(value),
  1063. detail::default_result_type_lambda<optional<D>>);
  1064. #else
  1065. if constexpr (detail::is_sparse_v<D> || detail::is_flags_v<D>) {
  1066. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  1067. if (value == static_cast<U>(enum_value<D>(i))) {
  1068. return static_cast<D>(value);
  1069. }
  1070. }
  1071. } else {
  1072. if (value >= detail::min_v<D> && value <= detail::max_v<D>) {
  1073. return static_cast<D>(value);
  1074. }
  1075. }
  1076. return {}; // Invalid value or out of range.
  1077. #endif
  1078. }
  1079. }
  1080. // Obtains enum-flags value from integer value.
  1081. // Returns optional with enum-flags value.
  1082. template <typename E>
  1083. [[nodiscard]] constexpr auto enum_flags_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, optional<std::decay_t<E>>> {
  1084. using D = std::decay_t<E>;
  1085. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_cast requires enum-flags type.");
  1086. return enum_cast<D, detail::value_type::flags_value>(value);
  1087. }
  1088. // Obtains enum value from name.
  1089. // Returns optional with enum value.
  1090. template <typename E, detail::value_type VT = detail::value_type::default_value, typename BinaryPredicate = std::equal_to<>>
  1091. [[nodiscard]] constexpr auto enum_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> {
  1092. using D = std::decay_t<E>;
  1093. using U = underlying_type_t<D>;
  1094. if constexpr (detail::count_v<D> == 0) {
  1095. return {}; // Empty enum.
  1096. } else if constexpr (VT == detail::value_type::flags_value && detail::is_flags_v<D>) {
  1097. auto result = U{0};
  1098. while (!value.empty()) {
  1099. const auto d = detail::find(value, '|');
  1100. const auto s = (d == string_view::npos) ? value : value.substr(0, d);
  1101. auto f = U{0};
  1102. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  1103. if (detail::cmp_equal(s, detail::names_v<D>[i], p)) {
  1104. f = static_cast<U>(enum_value<D>(i));
  1105. result |= f;
  1106. break;
  1107. }
  1108. }
  1109. if (f == U{0}) {
  1110. return {}; // Invalid value or out of range.
  1111. }
  1112. value.remove_prefix((d == string_view::npos) ? value.size() : d + 1);
  1113. }
  1114. if (result != U{0}) {
  1115. return static_cast<D>(result);
  1116. }
  1117. return {}; // Invalid value or out of range.
  1118. } else {
  1119. if constexpr (detail::is_default_predicate<BinaryPredicate>() && detail::has_hash<D>) {
  1120. #if defined(MAGIC_ENUM_ENABLE_HASH)
  1121. return detail::constexpr_switch<&detail::names_v<D>, detail::case_call_t::index>(
  1122. [](std::size_t i) { return optional<D>{detail::values_v<D>[i]}; },
  1123. value,
  1124. detail::default_result_type_lambda<optional<D>>,
  1125. [&p](string_view lhs, string_view rhs) { return detail::cmp_equal(lhs, rhs, p); });
  1126. #endif
  1127. } else {
  1128. for (std::size_t i = 0; i < detail::count_v<D>; ++i) {
  1129. if (detail::cmp_equal(value, detail::names_v<D>[i], p)) {
  1130. return enum_value<D>(i);
  1131. }
  1132. }
  1133. return {}; // Invalid value or out of range.
  1134. }
  1135. }
  1136. }
  1137. // Obtains enum-flags value from name.
  1138. // Returns optional with enum-flags value.
  1139. template <typename E, typename BinaryPredicate = std::equal_to<>>
  1140. [[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> {
  1141. using D = std::decay_t<E>;
  1142. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_cast requires enum-flags type.");
  1143. return enum_cast<D, detail::value_type::flags_value>(value, std::move(p));
  1144. }
  1145. // Checks whether enum contains value with such value.
  1146. template <typename E, detail::value_type VT = detail::value_type::default_value>
  1147. [[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_t<E, bool> {
  1148. using D = std::decay_t<E>;
  1149. using U = underlying_type_t<D>;
  1150. return static_cast<bool>(enum_cast<D, VT>(static_cast<U>(value)));
  1151. }
  1152. // Checks whether enum-flags contains value with such value.
  1153. template <typename E>
  1154. [[nodiscard]] constexpr auto enum_flags_contains(E value) noexcept -> detail::enable_if_t<E, bool> {
  1155. using D = std::decay_t<E>;
  1156. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_contains requires enum-flags type.");
  1157. return enum_contains<D, detail::value_type::flags_value>(value);
  1158. }
  1159. // Checks whether enum contains value with such integer value.
  1160. template <typename E, detail::value_type VT = detail::value_type::default_value>
  1161. [[nodiscard]] constexpr auto enum_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, bool> {
  1162. using D = std::decay_t<E>;
  1163. return static_cast<bool>(enum_cast<D, VT>(value));
  1164. }
  1165. // Checks whether enum-flags contains value with such integer value.
  1166. template <typename E>
  1167. [[nodiscard]] constexpr auto enum_flags_contains(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, bool> {
  1168. using D = std::decay_t<E>;
  1169. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_contains requires enum-flags type.");
  1170. return enum_contains<D, detail::value_type::flags_value>(value);
  1171. }
  1172. // Checks whether enum contains enumerator with such name.
  1173. template <typename E, detail::value_type VT = detail::value_type::default_value, typename BinaryPredicate = std::equal_to<>>
  1174. [[nodiscard]] constexpr auto enum_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable<BinaryPredicate>()) -> detail::enable_if_t<E, bool, BinaryPredicate> {
  1175. using D = std::decay_t<E>;
  1176. return static_cast<bool>(enum_cast<D, VT>(value, std::move(p)));
  1177. }
  1178. // Checks whether enum-flags contains enumerator with such name.
  1179. template <typename E, typename BinaryPredicate = std::equal_to<>>
  1180. [[nodiscard]] constexpr auto enum_flags_contains(string_view value, BinaryPredicate p = {}) noexcept(detail::is_nothrow_invocable<BinaryPredicate>()) -> detail::enable_if_t<E, bool, BinaryPredicate> {
  1181. using D = std::decay_t<E>;
  1182. static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_contains requires enum-flags type.");
  1183. return enum_contains<D, detail::value_type::flags_value>(value, std::move(p));
  1184. }
  1185. template <typename E, typename F, detail::enable_if_t<E, int> = 0>
  1186. constexpr auto enum_for_each(F&& f) {
  1187. using D = std::decay_t<E>;
  1188. static_assert(std::is_enum_v<D>, "magic_enum::enum_for_each requires enum type.");
  1189. constexpr auto sep = std::make_index_sequence<detail::count_v<D>>{};
  1190. if constexpr (detail::all_invocable<D, F>(sep)) {
  1191. return detail::for_each<D>(std::forward<F>(f), sep);
  1192. } else {
  1193. static_assert(detail::always_false_v<D>, "magic_enum::enum_for_each requires invocable of all enum value.");
  1194. }
  1195. }
  1196. template <bool AsFlags = true>
  1197. inline constexpr auto as_flags = AsFlags ? detail::value_type::flags_value : detail::value_type::default_value;
  1198. #if !defined(MAGIC_ENUM_NO_STREAMS)
  1199. namespace ostream_operators {
  1200. template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
  1201. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, E value) {
  1202. using D = std::decay_t<E>;
  1203. using U = underlying_type_t<D>;
  1204. if constexpr (detail::supported<D>::value) {
  1205. if (const auto name = enum_name<D, as_flags<detail::is_flags_v<D>>>(value); !name.empty()) {
  1206. for (const auto c : name) {
  1207. os.put(c);
  1208. }
  1209. return os;
  1210. }
  1211. }
  1212. return (os << static_cast<U>(value));
  1213. }
  1214. template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
  1215. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& os, optional<E> value) {
  1216. return value ? (os << *value) : os;
  1217. }
  1218. } // namespace magic_enum::ostream_operators
  1219. namespace istream_operators {
  1220. template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
  1221. std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& is, E& value) {
  1222. using D = std::decay_t<E>;
  1223. std::basic_string<Char, Traits> s;
  1224. is >> s;
  1225. if (const auto v = enum_cast<D, as_flags<detail::is_flags_v<D>>>(s)) {
  1226. value = *v;
  1227. } else {
  1228. is.setstate(std::basic_ios<Char>::failbit);
  1229. }
  1230. return is;
  1231. }
  1232. } // namespace magic_enum::istream_operators
  1233. namespace iostream_operators {
  1234. using namespace ostream_operators;
  1235. using namespace istream_operators;
  1236. } // namespace magic_enum::iostream_operators
  1237. #endif
  1238. namespace bitwise_operators {
  1239. template <typename E, detail::enable_if_t<E, int> = 0>
  1240. constexpr E operator~(E rhs) noexcept {
  1241. return static_cast<E>(~static_cast<underlying_type_t<E>>(rhs));
  1242. }
  1243. template <typename E, detail::enable_if_t<E, int> = 0>
  1244. constexpr E operator|(E lhs, E rhs) noexcept {
  1245. return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) | static_cast<underlying_type_t<E>>(rhs));
  1246. }
  1247. template <typename E, detail::enable_if_t<E, int> = 0>
  1248. constexpr E operator&(E lhs, E rhs) noexcept {
  1249. return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) & static_cast<underlying_type_t<E>>(rhs));
  1250. }
  1251. template <typename E, detail::enable_if_t<E, int> = 0>
  1252. constexpr E operator^(E lhs, E rhs) noexcept {
  1253. return static_cast<E>(static_cast<underlying_type_t<E>>(lhs) ^ static_cast<underlying_type_t<E>>(rhs));
  1254. }
  1255. template <typename E, detail::enable_if_t<E, int> = 0>
  1256. constexpr E& operator|=(E& lhs, E rhs) noexcept {
  1257. return lhs = (lhs | rhs);
  1258. }
  1259. template <typename E, detail::enable_if_t<E, int> = 0>
  1260. constexpr E& operator&=(E& lhs, E rhs) noexcept {
  1261. return lhs = (lhs & rhs);
  1262. }
  1263. template <typename E, detail::enable_if_t<E, int> = 0>
  1264. constexpr E& operator^=(E& lhs, E rhs) noexcept {
  1265. return lhs = (lhs ^ rhs);
  1266. }
  1267. } // namespace magic_enum::bitwise_operators
  1268. } // namespace magic_enum
  1269. #if defined(__clang__)
  1270. # pragma clang diagnostic pop
  1271. #elif defined(__GNUC__)
  1272. # pragma GCC diagnostic pop
  1273. #elif defined(_MSC_VER)
  1274. # pragma warning(pop)
  1275. #endif
  1276. #endif // NEARGYE_MAGIC_ENUM_HPP