PIDUINO
Loading...
Searching...
No Matches
flags.h
1/* Copyright © 2018-2025 Pascal JEAN, All rights reserved.
2 This file is part of the Piduino Library.
3
4 The Piduino Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 3 of the License, or (at your option) any later version.
8
9 The Piduino Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with the Piduino Library; if not, see <http://www.gnu.org/licenses/>.
16*/
17#pragma once
18
19
20namespace Piduino {
21
33 template <typename EnumType, typename Underlying = int>
34 class Flags {
36 typedef Underlying Flags::*RestrictedBool;
37
38 public:
43 Flags (Underlying original = 0) : m_flags (original) {}
44
49 Flags (const Flags &original) :
50 m_flags (original.m_flags)
51 {}
52
60 m_flags |= f.m_flags;
61 return *this;
62 }
63
71 m_flags ^= f.m_flags;
72 return *this;
73 }
74
82 m_flags &= f.m_flags;
83 return *this;
84 }
85
92 friend Flags operator | (const Flags &f1, const Flags &f2) {
93 return Flags (f1) |= f2;
94 }
95
102 friend Flags operator ^ (const Flags &f1, const Flags &f2) {
103 return Flags (f1) ^= f2;
104 }
105
112 friend Flags operator & (const Flags &f1, const Flags &f2) {
113 return Flags (f1) &= f2;
114 }
115
121 Flags result (*this);
122 result.m_flags = ~result.m_flags;
123 return result;
124 }
125
132 operator RestrictedBool() const {
133 return m_flags ? &Flags::m_flags : 0;
134 }
135
141 bool operator== (const Flags &other) {
142 return m_flags == other.m_flags;
143 }
144
150 bool operator!= (const Flags &other) {
151 return m_flags != other.m_flags;
152 }
153
158 void setFlags (Underlying flags) {
159 m_flags |= flags;
160 }
161
166 void setFlag (EnumType singleFlag) {
167 setFlags (static_cast<Underlying> (singleFlag));
168 }
169
174 void clearFlags (Underlying flags) {
175 m_flags &= ~flags;
176 }
177
182 void clearFlag (EnumType singleFlag) {
183 clearFlags (static_cast<Underlying> (singleFlag));
184 }
185
190 Underlying value() const {
191 return m_flags;
192 }
193
194 protected:
196 Underlying m_flags;
197 };
198}
199
200
201/* ========================================================================== */
A type-safe flags class for bitwise operations on enum values.
Definition flags.h:34
bool operator!=(const Flags &other)
Inequality comparison operator.
Definition flags.h:150
bool operator==(const Flags &other)
Equality comparison operator.
Definition flags.h:141
Flags(const Flags &original)
Copy constructor that creates a new Flags object from an existing one.
Definition flags.h:49
Flags & operator|=(const Flags &f)
Bitwise OR assignment operator. Combines this flags object with another using bitwise OR operation.
Definition flags.h:59
Flags(Underlying original=0)
Default constructor that initializes flags with a given value.
Definition flags.h:43
friend Flags operator|(const Flags &f1, const Flags &f2)
Bitwise OR operator for combining two Flags objects.
Definition flags.h:92
Flags & operator^=(const Flags &f)
Bitwise XOR assignment operator. Combines this flags object with another using bitwise XOR operation.
Definition flags.h:70
Flags operator~() const
Bitwise NOT operator that inverts all bits in the flags.
Definition flags.h:120
Underlying m_flags
The underlying storage for the flag bits.
Definition flags.h:196
Flags & operator&=(const Flags &f)
Bitwise AND assignment operator. Combines this flags object with another using bitwise AND operation.
Definition flags.h:81
void setFlag(EnumType singleFlag)
Sets a single flag using bitwise OR operation.
Definition flags.h:166
void clearFlags(Underlying flags)
Clears multiple flags using bitwise AND with complement.
Definition flags.h:174
void setFlags(Underlying flags)
Sets multiple flags using bitwise OR operation.
Definition flags.h:158
void clearFlag(EnumType singleFlag)
Clears a single flag using bitwise AND with complement.
Definition flags.h:182
Underlying Flags::* RestrictedBool
Restricted bool type for safe boolean conversion.
Definition flags.h:36
friend Flags operator^(const Flags &f1, const Flags &f2)
Bitwise XOR operator for combining two Flags objects.
Definition flags.h:102
friend Flags operator&(const Flags &f1, const Flags &f2)
Bitwise AND operator for combining two Flags objects.
Definition flags.h:112
Underlying value() const
Gets the raw underlying value of the flags.
Definition flags.h:190
Global namespace for Piduino.
Definition board.h:28