OpenTwin 0.1
OpenTwin
 
Loading...
Searching...
No Matches
Flags.h
Go to the documentation of this file.
1
3// ###########################################################################################################################################################################################################################################################################################################################
4
5#pragma once
6
7// OpenTwin header
10
11// std header
12#include <type_traits>
13
16#define OT_ADD_FLAG_FUNCTIONS_IMPL(___prefix, ___castType, ___enumName) ___prefix constexpr ___enumName operator | (___enumName _lhv, ___enumName _rhv) { return static_cast<___enumName>(static_cast<___castType>(_lhv) | static_cast<___castType>(_rhv)); }; \
17___prefix constexpr ___enumName operator & (___enumName _lhv, ___enumName _rhv) { return static_cast<___enumName>(static_cast<___castType>(_lhv) & static_cast<___castType>(_rhv)); }; \
18___prefix constexpr ___enumName operator ~ (___enumName _lhv) { return static_cast<___enumName>(~(static_cast<___castType>(_lhv))); };
19
20#ifdef OT_OS_64Bit
21
47#define OT_ADD_FLAG_FUNCTIONS(___enumName) OT_ADD_FLAG_FUNCTIONS_IMPL(, long long, ___enumName)
48
100#define OT_ADD_FRIEND_FLAG_FUNCTIONS(___enumName) OT_ADD_FLAG_FUNCTIONS_IMPL(friend , long long, ___enumName)
101
102#else
103
129#define OT_ADD_FLAG_FUNCTIONS(___enumName) OT_ADD_FLAG_FUNCTIONS_IMPL(, long, ___enumName)
130
182#define OT_ADD_FRIEND_FLAG_FUNCTIONS(___enumName) OT_ADD_FLAG_FUNCTIONS_IMPL(friend , long, ___enumName)
183
184#endif
185
186namespace ot {
187
214 template<typename T> class OT_CORE_API_EXPORTONLY Flags {
215 private:
216#ifdef OT_OS_64Bit
219 typedef long long FlagCastType;
220#else
223 typedef long FlagCastType;
224#endif // _WIN64
225
226 static_assert((sizeof(T) <= sizeof(FlagCastType)), "Flags type may overflow.");
227 static_assert((std::is_enum<T>::value), "Flags accept only enumeration types.");
228
229 T m_data;
230
231 public:
234 inline Flags() : m_data{ static_cast<T>(0) } {};
235
238 inline Flags(T _initialData) : m_data{ _initialData } {}
239
242 inline Flags(const Flags<T>& _other) : m_data{ _other.data() } {};
243
246 inline Flags(Flags&& _other) noexcept : m_data(std::move(_other.m_data)) {};
247
249 inline T data(void) const { return m_data; };
250
253 inline void set(T _data) { m_data = _data; };
254
257 inline void setFlag(T _flag) { m_data = m_data | _flag; };
258
262 inline void setFlag(T _flag, bool _flagIsSet) {
263 if (_flagIsSet) { m_data = m_data | _flag; }
264 else { m_data = m_data & (~_flag); }
265 };
266
269 inline void removeFlag(T _flag) { m_data = m_data & (~_flag); };
270
273 inline bool flagIsSet(T _flag) const { return (m_data & _flag); };
274
276 inline operator T(void) const { return m_data; };
277
279 inline operator bool(void) const { return (bool)m_data; };
280
281 inline Flags<T>& operator = (T _flag) { m_data = _flag; return *this; };
282 inline Flags<T>& operator = (const Flags<T>& _other) { m_data = _other.m_data; return *this; };
283 inline Flags<T>& operator = (Flags<T>&& _other) { m_data = std::move(_other.m_data); return *this; };
284
285 inline Flags<T> operator | (T _flag) const { return Flags<T>{ m_data | _flag }; };
286 inline Flags<T> operator | (const Flags<T>& _other) const { return Flags<T>{ _other.m_data | m_data }; };
287
288 inline Flags<T> operator & (T _flag) const { return Flags<T>{ m_data& _flag }; };
289 inline Flags<T> operator & (const Flags<T>& _other) const { return Flags<T>{ _other.m_data& m_data }; };
290
291 inline Flags<T>& operator |= (T _flag) { m_data = m_data | _flag; return *this; };
292 inline Flags<T>& operator |= (const Flags<T>& _other) { m_data = m_data | _other.m_data; return *this; };
293
294 inline Flags<T>& operator &= (T _flag) { m_data = m_data & _flag; return *this; };
295 inline Flags<T>& operator &= (const Flags<T>& _other) { m_data = m_data & _other.m_data; return *this; };
296
297 inline Flags<T> operator ~(void) const { return Flags<T>{ ~m_data }; };
298
299 inline bool operator == (T _flag) const { return m_data == _flag; };
300 inline bool operator == (const Flags<T>& _other) const { return m_data == _other.m_data; };
301
302 inline bool operator != (T _flag) const { return m_data != _flag; };
303 inline bool operator != (const Flags<T>& _other) const { return m_data != _other.m_data; };
304 };
305
306}
The ArchitectureInfo contains multiple definitions that specify the current OS architecture.
#define OT_CORE_API_EXPORTONLY
Definition CoreAPIExport.h:11
bool operator==(const FaceSelection &left, const FaceSelection &right)
Definition Model.cpp:55
The Flags class is a wrapper around a enum that allows bitwise operations (flags)....
Definition Flags.h:214
Flags()
Data.
Definition Flags.h:234
Flags(const Flags< T > &_other)
Copy constructor.
Definition Flags.h:242
T data(void) const
Returns a copy of the data.
Definition Flags.h:249
Flags(Flags &&_other) noexcept
Move constructor.
Definition Flags.h:246
void removeFlag(T _flag)
Remove the provided flag.
Definition Flags.h:269
void setFlag(T _flag, bool _flagIsSet)
Set or remove the provided flag.
Definition Flags.h:262
Flags(T _initialData)
Assignment constructor.
Definition Flags.h:238
bool flagIsSet(T _flag) const
Returns true if the provided flag is set.
Definition Flags.h:273
void set(T _data)
Replace the current data.
Definition Flags.h:253
void setFlag(T _flag)
Set the provided flag.
Definition Flags.h:257
Definition Connector.h:8