AUI Framework  master
Cross-platform module-based framework for developing C++20 desktop applications
ABitField.h
1/*
2 * AUI Framework - Declarative UI toolkit for modern C++20
3 * Copyright (C) 2020-2024 Alex2772 and Contributors
4 *
5 * SPDX-License-Identifier: MPL-2.0
6 *
7 * This Source Code Form is subject to the terms of the Mozilla Public
8 * License, v. 2.0. If a copy of the MPL was not distributed with this
9 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 */
11
12#pragma once
13
14#include <cstdint>
15
19template <typename T = uint32_t>
20class ABitField {
21private:
22 T mStorage;
23
24public:
25 ABitField(T storage = static_cast<T>(0)) : mStorage(storage) {}
26
27 operator T() {
28 return mStorage;
29 }
30
31 T& value() {
32 return mStorage;
33 }
34
35 const T& value() const {
36 return mStorage;
37 }
38
45 mStorage |= flag;
46 return *this;
47 }
48
55 mStorage &= ~flag;
56 return *this;
57 }
58
64 bool checkAndSet(T flag) {
65 if (!!(mStorage & flag)) {
66 mStorage &= ~flag;
67 return true;
68 }
69 return false;
70 }
71
77 bool checkAndReset(T flag)
78 {
79 if (mStorage & flag)
80 {
81 return false;
82 }
83 mStorage |= flag;
84 return true;
85 }
86
93 bool test(T flags) const {
94 return (mStorage & flags) == flags;
95 }
102 bool testAny(T flags) const {
103 return bool(mStorage & flags);
104 }
105
112 bool operator&(T flags) const {
113 return test(flags);
114 }
115
116 void set(T flag, bool value) {
117 if (value) {
118 mStorage |= flag;
119 } else {
120 mStorage &= ~flag;
121 }
122 }
123};
Bit field implementation.
Definition: ABitField.h:20
ABitField & operator<<(T flag)
Sets flag.
Definition: ABitField.h:44
ABitField & operator>>(T flag)
Resets flag.
Definition: ABitField.h:54
bool checkAndReset(T flag)
Determines whether flag set or not and sets flag.
Definition: ABitField.h:77
bool checkAndSet(T flag)
Determines whether flag set or not and resets flag.
Definition: ABitField.h:64
bool testAny(T flags) const
Determines whether flag (or one of the flags flags) set or not.
Definition: ABitField.h:102
bool operator&(T flags) const
Determines whether flag(s) set or not.
Definition: ABitField.h:112
bool test(T flags) const
Determines whether flag (or all flags) set or not.
Definition: ABitField.h:93