StormByte C++ Library 1.2.0
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
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte.
5 *
6 * StormByte is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License version 3
8 * or later, as published by the Free Software Foundation.
9 *
10 * StormByte is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with StormByte. If not, see
17 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
18 */
19
20#pragma once
21
23
24using namespace StormByte;
25
33template <Type::UnsignedEnum E>
34constexpr E operator|(E a, E b) noexcept {
35 return static_cast<E>(Type::ToUnderlying(a) | Type::ToUnderlying(b));
36}
37
45template <Type::UnsignedEnum E>
46constexpr E operator&(E a, E b) noexcept {
47 return static_cast<E>(Type::ToUnderlying(a) & Type::ToUnderlying(b));
48}
49
57template <Type::UnsignedEnum E>
58constexpr E operator^(E a, E b) noexcept {
59 return static_cast<E>(Type::ToUnderlying(a) ^ Type::ToUnderlying(b));
60}
61
68template <Type::UnsignedEnum E>
69constexpr E operator~(E a) noexcept {
70 return static_cast<E>(~Type::ToUnderlying(a));
71}
72
77namespace 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
151 constexpr bool operator!=(const Bitmask& other) const noexcept;
152
158 constexpr Derived operator|(const Bitmask& other) const noexcept;
159
165 constexpr Derived operator&(const Bitmask& other) const noexcept;
166
172 constexpr Derived operator^(const Bitmask& other) const noexcept;
173
178 constexpr Derived operator~() const noexcept;
179
185 constexpr Bitmask& operator|=(const Bitmask& other) noexcept;
186
192 constexpr Bitmask& operator&=(const Bitmask& other) noexcept;
193
199 constexpr Bitmask& operator^=(const Bitmask& other) noexcept;
200
205 constexpr void Add(E value) noexcept;
206
211 constexpr void Remove(E value) noexcept;
212
217 constexpr E Value() const noexcept;
218
223 constexpr bool Has(E value) const noexcept;
224
229 constexpr bool Has(const Bitmask& other) const noexcept;
230
235 constexpr bool HasAny(E value) const noexcept;
236
241 constexpr bool HasAny(const Bitmask& other) const noexcept;
242
247 constexpr bool HasNone(E value) const noexcept;
248
253 constexpr bool HasNone(const Bitmask& other) const noexcept;
254
255 protected:
257 };
258}
259
260#include <StormByte/bitmask.txx>
constexpr E operator~(E a) noexcept
Bitwise NOT for unsigned scoped enumerations.
Definition bitmask.hxx:69
constexpr E operator&(E a, E b) noexcept
Bitwise AND for unsigned scoped enumerations.
Definition bitmask.hxx:46
constexpr E operator|(E a, E b) noexcept
Bitwise OR for unsigned scoped enumerations.
Definition bitmask.hxx:34
constexpr E operator^(E a, E b) noexcept
Bitwise XOR for unsigned scoped enumerations.
Definition bitmask.hxx:58
CRTP wrapper that stores an unsigned enum as a flag set.
Definition bitmask.hxx:95
constexpr Bitmask(E value) noexcept
Mask from a single enumerator (or already combined value).
Definition bitmask.hxx:106
constexpr void Remove(E value) noexcept
Clears every bit in value.
constexpr bool HasNone(E value) const noexcept
true when no bit in value is set.
constexpr bool HasAny(E value) const noexcept
true when any bit in value is set.
constexpr bool Has(E value) const noexcept
true when every bit in value is set.
constexpr Bitmask(Bitmask &&other) noexcept=default
Move constructor.
E m_value
Packed flags.
Definition bitmask.hxx:256
constexpr E Value() const noexcept
Stored enumerators.
constexpr Bitmask() noexcept
Empty mask (static_cast<E>(0)).
Definition bitmask.hxx:100
constexpr Bitmask(const Bitmask &other) noexcept=default
Copy constructor.
virtual constexpr ~Bitmask() noexcept=default
Destructor.
constexpr void Add(E value) noexcept
Sets every bit in value.
constexpr UnderlyingType< E > ToUnderlying(E e) noexcept
Converts an enumeration value to its underlying integer.
Definition enums.hxx:103
Root namespace of the StormByte suite.