Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Flags.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 Julien Bernard
4 *
5 * This software is provided 'as-is', without any express or implied
6 * warranty. In no event will the authors be held liable for any damages
7 * arising from the use of this software.
8 *
9 * Permission is granted to anyone to use this software for any purpose,
10 * including commercial applications, and to alter it and redistribute it
11 * freely, subject to the following restrictions:
12 *
13 * 1. The origin of this software must not be misrepresented; you must not
14 * claim that you wrote the original software. If you use this software
15 * in a product, an acknowledgment in the product documentation would be
16 * appreciated but is not required.
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 * 3. This notice may not be removed or altered from any source distribution.
20 */
21#ifndef GF_FLAGS_H
22#define GF_FLAGS_H
23
24#include <type_traits>
25
26#include "Types.h"
27
28namespace gf {
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
30inline namespace v1 {
31#endif
32 class Deserializer;
33 class Serializer;
34
47 template<typename E>
48 class Flags {
49 public:
53 Flags() = default;
54
58 constexpr Flags(NoneType)
59 : m_data(0)
60 {
61
62 }
63
67 constexpr Flags(AllType)
68 : m_data(static_cast<Type>(~0))
69 {
70
71 }
72
78 constexpr Flags(E e)
79 : m_data(static_cast<Type>(e))
80 {
81
82 }
83
89 constexpr Flags<E> operator~() const {
90 return Flags(~m_data);
91 }
92
99 constexpr Flags operator|(Flags flags) const {
100 return Flags(m_data | flags.m_data);
101 }
102
110 m_data |= flags.m_data;
111 return *this;
112 }
113
120 constexpr Flags operator&(Flags flags) const {
121 return Flags(m_data & flags.m_data);
122 }
123
131 m_data &= flags.m_data;
132 return *this;
133 }
134
140 constexpr operator bool() const {
141 return m_data != 0;
142 }
143
150 constexpr bool test(E flag) const {
151 return (m_data & static_cast<Type>(flag)) != 0;
152 }
153
159 void set(E flag) {
160 m_data |= static_cast<Type>(flag);
161 }
162
168 void reset(E flag) {
169 m_data &= ~static_cast<Type>(flag);
170 }
171
175 using Type = typename std::underlying_type<E>::type;
176
184 Type getValue() const {
185 return m_data;
186 }
187
188 template<typename T>
190 template<typename T>
192
193 private:
194 constexpr Flags(Type data)
195 : m_data(data)
196 {
197
198 }
199
200 Type m_data;
201 };
202
211 template<typename E>
212 constexpr
214 return lhs | Flags<E>(rhs);
215 }
216
225 template<typename E>
226 constexpr
228 return Flags<E>(lhs) | rhs;
229 }
230
239 template<typename E>
240 constexpr
242 return lhs & Flags<E>(rhs);
243 }
244
253 template<typename E>
254 constexpr
256 return Flags<E>(lhs) & rhs;
257 }
258
259
267 template<typename E>
268 constexpr Flags<E> combineFlags(E flag) {
269 return Flags<E>(flag);
270 }
271
280 template<typename E, typename ... F>
281 constexpr Flags<E> combineFlags(E flag, F ... others) {
282 return Flags<E>(flag) | combineFlags(others ...);
283 }
284
285#ifndef DOXYGEN_SHOULD_SKIP_THIS
286}
287
288
289// this traits is not versioned
290
291template<typename E>
292struct EnableBitmaskOperators {
293 static constexpr bool value = false;
294};
295
296// these overloads are only available to gf enum types and gf flags
297// unless you add: "using gf::operator|;"
298
299template<typename E>
300constexpr
301typename std::enable_if<EnableBitmaskOperators<E>::value, gf::Flags<E>>::type
302operator|(E lhs, E rhs) {
303 return gf::Flags<E>(lhs) | gf::Flags<E>(rhs);
304}
305
306template<typename E>
307constexpr
308typename std::enable_if<EnableBitmaskOperators<E>::value, gf::Flags<E>>::type
309operator&(E lhs, E rhs) {
310 return gf::Flags<E>(lhs) & gf::Flags<E>(rhs);
311}
312
313template<typename E>
314constexpr
315typename std::enable_if<EnableBitmaskOperators<E>::value, gf::Flags<E>>::type
316operator~(E val) {
317 return ~gf::Flags<E>(val);
318}
319
320#endif
321}
322
323#endif // GF_FLAGS_H
A deserializer from a binary file.
Definition: Serialization.h:151
Bitfield relying on an enumeration.
Definition: Flags.h:48
constexpr Flags< E > combineFlags(E flag, F ... others)
Combine several enum values into a flag value.
Definition: Flags.h:281
constexpr Flags(E e)
Constructor with an enum value.
Definition: Flags.h:78
Flags< E > & operator|=(Flags< E > flags)
Binary OR and assignment.
Definition: Flags.h:109
constexpr Flags(NoneType)
Constructor with no flag set.
Definition: Flags.h:58
typename std::underlying_type< E >::type Type
The underlying type of the enum.
Definition: Flags.h:175
Flags()=default
Default constructor.
friend Deserializer & operator|(Deserializer &ar, Flags< T > &data)
constexpr Flags< E > operator|(Flags< E > lhs, E rhs)
Binary OR between a bitfield and a flag.
Definition: Flags.h:213
Type getValue() const
Get the underlying value of the flags.
Definition: Flags.h:184
constexpr Flags< E > operator~() const
Binary NOT operator.
Definition: Flags.h:89
constexpr Flags< E > operator&(E lhs, Flags< E > rhs)
Binary AND between a flag and a bitfield.
Definition: Flags.h:255
void set(E flag)
Set a flag.
Definition: Flags.h:159
constexpr Flags< E > operator|(E lhs, Flags< E > rhs)
Binary OR between a flag and a bitfield.
Definition: Flags.h:227
Flags< E > & operator&=(Flags< E > flags)
Binary AND and assignment.
Definition: Flags.h:130
constexpr Flags< E > operator&(Flags< E > lhs, E rhs)
Binary AND between a bitfield and a flag.
Definition: Flags.h:241
constexpr Flags operator|(Flags flags) const
Binary OR between two bitfields.
Definition: Flags.h:99
friend Serializer & operator|(Serializer &ar, Flags< T > data)
constexpr Flags operator&(Flags flags) const
Binary AND between two bitfields.
Definition: Flags.h:120
void reset(E flag)
Reset a flag.
Definition: Flags.h:168
constexpr Flags(AllType)
Constructor with all flags set.
Definition: Flags.h:67
constexpr bool test(E flag) const
Test if a specified flag is set.
Definition: Flags.h:150
constexpr Flags< E > combineFlags(E flag)
Combine a single enum value into a flag value.
Definition: Flags.h:268
A serializer to a binary file.
Definition: Serialization.h:43
The namespace for gf classes.
Semantic type to represent "all".
Definition: Types.h:53
Semantic type to represent "none".
Definition: Types.h:37