BitMask.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (C) 2009-2021, 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. {
  9. /// @addtogroup util_containers
  10. /// @{
  11. /// Easy bit mask manipulation.
  12. template<typename T>
  13. class BitMask
  14. {
  15. public:
  16. using Value = T;
  17. /// Default contructor. Will zero the mask.
  18. BitMask()
  19. : m_bitmask(0)
  20. {
  21. }
  22. /// Construcor.
  23. /// @param mask The bits to enable.
  24. BitMask(Value mask)
  25. : m_bitmask(mask)
  26. {
  27. }
  28. /// Copy.
  29. BitMask(const BitMask& b) = default;
  30. /// Copy.
  31. BitMask& operator=(const BitMask& b) = default;
  32. /// Set or unset a bit at the given position.
  33. void set(Value mask, Bool setBits = true)
  34. {
  35. m_bitmask = (setBits) ? (m_bitmask | mask) : (m_bitmask & ~mask);
  36. }
  37. /// Unset a bit (set to zero) at the given position.
  38. void unset(Value mask)
  39. {
  40. m_bitmask &= ~mask;
  41. }
  42. /// Flip the bits at the given position. It will go from 1 to 0 or from 0 to 1.
  43. void flip(Value mask)
  44. {
  45. m_bitmask ^= mask;
  46. }
  47. /// Return true if the bit is set or false if it's not.
  48. Bool get(Value mask) const
  49. {
  50. return (m_bitmask & mask) == mask;
  51. }
  52. /// Given a mask check if any are enabled.
  53. Bool getAny(Value mask) const
  54. {
  55. return (m_bitmask & mask) != 0;
  56. }
  57. protected:
  58. Value m_bitmask;
  59. };
  60. /// @}
  61. } // end namespace anki