magic_enum_fuse.hpp 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_FUSE_HPP
  32. #define NEARGYE_MAGIC_ENUM_FUSE_HPP
  33. #include "magic_enum.hpp"
  34. namespace magic_enum {
  35. namespace detail {
  36. template <typename E>
  37. constexpr optional<std::uintmax_t> fuse_one_enum(optional<std::uintmax_t> hash, E value) noexcept {
  38. if (hash) {
  39. if (const auto index = enum_index(value)) {
  40. return (*hash << log2(enum_count<E>() + 1)) | *index;
  41. }
  42. }
  43. return {};
  44. }
  45. template <typename E>
  46. constexpr optional<std::uintmax_t> fuse_enum(E value) noexcept {
  47. return fuse_one_enum(0, value);
  48. }
  49. template <typename E, typename... Es>
  50. constexpr optional<std::uintmax_t> fuse_enum(E head, Es... tail) noexcept {
  51. return fuse_one_enum(fuse_enum(tail...), head);
  52. }
  53. template <typename... Es>
  54. constexpr auto typesafe_fuse_enum(Es... values) noexcept {
  55. enum class enum_fuse_t : std::uintmax_t;
  56. const auto fuse = fuse_enum(values...);
  57. if (fuse) {
  58. return optional<enum_fuse_t>{static_cast<enum_fuse_t>(*fuse)};
  59. }
  60. return optional<enum_fuse_t>{};
  61. }
  62. } // namespace magic_enum::detail
  63. // Returns a bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
  64. template <typename... Es>
  65. [[nodiscard]] constexpr auto enum_fuse(Es... values) noexcept {
  66. static_assert((std::is_enum_v<std::decay_t<Es>> && ...), "magic_enum::enum_fuse requires enum type.");
  67. static_assert(sizeof...(Es) >= 2, "magic_enum::enum_fuse requires at least 2 values.");
  68. static_assert((detail::log2(enum_count<std::decay_t<Es>>() + 1) + ...) <= (sizeof(std::uintmax_t) * 8), "magic_enum::enum_fuse does not work for large enums");
  69. #if defined(MAGIC_ENUM_NO_TYPESAFE_ENUM_FUSE)
  70. const auto fuse = detail::fuse_enum<std::decay_t<Es>...>(values...);
  71. #else
  72. const auto fuse = detail::typesafe_fuse_enum<std::decay_t<Es>...>(values...);
  73. #endif
  74. return assert(fuse), fuse;
  75. }
  76. } // namespace magic_enum
  77. #endif // NEARGYE_MAGIC_ENUM_FUSE_HPP