Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qflags.h
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#ifndef QFLAGS_H
6#define QFLAGS_H
7
8#include <QtCore/qcompare_impl.h>
9#include <QtCore/qtypeinfo.h>
10
11#include <algorithm>
12#include <initializer_list>
13
14QT_BEGIN_NAMESPACE
15
16template<typename Enum> class QFlags;
17
18class QFlag
19{
20 int i;
21public:
22 constexpr inline Q_IMPLICIT QFlag(int value) noexcept : i(value) {}
23 constexpr inline Q_IMPLICIT operator int() const noexcept { return i; }
24
25#if !defined(Q_CC_MSVC)
26 // Microsoft Visual Studio has buggy behavior when it comes to
27 // unsigned enums: even if the enum is unsigned, the enum tags are
28 // always signed
29# if !defined(__LP64__) && !defined(Q_QDOC)
30 constexpr inline Q_IMPLICIT QFlag(long value) noexcept : i(int(value)) {}
31 constexpr inline Q_IMPLICIT QFlag(ulong value) noexcept : i(int(long(value))) {}
32# endif
33 constexpr inline Q_IMPLICIT QFlag(uint value) noexcept : i(int(value)) {}
34 constexpr inline Q_IMPLICIT QFlag(short value) noexcept : i(int(value)) {}
35 constexpr inline Q_IMPLICIT QFlag(ushort value) noexcept : i(int(uint(value))) {}
36 constexpr inline Q_IMPLICIT operator uint() const noexcept { return uint(i); }
37#endif
38};
40
42{
43 int i;
44public:
45 constexpr inline explicit QIncompatibleFlag(int i) noexcept;
46 constexpr inline Q_IMPLICIT operator int() const noexcept { return i; }
47};
49
50constexpr inline QIncompatibleFlag::QIncompatibleFlag(int value) noexcept : i(value) {}
51
52namespace QtPrivate {
53template <typename T> struct IsQFlags : std::false_type {};
54template <typename E> struct IsQFlags<QFlags<E>> : std::true_type {};
55
56template<typename Enum>
58{
59 static_assert(sizeof(Enum) <= sizeof(quint64),
60 "Only enumerations 64 bits or smaller are supported.");
61 static_assert((std::is_enum<Enum>::value), "QFlags is only usable on enumeration types.");
62
63 static constexpr size_t IntegerSize = (std::max)(sizeof(Enum), sizeof(int));
65
66protected:
67 typedef typename std::conditional<
69 typename Integers::Unsigned,
70 typename Integers::Signed
72
73 Int i = 0;
74
76public:
77 constexpr inline QFlagsStorage() noexcept = default;
78 constexpr inline explicit QFlagsStorage(std::in_place_t, Int v) : i(v) {}
79};
80
81template <typename Enum, int Size = sizeof(QFlagsStorage<Enum>)>
88template <typename Enum> struct QFlagsStorageHelper<Enum, sizeof(int)> : QFlagsStorage<Enum>
89{
90 using QFlagsStorage<Enum>::QFlagsStorage;
91
92 // For compatibility with Qt 3, moc goes through QFlag in order to
93 // read/write properties of type QFlags; so a conversion to QFlag is also
94 // needed here. (It otherwise goes through a QFlags->int->QFlag conversion
95 // sequence.)
96 constexpr inline Q_IMPLICIT QFlagsStorageHelper(QFlag flag) noexcept
98#ifdef QT_TYPESAFE_FLAGS
99 constexpr inline explicit operator QFlag() const noexcept { return QFlag(this->i); }
100#endif
101protected:
103};
104} // namespace QtPrivate
105
106template<typename Enum>
108{
110public:
111 typedef Enum enum_type;
112 using Int = typename Base::Int;
113 using Base::Base;
114
115 // compiler-generated copy/move ctor/assignment operators are fine!
116 constexpr inline QFlags() noexcept = default;
117
118 constexpr inline Q_IMPLICIT QFlags(Enum flags) noexcept : Base(std::in_place, Int(flags)) {}
119
120#ifdef Q_QDOC
121 constexpr inline Q_IMPLICIT QFlags(std::in_place_t, Int flags) noexcept;
122 constexpr inline Q_IMPLICIT QFlags(QFlag flag) noexcept
123 requires(sizeof(Enum) == sizeof(int));
124#endif
125
126 constexpr inline QFlags(std::initializer_list<Enum> flags) noexcept
128
129 constexpr static inline QFlags fromInt(Int i) noexcept { return QFlags(std::in_place, i); }
130 constexpr inline Int toInt() const noexcept { return i; }
131
132#ifndef QT_TYPESAFE_FLAGS
133 constexpr inline QFlags &operator&=(int mask) noexcept { i &= mask; return *this; }
134 constexpr inline QFlags &operator&=(uint mask) noexcept { i &= mask; return *this; }
135#endif
136 constexpr inline QFlags &operator&=(QFlags mask) noexcept { i &= mask.i; return *this; }
137 constexpr inline QFlags &operator&=(Enum mask) noexcept { i &= Int(mask); return *this; }
138 constexpr inline QFlags &operator|=(QFlags other) noexcept { i |= other.i; return *this; }
139 constexpr inline QFlags &operator|=(Enum other) noexcept { i |= Int(other); return *this; }
140 constexpr inline QFlags &operator^=(QFlags other) noexcept { i ^= other.i; return *this; }
141 constexpr inline QFlags &operator^=(Enum other) noexcept { i ^= Int(other); return *this; }
142
143#ifdef QT_TYPESAFE_FLAGS
144 constexpr inline explicit operator Int() const noexcept { return i; }
145 constexpr inline explicit operator bool() const noexcept { return i; }
146#else
147 constexpr inline Q_IMPLICIT operator Int() const noexcept { return i; }
148 constexpr inline bool operator!() const noexcept { return !i; }
149#endif
150
151 constexpr inline QFlags operator|(QFlags other) const noexcept { return QFlags(std::in_place, i | other.i); }
152 constexpr inline QFlags operator|(Enum other) const noexcept { return QFlags(std::in_place, i | Int(other)); }
153 constexpr inline QFlags operator^(QFlags other) const noexcept { return QFlags(std::in_place, i ^ other.i); }
154 constexpr inline QFlags operator^(Enum other) const noexcept { return QFlags(std::in_place, i ^ Int(other)); }
155#ifndef QT_TYPESAFE_FLAGS
156 constexpr inline QFlags operator&(int mask) const noexcept { return QFlags(std::in_place, i & mask); }
157 constexpr inline QFlags operator&(uint mask) const noexcept { return QFlags(std::in_place, i & mask); }
158#endif
159 constexpr inline QFlags operator&(QFlags other) const noexcept { return QFlags(std::in_place, i & other.i); }
160 constexpr inline QFlags operator&(Enum other) const noexcept { return QFlags(std::in_place, i & Int(other)); }
161 constexpr inline QFlags operator~() const noexcept { return QFlags(std::in_place, ~i); }
162
163 constexpr inline void operator+(QFlags other) const noexcept = delete;
164 constexpr inline void operator+(Enum other) const noexcept = delete;
165 constexpr inline void operator+(int other) const noexcept = delete;
166 constexpr inline void operator-(QFlags other) const noexcept = delete;
167 constexpr inline void operator-(Enum other) const noexcept = delete;
168 constexpr inline void operator-(int other) const noexcept = delete;
169
170 constexpr inline bool testFlag(Enum flag) const noexcept { return testFlags(flag); }
171 constexpr inline bool testFlags(QFlags flags) const noexcept { return flags.i ? ((i & flags.i) == flags.i) : i == Int(0); }
172 constexpr inline bool testAnyFlag(Enum flag) const noexcept { return testAnyFlags(flag); }
173 constexpr inline bool testAnyFlags(QFlags flags) const noexcept { return (i & flags.i) != Int(0); }
174 constexpr inline QFlags &setFlag(Enum flag, bool on = true) noexcept
175 {
176 return on ? (*this |= flag) : (*this &= ~QFlags(flag));
177 }
178
179 friend constexpr inline bool operator==(QFlags lhs, QFlags rhs) noexcept
180 { return lhs.i == rhs.i; }
181 friend constexpr inline bool operator!=(QFlags lhs, QFlags rhs) noexcept
182 { return lhs.i != rhs.i; }
183 friend constexpr inline bool operator==(QFlags lhs, Enum rhs) noexcept
184 { return lhs == QFlags(rhs); }
185 friend constexpr inline bool operator!=(QFlags lhs, Enum rhs) noexcept
186 { return lhs != QFlags(rhs); }
187 friend constexpr inline bool operator==(Enum lhs, QFlags rhs) noexcept
188 { return QFlags(lhs) == rhs; }
189 friend constexpr inline bool operator!=(Enum lhs, QFlags rhs) noexcept
190 { return QFlags(lhs) != rhs; }
191
192#ifdef QT_TYPESAFE_FLAGS
193 // Provide means of comparing flags against a literal 0; opt-in
194 // because otherwise they're ambiguous against operator==(int,int)
195 // after a QFlags->int conversion.
196 friend constexpr inline bool operator==(QFlags flags, QtPrivate::CompareAgainstLiteralZero) noexcept
197 { return flags.i == Int(0); }
198 friend constexpr inline bool operator!=(QFlags flags, QtPrivate::CompareAgainstLiteralZero) noexcept
199 { return flags.i != Int(0); }
200 friend constexpr inline bool operator==(QtPrivate::CompareAgainstLiteralZero, QFlags flags) noexcept
201 { return Int(0) == flags.i; }
202 friend constexpr inline bool operator!=(QtPrivate::CompareAgainstLiteralZero, QFlags flags) noexcept
203 { return Int(0) != flags.i; }
204#endif
205
206private:
207 constexpr static inline Int initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
208 typename std::initializer_list<Enum>::const_iterator end)
209 noexcept
210 {
211 return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
212 }
213
214 using Base::i;
215};
216
217#ifndef Q_MOC_RUN
218#define Q_DECLARE_FLAGS(Flags, Enum)typedef
219 QFlags<Enum> Flags;
220#endif
221
222#ifdef QT_TYPESAFE_FLAGS
223
224// These are opt-in, for backwards compatibility
225#define QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags) \
226[[maybe_unused]]constexpr
227 inline Flags operator~(Flags::enum_type e) noexcept \
228{return ~Flags(e); } \
229[[maybe_unused]]constexpr
230 inline void operator|(Flags::enum_type f1, int f2) noexcept = delete;
231#else
232#define QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags) \
233[[maybe_unused]]constexpr
234 inline QIncompatibleFlag operator|(Flags::enum_type f1, int f2) noexcept \
235{return QIncompatibleFlag(int(f1) | f2); }
236#endif
237
238#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags) \
239[[maybe_unused]]constexpr
240 inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, Flags::enum_type f2) noexcept \
241{return QFlags<Flags::enum_type>(f1) | f2; } \
242[[maybe_unused]]constexpr
243 inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
244{return f2 | f1; } \
245[[maybe_unused]]constexpr
246 inline QFlags<Flags::enum_type> operator&(Flags::enum_type f1, Flags::enum_type f2) noexcept \
247{return QFlags<Flags::enum_type>(f1) & f2; } \
248[[maybe_unused]]constexpr
249 inline QFlags<Flags::enum_type> operator&(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
250{return f2 & f1; } \
251[[maybe_unused]]constexpr
252 inline QFlags<Flags::enum_type> operator^(Flags::enum_type f1, Flags::enum_type f2) noexcept \
253{return QFlags<Flags::enum_type>(f1) ^ f2; } \
254[[maybe_unused]]constexpr
255 inline QFlags<Flags::enum_type> operator^(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
256{return f2 ^ f1; } constexpr
257 inline void operator+(Flags::enum_type f1, Flags::enum_type f2) noexcept = delete; constexpr
258 inline void operator+(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept = delete; constexpr
259 inline void operator+(int f1, QFlags<Flags::enum_type> f2) noexcept = delete; constexpr
260 inline void operator-(Flags::enum_type f1, Flags::enum_type f2) noexcept = delete; constexpr
261 inline void operator-(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept = delete; constexpr
262 inline void operator-(int f1, QFlags<Flags::enum_type> f2) noexcept = delete; constexpr
263 inline void operator+(int f1, Flags::enum_type f2) noexcept = delete; constexpr
264 inline void operator+(Flags::enum_type f1, int f2) noexcept = delete; constexpr
265 inline void operator-(int f1, Flags::enum_type f2) noexcept = delete; constexpr
266 inline void operator-(Flags::enum_type f1, int f2) noexcept = delete; QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM
267 (Flags)
268
269// restore bit-wise enum-enum operators deprecated in C++20,
270// but used in a few places in the API
271#if __cplusplus > 201702L // assume compilers don't warn if in C++17 mode
272 // in C++20 mode, provide user-defined operators to override the deprecated operations:
273# define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS)
274 [[maybe_unused]]
275 constexpr inline Ret operator op (LHS lhs, RHS rhs) noexcept
276 { return static_cast<Ret>(qToUnderlying(lhs) op qToUnderlying(rhs)); }
277 /* end */
278#else
279 // in C++17 mode, statically-assert that this compiler's result of the
280 // operation is the same that the C++20 version would produce:
281# define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS)
282 static_assert(std::is_same_v<decltype(std::declval<LHS>() op std::declval<RHS>()), Ret>);
283#endif
284
285#define Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum)
286 Q_DECLARE_MIXED_ENUM_OPERATOR(|, Ret, Flags, Enum)
287 Q_DECLARE_MIXED_ENUM_OPERATOR(&, Ret, Flags, Enum)
288 Q_DECLARE_MIXED_ENUM_OPERATOR(^, Ret, Flags, Enum)
289 /* end */
290
291#define Q_DECLARE_MIXED_ENUM_OPERATORS_SYMMETRIC(Ret, Flags, Enum)
292 Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum)
293 Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Enum, Flags)
294 /* end */
295
296
297QT_END_NAMESPACE
298
299#endif // QFLAGS_H
Definition qflags.h:19
constexpr QFlags(std::initializer_list< Enum > flags) noexcept
Definition qflags.h:126
constexpr QFlags & operator&=(Enum mask) noexcept
Definition qflags.h:137
constexpr QFlags & operator&=(int mask) noexcept
Definition qflags.h:133
constexpr void operator+(int other) const noexcept=delete
constexpr QFlags & operator|=(QFlags other) noexcept
Definition qflags.h:138
constexpr Int toInt() const noexcept
Definition qflags.h:130
constexpr QFlags & operator&=(uint mask) noexcept
Definition qflags.h:134
friend constexpr bool operator!=(Enum lhs, QFlags rhs) noexcept
Definition qflags.h:189
friend constexpr bool operator==(Enum lhs, QFlags rhs) noexcept
Definition qflags.h:187
constexpr void operator-(int other) const noexcept=delete
constexpr bool testFlag(Enum flag) const noexcept
Definition qflags.h:170
constexpr QFlags & operator^=(Enum other) noexcept
Definition qflags.h:141
friend constexpr bool operator==(QFlags lhs, Enum rhs) noexcept
Definition qflags.h:183
constexpr bool testAnyFlag(Enum flag) const noexcept
Definition qflags.h:172
constexpr bool testAnyFlags(QFlags flags) const noexcept
Definition qflags.h:173
constexpr void operator+(Enum other) const noexcept=delete
friend constexpr bool operator!=(QFlags lhs, QFlags rhs) noexcept
Definition qflags.h:181
constexpr QFlags & operator&=(QFlags mask) noexcept
Definition qflags.h:136
constexpr QFlags & operator|=(Enum other) noexcept
Definition qflags.h:139
constexpr void operator-(Enum other) const noexcept=delete
friend constexpr bool operator!=(QFlags lhs, Enum rhs) noexcept
Definition qflags.h:185
friend constexpr bool operator==(QFlags lhs, QFlags rhs) noexcept
Definition qflags.h:179
constexpr void operator-(QFlags other) const noexcept=delete
static constexpr QFlags fromInt(Int i) noexcept
Definition qflags.h:129
constexpr QFlags & operator^=(QFlags other) noexcept
Definition qflags.h:140
constexpr QFlags & setFlag(Enum flag, bool on=true) noexcept
Definition qflags.h:174
constexpr bool testFlags(QFlags flags) const noexcept
Definition qflags.h:171
Enum enum_type
Definition qflags.h:111
constexpr QFlags() noexcept=default
constexpr QIncompatibleFlag(int i) noexcept
Definition qflags.h:50
constexpr QFlagsStorage(std::in_place_t, Int v)
Definition qflags.h:78
std::conditional< std::is_unsigned< typenamestd::underlying_type< Enum >::type >::value, typenameIntegers::Unsigned, typenameIntegers::Signed >::type Int
Definition qflags.h:71
Q_DECLARE_TYPEINFO(QIncompatibleFlag, Q_PRIMITIVE_TYPE)
#define Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum)
Definition qflags.h:285
Q_DECLARE_TYPEINFO(QFlag, Q_PRIMITIVE_TYPE)
#define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS)
Definition qflags.h:281