enum.hpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #ifndef ENTT_CORE_ENUM_HPP
  2. #define ENTT_CORE_ENUM_HPP
  3. #include <type_traits>
  4. namespace entt {
  5. /**
  6. * @brief Enable bitmask support for enum classes.
  7. * @tparam Type The enum type for which to enable bitmask support.
  8. */
  9. template<typename Type, typename = void>
  10. struct enum_as_bitmask: std::false_type {};
  11. /*! @copydoc enum_as_bitmask */
  12. template<typename Type>
  13. struct enum_as_bitmask<Type, std::void_t<decltype(Type::_entt_enum_as_bitmask)>>: std::is_enum<Type> {};
  14. /**
  15. * @brief Helper variable template.
  16. * @tparam Type The enum class type for which to enable bitmask support.
  17. */
  18. template<typename Type>
  19. inline constexpr bool enum_as_bitmask_v = enum_as_bitmask<Type>::value;
  20. } // namespace entt
  21. /**
  22. * @brief Operator available for enums for which bitmask support is enabled.
  23. * @tparam Type Enum class type.
  24. * @param lhs The first value to use.
  25. * @param rhs The second value to use.
  26. * @return The result of invoking the operator on the underlying types of the
  27. * two values provided.
  28. */
  29. template<typename Type>
  30. [[nodiscard]] constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type>
  31. operator|(const Type lhs, const Type rhs) noexcept {
  32. return static_cast<Type>(static_cast<std::underlying_type_t<Type>>(lhs) | static_cast<std::underlying_type_t<Type>>(rhs));
  33. }
  34. /*! @copydoc operator| */
  35. template<typename Type>
  36. [[nodiscard]] constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type>
  37. operator&(const Type lhs, const Type rhs) noexcept {
  38. return static_cast<Type>(static_cast<std::underlying_type_t<Type>>(lhs) & static_cast<std::underlying_type_t<Type>>(rhs));
  39. }
  40. /*! @copydoc operator| */
  41. template<typename Type>
  42. [[nodiscard]] constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type>
  43. operator^(const Type lhs, const Type rhs) noexcept {
  44. return static_cast<Type>(static_cast<std::underlying_type_t<Type>>(lhs) ^ static_cast<std::underlying_type_t<Type>>(rhs));
  45. }
  46. /**
  47. * @brief Operator available for enums for which bitmask support is enabled.
  48. * @tparam Type Enum class type.
  49. * @param value The value to use.
  50. * @return The result of invoking the operator on the underlying types of the
  51. * value provided.
  52. */
  53. template<typename Type>
  54. [[nodiscard]] constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type>
  55. operator~(const Type value) noexcept {
  56. return static_cast<Type>(~static_cast<std::underlying_type_t<Type>>(value));
  57. }
  58. /*! @copydoc operator~ */
  59. template<typename Type>
  60. [[nodiscard]] constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, bool>
  61. operator!(const Type value) noexcept {
  62. return !static_cast<std::underlying_type_t<Type>>(value);
  63. }
  64. /*! @copydoc operator| */
  65. template<typename Type>
  66. constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type &>
  67. operator|=(Type &lhs, const Type rhs) noexcept {
  68. return (lhs = (lhs | rhs));
  69. }
  70. /*! @copydoc operator| */
  71. template<typename Type>
  72. constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type &>
  73. operator&=(Type &lhs, const Type rhs) noexcept {
  74. return (lhs = (lhs & rhs));
  75. }
  76. /*! @copydoc operator| */
  77. template<typename Type>
  78. constexpr std::enable_if_t<entt::enum_as_bitmask_v<Type>, Type &>
  79. operator^=(Type &lhs, const Type rhs) noexcept {
  80. return (lhs = (lhs ^ rhs));
  81. }
  82. #endif