BitMask.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (C) 2009-present, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Util/StdTypes.h>
  7. namespace anki {
  8. /// @addtogroup util_containers
  9. /// @{
  10. /// Easy bit mask manipulation.
  11. template<typename T>
  12. class BitMask
  13. {
  14. public:
  15. using Value = T;
  16. /// Default contructor. Will zero the mask.
  17. BitMask()
  18. : m_bitmask(0)
  19. {
  20. }
  21. /// Construcor.
  22. /// @param mask The bits to enable.
  23. BitMask(Value mask)
  24. : m_bitmask(mask)
  25. {
  26. }
  27. /// Copy.
  28. BitMask(const BitMask& b) = default;
  29. /// Copy.
  30. BitMask& operator=(const BitMask& b) = default;
  31. /// Set or unset a bit at the given position.
  32. void set(Value mask, Bool setBits = true)
  33. {
  34. m_bitmask = (setBits) ? (m_bitmask | mask) : (m_bitmask & ~mask);
  35. }
  36. /// Unset a bit (set to zero) at the given position.
  37. void unset(Value mask)
  38. {
  39. m_bitmask &= ~mask;
  40. }
  41. /// Flip the bits at the given position. It will go from 1 to 0 or from 0 to 1.
  42. void flip(Value mask)
  43. {
  44. m_bitmask ^= mask;
  45. }
  46. /// Return true if the bit is set or false if it's not.
  47. Bool get(Value mask) const
  48. {
  49. return (m_bitmask & mask) == mask;
  50. }
  51. /// Given a mask check if any are enabled.
  52. Bool getAny(Value mask) const
  53. {
  54. return (m_bitmask & mask) != 0;
  55. }
  56. protected:
  57. Value m_bitmask;
  58. };
  59. /// @}
  60. } // end namespace anki