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