enum_mask.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. namespace base {
  9. template <typename Enum>
  10. class enum_mask {
  11. using Type = std::uint32_t;
  12. public:
  13. static_assert(static_cast<int>(Enum::kCount) <= 32, "We have only 32 bit.");
  14. enum_mask() = default;
  15. enum_mask(Enum value) : _value(ToBit(value)) {
  16. }
  17. static enum_mask All() {
  18. auto result = enum_mask();
  19. result._value = ~Type(0);
  20. return result;
  21. }
  22. enum_mask added(enum_mask other) const {
  23. auto result = *this;
  24. result.set(other);
  25. return result;
  26. }
  27. void set(enum_mask other) {
  28. _value |= other._value;
  29. }
  30. bool test(Enum value) const {
  31. return _value & ToBit(value);
  32. }
  33. explicit operator bool() const {
  34. return _value != 0;
  35. }
  36. private:
  37. inline static Type ToBit(Enum value) {
  38. return 1 << static_cast<Type>(value);
  39. }
  40. Type _value = 0;
  41. };
  42. } // namespace base