StormByte C++ Library 0.0.9999
StormByte is a comprehensive, cross-platform C++ library aimed at easing system programming, configuration management, logging, and database handling tasks. This library provides a unified API that abstracts away the complexities and inconsistencies of different platforms (Windows, Linux).
Loading...
Searching...
No Matches
bitmask.hxx
1#include <StormByte/type_traits.hxx>
2
3using namespace StormByte;
4
14template <Type::UnsignedEnum E>
15constexpr E operator|(E a, E b) noexcept {
16 return static_cast<E>(Type::ToUnderlying(a) | Type::ToUnderlying(b));
17}
18
28template <Type::UnsignedEnum E>
29constexpr E operator&(E a, E b) noexcept {
30 return static_cast<E>(Type::ToUnderlying(a) & Type::ToUnderlying(b));
31}
32
42template <Type::UnsignedEnum E>
43constexpr E operator^(E a, E b) noexcept {
44 return static_cast<E>(Type::ToUnderlying(a) ^ Type::ToUnderlying(b));
45}
46
55template <Type::UnsignedEnum E>
56constexpr E operator~(E a) noexcept {
57 return static_cast<E>(~Type::ToUnderlying(a));
58}
59
67namespace StormByte {
94 template<typename Derived, Type::UnsignedEnum E>
95 class Bitmask {
96 public:
100 constexpr Bitmask() noexcept: m_value(static_cast<E>(0)) {}
101
106 constexpr Bitmask(E value) noexcept: m_value(value) {}
107
112 constexpr Bitmask(const Bitmask& other) noexcept = default;
113
118 constexpr Bitmask(Bitmask&& other) noexcept = default;
119
123 constexpr virtual ~Bitmask() noexcept = default;
124
130 constexpr Bitmask& operator=(const Bitmask& other) noexcept = default;
131
137 constexpr Bitmask& operator=(Bitmask&& other) noexcept = default;
138
144 constexpr bool operator==(const Bitmask& other) const noexcept {
145 return m_value == other.m_value;
146 }
147
153 constexpr bool operator!=(const Bitmask& other) const noexcept {
154 return m_value != other.m_value;
155 }
156
162 constexpr Derived operator|(const Bitmask& other) const noexcept {
163 return Derived(m_value | other.m_value);
164 }
165
171 constexpr Derived operator&(const Bitmask& other) const noexcept {
172 return Derived(m_value & other.m_value);
173 }
174
180 constexpr Derived operator^(const Bitmask& other) const noexcept {
181 return Derived(m_value ^ other.m_value);
182 }
183
188 constexpr Derived operator~() const noexcept {
189 return Derived(~m_value);
190 }
191
197 constexpr Bitmask& operator|=(const Bitmask& other) noexcept {
198 m_value = m_value | other.m_value;
199 return *this;
200 }
201
207 constexpr Bitmask& operator&=(const Bitmask& other) noexcept {
208 m_value = m_value & other.m_value;
209 return *this;
210 }
211
217 constexpr Bitmask& operator^=(const Bitmask& other) noexcept {
218 m_value = m_value ^ other.m_value;
219 return *this;
220 }
221
226 constexpr void Add(E value) noexcept {
227 m_value = m_value | value;
228 }
229
234 constexpr void Remove(E value) noexcept {
235 m_value = m_value & ~value;
236 }
237
242 constexpr E Value() const noexcept {
243 return m_value;
244 }
245
251 constexpr bool Has(E value) const noexcept {
252 return (m_value & value) == value;
253 }
254
260 constexpr bool Has(const Bitmask& other) const noexcept {
261 return Has(other.m_value);
262 }
263
269 constexpr bool HasAny(E value) const noexcept {
270 return (m_value & value) != static_cast<E>(0);
271 }
272
278 constexpr bool HasAny(const Bitmask& other) const noexcept {
279 return HasAny(other.m_value);
280 }
281
287 constexpr bool HasNone(E value) const noexcept {
288 return (m_value & value) == static_cast<E>(0);
289 }
290
296 constexpr bool HasNone(const Bitmask& other) const noexcept {
297 return HasNone(other.m_value);
298 }
299
300 protected:
301 E m_value;
302 };
303}
Bitmask class template for managing enum class flags.
Definition bitmask.hxx:95
constexpr Bitmask(E value) noexcept
Constructor initializes the bitmask with a specific enum value.
Definition bitmask.hxx:106
constexpr Bitmask & operator&=(const Bitmask &other) noexcept
Bitwise AND assignment operator.
Definition bitmask.hxx:207
constexpr void Remove(E value) noexcept
Remove a flag from the bitmask.
Definition bitmask.hxx:234
constexpr bool HasNone(E value) const noexcept
Check if none of the specified flags are set in the bitmask.
Definition bitmask.hxx:287
constexpr bool HasAny(E value) const noexcept
Check if any of the specified flags are set in the bitmask.
Definition bitmask.hxx:269
constexpr bool operator!=(const Bitmask &other) const noexcept
Inequality operator.
Definition bitmask.hxx:153
constexpr bool Has(E value) const noexcept
Check if the bitmask has a specific flag set.
Definition bitmask.hxx:251
constexpr Derived operator|(const Bitmask &other) const noexcept
Bitwise OR operator.
Definition bitmask.hxx:162
constexpr Bitmask(Bitmask &&other) noexcept=default
Move constructor.
constexpr E Value() const noexcept
Get the current value of the bitmask.
Definition bitmask.hxx:242
constexpr bool HasAny(const Bitmask &other) const noexcept
Check if any of the flags from another bitmask are set in this bitmask.
Definition bitmask.hxx:278
constexpr Bitmask & operator^=(const Bitmask &other) noexcept
Bitwise XOR assignment operator.
Definition bitmask.hxx:217
constexpr Bitmask & operator|=(const Bitmask &other) noexcept
Bitwise OR assignment operator.
Definition bitmask.hxx:197
constexpr bool HasNone(const Bitmask &other) const noexcept
Check if none of the flags from another bitmask are set in this bitmask.
Definition bitmask.hxx:296
constexpr Bitmask() noexcept
Default constructor initializes the bitmask to zero.
Definition bitmask.hxx:100
constexpr Derived operator~() const noexcept
Bitwise NOT operator.
Definition bitmask.hxx:188
constexpr Bitmask(const Bitmask &other) noexcept=default
Copy constructor.
virtual constexpr ~Bitmask() noexcept=default
Destructor.
constexpr Derived operator&(const Bitmask &other) const noexcept
Bitwise AND operator.
Definition bitmask.hxx:171
constexpr bool Has(const Bitmask &other) const noexcept
Check if the bitmask has all flags set from another bitmask.
Definition bitmask.hxx:260
constexpr Derived operator^(const Bitmask &other) const noexcept
Bitwise XOR operator.
Definition bitmask.hxx:180
constexpr void Add(E value) noexcept
Add a flag to the bitmask.
Definition bitmask.hxx:226
Main namespace for the StormByte library.
Modern C++20 concepts for type checking.