Debian Codegraph @

src/openmpt/base/FlagSet.hpp

raw
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* SPDX-FileCopyrightText: OpenMPT Project Developers and Contributors */
3
4 /*
5 * Originally based on <http://stackoverflow.com/questions/4226960/type-safer-bitflags-in-c>.
6 * Rewritten to be standard-conforming.
7 */
8
9
10 #pragma once
11
12 #include "openmpt/all/BuildSettings.hpp"
13
14 #include "mpt/base/macros.hpp"
15
16 #include <type_traits>
17
18 #include <cstddef>
19
20
21 OPENMPT_NAMESPACE_BEGIN
22
23
24 // Type-safe wrapper around an enum, that can represent all enum values and bitwise compositions thereof.
25 // Conversions to and from plain integers as well as conversions to the base enum are always explicit.
26 template <typename enum_t>
27 // cppcheck-suppress copyCtorAndEqOperator
28 class enum_value_type
29 {
30 public:
31 using enum_type = enum_t;
32 using value_type = enum_value_type;
33 using store_type = typename std::make_unsigned<enum_t>::type;
34
35 private:
36 store_type bits;
37
38 public:
39 MPT_CONSTEXPRINLINE enum_value_type() noexcept
40 : bits(0)
41 {
42 }
43 MPT_CONSTEXPRINLINE enum_value_type(const enum_value_type &x) noexcept
44 : bits(x.bits)
45 {
46 }
47 MPT_CONSTEXPRINLINE enum_value_type(enum_type x) noexcept
48 : bits(static_cast<store_type>(x))
49 {
50 }
51
52 private:
53 explicit MPT_CONSTEXPRINLINE enum_value_type(store_type x) noexcept
54 : bits(x)
55 {
56 } // private in order to prevent accidental conversions. use from_bits.
57 MPT_CONSTEXPRINLINE operator store_type() const noexcept { return bits; } // private in order to prevent accidental conversions. use as_bits.
58 public:
59 static MPT_CONSTEXPRINLINE enum_value_type from_bits(store_type bits) noexcept { return value_type(bits); }
60 MPT_CONSTEXPRINLINE enum_type as_enum() const noexcept { return static_cast<enum_t>(bits); }
61 MPT_CONSTEXPRINLINE store_type as_bits() const noexcept { return bits; }
62
63 public:
64 MPT_CONSTEXPRINLINE operator bool() const noexcept { return bits != store_type(); }
65 MPT_CONSTEXPRINLINE bool operator!() const noexcept { return bits == store_type(); }
66
67 MPT_CONSTEXPRINLINE const enum_value_type operator~() const noexcept { return enum_value_type(~bits); }
68
69 friend MPT_CONSTEXPRINLINE bool operator==(enum_value_type a, enum_value_type b) noexcept { return a.bits == b.bits; }
70 friend MPT_CONSTEXPRINLINE bool operator!=(enum_value_type a, enum_value_type b) noexcept { return a.bits != b.bits; }
71
72 friend MPT_CONSTEXPRINLINE bool operator==(enum_value_type a, enum_t b) noexcept { return a == enum_value_type(b); }
73 friend MPT_CONSTEXPRINLINE bool operator!=(enum_value_type a, enum_t b) noexcept { return a != enum_value_type(b); }
74
75 friend MPT_CONSTEXPRINLINE bool operator==(enum_t a, enum_value_type b) noexcept { return enum_value_type(a) == b; }
76 friend MPT_CONSTEXPRINLINE bool operator!=(enum_t a, enum_value_type b) noexcept { return enum_value_type(a) != b; }
77
78 friend MPT_CONSTEXPRINLINE const enum_value_type operator|(enum_value_type a, enum_value_type b) noexcept { return enum_value_type(a.bits | b.bits); }
79 friend MPT_CONSTEXPRINLINE const enum_value_type operator&(enum_value_type a, enum_value_type b) noexcept { return enum_value_type(a.bits & b.bits); }
80 friend MPT_CONSTEXPRINLINE const enum_value_type operator^(enum_value_type a, enum_value_type b) noexcept { return enum_value_type(a.bits ^ b.bits); }
81
82 friend MPT_CONSTEXPRINLINE const enum_value_type operator|(enum_value_type a, enum_t b) noexcept { return a | enum_value_type(b); }
83 friend MPT_CONSTEXPRINLINE const enum_value_type operator&(enum_value_type a, enum_t b) noexcept { return a & enum_value_type(b); }
84 friend MPT_CONSTEXPRINLINE const enum_value_type operator^(enum_value_type a, enum_t b) noexcept { return a ^ enum_value_type(b); }
85
86 friend MPT_CONSTEXPRINLINE const enum_value_type operator|(enum_t a, enum_value_type b) noexcept { return enum_value_type(a) | b; }
87 friend MPT_CONSTEXPRINLINE const enum_value_type operator&(enum_t a, enum_value_type b) noexcept { return enum_value_type(a) & b; }
88 friend MPT_CONSTEXPRINLINE const enum_value_type operator^(enum_t a, enum_value_type b) noexcept { return enum_value_type(a) ^ b; }
89
90 MPT_CONSTEXPRINLINE enum_value_type &operator|=(enum_value_type b) noexcept
91 {
92 *this = *this | b;
93 return *this;
94 }
95 MPT_CONSTEXPRINLINE enum_value_type &operator&=(enum_value_type b) noexcept
96 {
97 *this = *this & b;
98 return *this;
99 }
100 MPT_CONSTEXPRINLINE enum_value_type &operator^=(enum_value_type b) noexcept
101 {
102 *this = *this ^ b;
103 return *this;
104 }
105
106 MPT_CONSTEXPRINLINE enum_value_type &operator|=(enum_t b) noexcept
107 {
108 *this = *this | b;
109 return *this;
110 }
111 MPT_CONSTEXPRINLINE enum_value_type &operator&=(enum_t b) noexcept
112 {
113 *this = *this & b;
114 return *this;
115 }
116 MPT_CONSTEXPRINLINE enum_value_type &operator^=(enum_t b) noexcept
117 {
118 *this = *this ^ b;
119 return *this;
120 }
121 };
122
123
124 // Type-safe enum wrapper that allows type-safe bitwise testing.
125 template <typename enum_t>
126 class Enum
127 {
128 public:
129 using self_type = Enum;
130 using enum_type = enum_t;
131 using value_type = enum_value_type<enum_t>;
132 using store_type = typename value_type::store_type;
133
134 private:
135 enum_type value;
136
137 public:
138 explicit MPT_CONSTEXPRINLINE Enum(enum_type val) noexcept
139 : value(val)
140 {
141 }
142 MPT_CONSTEXPRINLINE operator enum_type() const noexcept { return value; }
143 MPT_CONSTEXPRINLINE Enum &operator=(enum_type val) noexcept
144 {
145 value = val;
146 return *this;
147 }
148
149 public:
150 MPT_CONSTEXPRINLINE const value_type operator~() const { return ~value_type(value); }
151
152 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, self_type b) noexcept { return value_type(a) == value_type(b); }
153 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, self_type b) noexcept { return value_type(a) != value_type(b); }
154
155 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, value_type b) noexcept { return value_type(a) == value_type(b); }
156 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, value_type b) noexcept { return value_type(a) != value_type(b); }
157
158 friend MPT_CONSTEXPRINLINE bool operator==(value_type a, self_type b) noexcept { return value_type(a) == value_type(b); }
159 friend MPT_CONSTEXPRINLINE bool operator!=(value_type a, self_type b) noexcept { return value_type(a) != value_type(b); }
160
161 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, enum_type b) noexcept { return value_type(a) == value_type(b); }
162 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, enum_type b) noexcept { return value_type(a) != value_type(b); }
163
164 friend MPT_CONSTEXPRINLINE bool operator==(enum_type a, self_type b) noexcept { return value_type(a) == value_type(b); }
165 friend MPT_CONSTEXPRINLINE bool operator!=(enum_type a, self_type b) noexcept { return value_type(a) != value_type(b); }
166
167 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, self_type b) noexcept { return value_type(a) | value_type(b); }
168 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, self_type b) noexcept { return value_type(a) & value_type(b); }
169 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, self_type b) noexcept { return value_type(a) ^ value_type(b); }
170
171 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, value_type b) noexcept { return value_type(a) | value_type(b); }
172 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, value_type b) noexcept { return value_type(a) & value_type(b); }
173 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, value_type b) noexcept { return value_type(a) ^ value_type(b); }
174
175 friend MPT_CONSTEXPRINLINE const value_type operator|(value_type a, self_type b) noexcept { return value_type(a) | value_type(b); }
176 friend MPT_CONSTEXPRINLINE const value_type operator&(value_type a, self_type b) noexcept { return value_type(a) & value_type(b); }
177 friend MPT_CONSTEXPRINLINE const value_type operator^(value_type a, self_type b) noexcept { return value_type(a) ^ value_type(b); }
178
179 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, enum_type b) noexcept { return value_type(a) | value_type(b); }
180 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, enum_type b) noexcept { return value_type(a) & value_type(b); }
181 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, enum_type b) noexcept { return value_type(a) ^ value_type(b); }
182
183 friend MPT_CONSTEXPRINLINE const value_type operator|(enum_type a, self_type b) noexcept { return value_type(a) | value_type(b); }
184 friend MPT_CONSTEXPRINLINE const value_type operator&(enum_type a, self_type b) noexcept { return value_type(a) & value_type(b); }
185 friend MPT_CONSTEXPRINLINE const value_type operator^(enum_type a, self_type b) noexcept { return value_type(a) ^ value_type(b); }
186 };
187
188
189 template <typename enum_t, typename store_t = typename enum_value_type<enum_t>::store_type>
190 // cppcheck-suppress copyCtorAndEqOperator
191 class FlagSet
192 {
193 public:
194 using self_type = FlagSet;
195 using enum_type = enum_t;
196 using value_type = enum_value_type<enum_t>;
197 using store_type = store_t;
198
199 private:
200 // support truncated store_type ... :
201 store_type bits_;
202 static MPT_CONSTEXPRINLINE store_type store_from_value(value_type bits) noexcept { return static_cast<store_type>(bits.as_bits()); }
203 static MPT_CONSTEXPRINLINE value_type value_from_store(store_type bits) noexcept { return value_type::from_bits(static_cast<typename value_type::store_type>(bits)); }
204
205 MPT_CONSTEXPRINLINE FlagSet &store(value_type bits) noexcept
206 {
207 bits_ = store_from_value(bits);
208 return *this;
209 }
210 MPT_CONSTEXPRINLINE value_type load() const noexcept { return value_from_store(bits_); }
211
212 public:
213 // Default constructor (no flags set)
214 MPT_CONSTEXPRINLINE FlagSet() noexcept
215 : bits_(store_from_value(value_type()))
216 {
217 }
218
219 // Copy constructor
220 MPT_CONSTEXPRINLINE FlagSet(const FlagSet &flags) noexcept
221 : bits_(flags.bits_)
222 {
223 }
224
225 // Value constructor
226 MPT_CONSTEXPRINLINE FlagSet(value_type flags) noexcept
227 : bits_(store_from_value(value_type(flags)))
228 {
229 }
230
231 MPT_CONSTEXPRINLINE FlagSet(enum_type flag) noexcept
232 : bits_(store_from_value(value_type(flag)))
233 {
234 }
235
236 explicit MPT_CONSTEXPRINLINE FlagSet(store_type flags) noexcept
237 : bits_(store_from_value(value_type::from_bits(flags)))
238 {
239 }
240
241 MPT_CONSTEXPRINLINE explicit operator bool() const noexcept
242 {
243 return load();
244 }
245 MPT_CONSTEXPRINLINE explicit operator store_type() const noexcept
246 {
247 return load().as_bits();
248 }
249
250 MPT_CONSTEXPRINLINE value_type value() const noexcept
251 {
252 return load();
253 }
254
255 MPT_CONSTEXPRINLINE operator value_type() const noexcept
256 {
257 return load();
258 }
259
260 // Test if one or more flags are set. Returns true if at least one of the given flags is set.
261 MPT_CONSTEXPRINLINE bool operator[](value_type flags) const noexcept
262 {
263 return test(flags);
264 }
265
266 // Set one or more flags.
267 MPT_CONSTEXPRINLINE FlagSet &set(value_type flags) noexcept
268 {
269 return store(load() | flags);
270 }
271
272 // Set or clear one or more flags.
273 MPT_CONSTEXPRINLINE FlagSet &set(value_type flags, bool val) noexcept
274 {
275 return store((val ? (load() | flags) : (load() & ~flags)));
276 }
277
278 // Clear or flags.
279 MPT_CONSTEXPRINLINE FlagSet &reset() noexcept
280 {
281 return store(value_type());
282 }
283
284 // Clear one or more flags.
285 MPT_CONSTEXPRINLINE FlagSet &reset(value_type flags) noexcept
286 {
287 return store(load() & ~flags);
288 }
289
290 // Toggle all flags.
291 MPT_CONSTEXPRINLINE FlagSet &flip() noexcept
292 {
293 return store(~load());
294 }
295
296 // Toggle one or more flags.
297 MPT_CONSTEXPRINLINE FlagSet &flip(value_type flags) noexcept
298 {
299 return store(load() ^ flags);
300 }
301
302 // Returns the size of the flag set in bytes
303 MPT_CONSTEXPRINLINE std::size_t size() const noexcept
304 {
305 return sizeof(store_type);
306 }
307
308 // Returns the size of the flag set in bits
309 MPT_CONSTEXPRINLINE std::size_t size_bits() const noexcept
310 {
311 return size() * 8;
312 }
313
314 // Test if one or more flags are set. Returns true if at least one of the given flags is set.
315 MPT_CONSTEXPRINLINE bool test(value_type flags) const noexcept
316 {
317 return (load() & flags);
318 }
319
320 // Test if all specified flags are set.
321 MPT_CONSTEXPRINLINE bool test_all(value_type flags) const noexcept
322 {
323 return (load() & flags) == flags;
324 }
325
326 // Test if any but the specified flags are set.
327 MPT_CONSTEXPRINLINE bool test_any_except(value_type flags) const noexcept
328 {
329 return (load() & ~flags);
330 }
331
332 // Test if any flag is set.
333 MPT_CONSTEXPRINLINE bool any() const noexcept
334 {
335 return load();
336 }
337
338 // Test if no flags are set.
339 MPT_CONSTEXPRINLINE bool none() const noexcept
340 {
341 return !load();
342 }
343
344 MPT_CONSTEXPRINLINE store_type GetRaw() const noexcept
345 {
346 return bits_;
347 }
348
349 MPT_CONSTEXPRINLINE FlagSet &SetRaw(store_type flags) noexcept
350 {
351 bits_ = flags;
352 return *this;
353 }
354
355 MPT_CONSTEXPRINLINE FlagSet &operator=(value_type flags) noexcept
356 {
357 return store(flags);
358 }
359
360 MPT_CONSTEXPRINLINE FlagSet &operator=(enum_type flag) noexcept
361 {
362 return store(flag);
363 }
364
365 MPT_CONSTEXPRINLINE FlagSet &operator=(FlagSet flags) noexcept
366 {
367 return store(flags.load());
368 }
369
370 MPT_CONSTEXPRINLINE FlagSet &operator&=(value_type flags) noexcept
371 {
372 return store(load() & flags);
373 }
374
375 MPT_CONSTEXPRINLINE FlagSet &operator|=(value_type flags) noexcept
376 {
377 return store(load() | flags);
378 }
379
380 MPT_CONSTEXPRINLINE FlagSet &operator^=(value_type flags) noexcept
381 {
382 return store(load() ^ flags);
383 }
384
385 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, self_type b) noexcept { return a.load() == b.load(); }
386 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, self_type b) noexcept { return a.load() != b.load(); }
387
388 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, value_type b) noexcept { return a.load() == value_type(b); }
389 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, value_type b) noexcept { return a.load() != value_type(b); }
390
391 friend MPT_CONSTEXPRINLINE bool operator==(value_type a, self_type b) noexcept { return value_type(a) == b.load(); }
392 friend MPT_CONSTEXPRINLINE bool operator!=(value_type a, self_type b) noexcept { return value_type(a) != b.load(); }
393
394 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, enum_type b) noexcept { return a.load() == value_type(b); }
395 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, enum_type b) noexcept { return a.load() != value_type(b); }
396
397 friend MPT_CONSTEXPRINLINE bool operator==(enum_type a, self_type b) noexcept { return value_type(a) == b.load(); }
398 friend MPT_CONSTEXPRINLINE bool operator!=(enum_type a, self_type b) noexcept { return value_type(a) != b.load(); }
399
400 friend MPT_CONSTEXPRINLINE bool operator==(self_type a, Enum<enum_type> b) noexcept { return a.load() == value_type(b); }
401 friend MPT_CONSTEXPRINLINE bool operator!=(self_type a, Enum<enum_type> b) noexcept { return a.load() != value_type(b); }
402
403 friend MPT_CONSTEXPRINLINE bool operator==(Enum<enum_type> a, self_type b) noexcept { return value_type(a) == b.load(); }
404 friend MPT_CONSTEXPRINLINE bool operator!=(Enum<enum_type> a, self_type b) noexcept { return value_type(a) != b.load(); }
405
406 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, self_type b) noexcept { return a.load() | b.load(); }
407 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, self_type b) noexcept { return a.load() & b.load(); }
408 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, self_type b) noexcept { return a.load() ^ b.load(); }
409
410 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, value_type b) noexcept { return a.load() | value_type(b); }
411 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, value_type b) noexcept { return a.load() & value_type(b); }
412 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, value_type b) noexcept { return a.load() ^ value_type(b); }
413
414 friend MPT_CONSTEXPRINLINE const value_type operator|(value_type a, self_type b) noexcept { return value_type(a) | b.load(); }
415 friend MPT_CONSTEXPRINLINE const value_type operator&(value_type a, self_type b) noexcept { return value_type(a) & b.load(); }
416 friend MPT_CONSTEXPRINLINE const value_type operator^(value_type a, self_type b) noexcept { return value_type(a) ^ b.load(); }
417
418 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, enum_type b) noexcept { return a.load() | value_type(b); }
419 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, enum_type b) noexcept { return a.load() & value_type(b); }
420 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, enum_type b) noexcept { return a.load() ^ value_type(b); }
421
422 friend MPT_CONSTEXPRINLINE const value_type operator|(enum_type a, self_type b) noexcept { return value_type(a) | b.load(); }
423 friend MPT_CONSTEXPRINLINE const value_type operator&(enum_type a, self_type b) noexcept { return value_type(a) & b.load(); }
424 friend MPT_CONSTEXPRINLINE const value_type operator^(enum_type a, self_type b) noexcept { return value_type(a) ^ b.load(); }
425
426 friend MPT_CONSTEXPRINLINE const value_type operator|(self_type a, Enum<enum_type> b) noexcept { return a.load() | value_type(b); }
427 friend MPT_CONSTEXPRINLINE const value_type operator&(self_type a, Enum<enum_type> b) noexcept { return a.load() & value_type(b); }
428 friend MPT_CONSTEXPRINLINE const value_type operator^(self_type a, Enum<enum_type> b) noexcept { return a.load() ^ value_type(b); }
429
430 friend MPT_CONSTEXPRINLINE const value_type operator|(Enum<enum_type> a, self_type b) noexcept { return value_type(a) | b.load(); }
431 friend MPT_CONSTEXPRINLINE const value_type operator&(Enum<enum_type> a, self_type b) noexcept { return value_type(a) & b.load(); }
432 friend MPT_CONSTEXPRINLINE const value_type operator^(Enum<enum_type> a, self_type b) noexcept { return value_type(a) ^ b.load(); }
433 };
434
435
436 // Declare typesafe logical operators for enum_t
437 #define MPT_DECLARE_ENUM(enum_t) \
438 MPT_CONSTEXPRINLINE enum_value_type<enum_t> operator|(enum_t a, enum_t b) noexcept \
439 { \
440 return enum_value_type<enum_t>(a) | enum_value_type<enum_t>(b); \
441 } \
442 MPT_CONSTEXPRINLINE enum_value_type<enum_t> operator&(enum_t a, enum_t b) noexcept \
443 { \
444 return enum_value_type<enum_t>(a) & enum_value_type<enum_t>(b); \
445 } \
446 MPT_CONSTEXPRINLINE enum_value_type<enum_t> operator^(enum_t a, enum_t b) noexcept \
447 { \
448 return enum_value_type<enum_t>(a) ^ enum_value_type<enum_t>(b); \
449 } \
450 MPT_CONSTEXPRINLINE enum_value_type<enum_t> operator~(enum_t a) noexcept \
451 { \
452 return ~enum_value_type<enum_t>(a); \
453 } \
454 /**/
455
456 // backwards compatibility
457 #define DECLARE_FLAGSET MPT_DECLARE_ENUM
458
459
460 OPENMPT_NAMESPACE_END