bitmask.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // boost/detail/bitmask.hpp ------------------------------------------------//
  2. // Copyright Beman Dawes 2006
  3. // Distributed under the Boost Software License, Version 1.0
  4. // http://www.boost.org/LICENSE_1_0.txt
  5. // Usage: enum foo { a=1, b=2, c=4 };
  6. // BOOST_BITMASK( foo )
  7. //
  8. // void f( foo arg );
  9. // ...
  10. // f( a | c );
  11. //
  12. // See [bitmask.types] in the C++ standard for the formal specification
  13. #ifndef BOOST_BITMASK_HPP
  14. #define BOOST_BITMASK_HPP
  15. #include <boost/config.hpp>
  16. #include <boost/cstdint.hpp>
  17. #define BOOST_BITMASK(Bitmask) \
  18. \
  19. inline BOOST_CONSTEXPR Bitmask operator| (Bitmask x , Bitmask y ) \
  20. { return static_cast<Bitmask>( static_cast<boost::int_least32_t>(x) \
  21. | static_cast<boost::int_least32_t>(y)); } \
  22. \
  23. inline BOOST_CONSTEXPR Bitmask operator& (Bitmask x , Bitmask y ) \
  24. { return static_cast<Bitmask>( static_cast<boost::int_least32_t>(x) \
  25. & static_cast<boost::int_least32_t>(y)); } \
  26. \
  27. inline BOOST_CONSTEXPR Bitmask operator^ (Bitmask x , Bitmask y ) \
  28. { return static_cast<Bitmask>( static_cast<boost::int_least32_t>(x) \
  29. ^ static_cast<boost::int_least32_t>(y)); } \
  30. \
  31. inline BOOST_CONSTEXPR Bitmask operator~ (Bitmask x ) \
  32. { return static_cast<Bitmask>(~static_cast<boost::int_least32_t>(x)); } \
  33. \
  34. inline Bitmask & operator&=(Bitmask& x , Bitmask y) \
  35. { x = x & y ; return x ; } \
  36. \
  37. inline Bitmask & operator|=(Bitmask& x , Bitmask y) \
  38. { x = x | y ; return x ; } \
  39. \
  40. inline Bitmask & operator^=(Bitmask& x , Bitmask y) \
  41. { x = x ^ y ; return x ; } \
  42. \
  43. /* Boost extensions to [bitmask.types] */ \
  44. \
  45. inline BOOST_CONSTEXPR bool operator!(Bitmask x) \
  46. { return !static_cast<int>(x); } \
  47. \
  48. inline BOOST_CONSTEXPR bool bitmask_set(Bitmask x) \
  49. { return !!x; }
  50. #endif // BOOST_BITMASK_HPP