🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.2 KiB

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