SlHelpers
Enum.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <type_traits>
6 
7 namespace SlHelpers {
8 
10 template <typename Enum>
11 class EnumRange {
12 public:
14  using Underlying = std::underlying_type_t<Enum>;
15 
17  struct iterator {
20 
22  Enum operator*() const { return static_cast<Enum>(v); }
23 
26  ++v;
27  return *this;
28  }
29 
31  bool operator==(const iterator &other) const { return v == other.v; }
33  bool operator!=(const iterator &other) const { return v != other.v; }
34  };
35 
37  iterator begin() const { return { static_cast<Underlying>(Enum::First) }; }
39  iterator end() const { return { static_cast<Underlying>(Enum::Last) + 1 }; }
40 };
41 
42 template<typename E>
43 struct hasBitmaskOperators : std::false_type {};
44 
45 #define ENABLE_BITMASK_OPERATORS(E) \
46  template<> struct SlHelpers::hasBitmaskOperators<E> : std::true_type {}
47 
48 template<typename E>
49 concept BitmaskEnum = std::is_enum_v<E> && hasBitmaskOperators<E>::value;
50 
51 } // namespace
52 
53 template<SlHelpers::BitmaskEnum E>
54 constexpr E operator~(E lhs)
55 {
56  using underlying = std::underlying_type_t<E>;
57  return static_cast<E>(~static_cast<underlying>(lhs));
58 }
59 
60 template<SlHelpers::BitmaskEnum E>
61 constexpr E operator|(E lhs, E rhs)
62 {
63  using underlying = std::underlying_type_t<E>;
64  return static_cast<E>(static_cast<underlying>(lhs) | static_cast<underlying>(rhs));
65 }
66 
67 template<SlHelpers::BitmaskEnum E>
68 constexpr E operator&(E lhs, E rhs)
69 {
70  using underlying = std::underlying_type_t<E>;
71  return static_cast<E>(static_cast<underlying>(lhs) & static_cast<underlying>(rhs));
72 }
73 
74 template<SlHelpers::BitmaskEnum E>
75 constexpr E &operator|=(E &lhs, E rhs)
76 {
77  return lhs = lhs | rhs;
78 }
79 
80 template<SlHelpers::BitmaskEnum E>
81 constexpr E &operator&=(E &lhs, E rhs)
82 {
83  return lhs = lhs & rhs;
84 }
85 
86 template<SlHelpers::BitmaskEnum E>
87 constexpr bool hasFlag(E flags, E flag)
88 {
89  return (flags & flag) != E::NONE;
90 }
std::underlying_type_t< Enum > Underlying
Type of the underlying enum values.
Definition: Enum.h:14
Definition: Enum.h:43
bool operator!=(const iterator &other) const
Inequality operator to compare two iterators.
Definition: Enum.h:33
Enum operator*() const
Dereference operator to get the current enum value.
Definition: Enum.h:22
iterator begin() const
Returns an iterator to the beginning of the enum range.
Definition: Enum.h:37
Helper class to iterate over enum values from Enum::First to Enum::Last.
Definition: Enum.h:11
Underlying v
Current value of the iterator.
Definition: Enum.h:19
iterator end() const
Returns an iterator to the end of the enum range.
Definition: Enum.h:39
Iterator class to iterate over enum values.
Definition: Enum.h:17
bool operator==(const iterator &other) const
Equality operator to compare two iterators.
Definition: Enum.h:31
iterator & operator++()
Pre-increment operator to move to the next enum value.
Definition: Enum.h:25
Definition: Color.h:8