bitmask.hpp 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // boost/detail/bitmask.hpp ------------------------------------------------//
  2. // Copyright Beman Dawes 2006
  3. // Copyright Andrey Semashev 2025
  4. // Distributed under the Boost Software License, Version 1.0
  5. // http://www.boost.org/LICENSE_1_0.txt
  6. // Usage: enum foo { a=1, b=2, c=4 };
  7. // BOOST_BITMASK( foo )
  8. //
  9. // void f( foo arg );
  10. // ...
  11. // f( a | c );
  12. //
  13. // See [bitmask.types] in the C++ standard for the formal specification
  14. #ifndef BOOST_BITMASK_HPP
  15. #define BOOST_BITMASK_HPP
  16. #include <boost/config.hpp>
  17. #if defined(__has_builtin)
  18. #if __has_builtin(__underlying_type)
  19. #define BOOST_BITMASK_DETAIL_UNDERLYING_TYPE(enum_type) __underlying_type(enum_type)
  20. #endif
  21. #endif
  22. #if !defined(BOOST_BITMASK_DETAIL_UNDERLYING_TYPE) && \
  23. ((defined(BOOST_GCC_VERSION) && (BOOST_GCC_VERSION >= 40700)) || (defined(_MSC_VER) && (_MSC_VER >= 1700)))
  24. #define BOOST_BITMASK_DETAIL_UNDERLYING_TYPE(enum_type) __underlying_type(enum_type)
  25. #endif
  26. #if !defined(BOOST_BITMASK_DETAIL_UNDERLYING_TYPE)
  27. #include <type_traits>
  28. #endif
  29. namespace boost {
  30. namespace detail {
  31. namespace bitmask {
  32. #if defined(BOOST_BITMASK_DETAIL_UNDERLYING_TYPE)
  33. template< typename Enum >
  34. using underlying_type_t = BOOST_BITMASK_DETAIL_UNDERLYING_TYPE(Enum);
  35. #elif (BOOST_CXX_VERSION >= 201402)
  36. using std::underlying_type_t;
  37. #else
  38. template< typename Enum >
  39. using underlying_type_t = typename std::underlying_type< Enum >::type;
  40. #endif
  41. }}}
  42. #undef BOOST_BITMASK_DETAIL_UNDERLYING_TYPE
  43. #define BOOST_BITMASK(Bitmask) \
  44. \
  45. inline BOOST_CONSTEXPR Bitmask operator| (Bitmask x, Bitmask y) BOOST_NOEXCEPT \
  46. { return static_cast< Bitmask >(static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x) \
  47. | static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(y)); } \
  48. \
  49. inline BOOST_CONSTEXPR Bitmask operator& (Bitmask x, Bitmask y) BOOST_NOEXCEPT \
  50. { return static_cast< Bitmask >(static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x) \
  51. & static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(y)); } \
  52. \
  53. inline BOOST_CONSTEXPR Bitmask operator^ (Bitmask x, Bitmask y) BOOST_NOEXCEPT \
  54. { return static_cast< Bitmask >(static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x) \
  55. ^ static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(y)); } \
  56. \
  57. inline BOOST_CONSTEXPR Bitmask operator~ (Bitmask x) BOOST_NOEXCEPT \
  58. { return static_cast< Bitmask >(~static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x)); } \
  59. \
  60. inline BOOST_CXX14_CONSTEXPR Bitmask& operator&=(Bitmask& x, Bitmask y) BOOST_NOEXCEPT \
  61. { x = x & y; return x; } \
  62. \
  63. inline BOOST_CXX14_CONSTEXPR Bitmask& operator|=(Bitmask& x, Bitmask y) BOOST_NOEXCEPT \
  64. { x = x | y; return x; } \
  65. \
  66. inline BOOST_CXX14_CONSTEXPR Bitmask& operator^=(Bitmask& x, Bitmask y) BOOST_NOEXCEPT \
  67. { x = x ^ y; return x; } \
  68. \
  69. /* Boost extensions to [bitmask.types] */ \
  70. \
  71. inline BOOST_CONSTEXPR bool operator!(Bitmask x) BOOST_NOEXCEPT \
  72. { return !static_cast< ::boost::detail::bitmask::underlying_type_t< Bitmask > >(x); } \
  73. \
  74. BOOST_DEPRECATED("bitmask_set(enum) is deprecated, use !!enum or comparison operators instead") \
  75. inline BOOST_CONSTEXPR bool bitmask_set(Bitmask x) BOOST_NOEXCEPT \
  76. { return !!x; }
  77. #endif // BOOST_BITMASK_HPP