File indexing completed on 2025-01-18 10:07:23
0001
0002
0003
0004 #ifndef QFLAGS_H
0005 #define QFLAGS_H
0006
0007 #include <QtCore/qcompare_impl.h>
0008 #include <QtCore/qtypeinfo.h>
0009
0010 #include <initializer_list>
0011
0012 QT_BEGIN_NAMESPACE
0013
0014 class QDataStream;
0015
0016 class QFlag
0017 {
0018 int i;
0019 public:
0020 constexpr inline Q_IMPLICIT QFlag(int value) noexcept : i(value) {}
0021 constexpr inline Q_IMPLICIT operator int() const noexcept { return i; }
0022
0023 #if !defined(Q_CC_MSVC)
0024
0025
0026
0027 # if !defined(__LP64__) && !defined(Q_QDOC)
0028 constexpr inline Q_IMPLICIT QFlag(long value) noexcept : i(int(value)) {}
0029 constexpr inline Q_IMPLICIT QFlag(ulong value) noexcept : i(int(long(value))) {}
0030 # endif
0031 constexpr inline Q_IMPLICIT QFlag(uint value) noexcept : i(int(value)) {}
0032 constexpr inline Q_IMPLICIT QFlag(short value) noexcept : i(int(value)) {}
0033 constexpr inline Q_IMPLICIT QFlag(ushort value) noexcept : i(int(uint(value))) {}
0034 constexpr inline Q_IMPLICIT operator uint() const noexcept { return uint(i); }
0035 #endif
0036 };
0037 Q_DECLARE_TYPEINFO(QFlag, Q_PRIMITIVE_TYPE);
0038
0039 class QIncompatibleFlag
0040 {
0041 int i;
0042 public:
0043 constexpr inline explicit QIncompatibleFlag(int i) noexcept;
0044 constexpr inline Q_IMPLICIT operator int() const noexcept { return i; }
0045 };
0046 Q_DECLARE_TYPEINFO(QIncompatibleFlag, Q_PRIMITIVE_TYPE);
0047
0048 constexpr inline QIncompatibleFlag::QIncompatibleFlag(int value) noexcept : i(value) {}
0049
0050
0051 template<typename Enum>
0052 class QFlags
0053 {
0054 static_assert((sizeof(Enum) <= sizeof(int)),
0055 "QFlags uses an int as storage, so an enum with underlying "
0056 "long long will overflow.");
0057 static_assert((std::is_enum<Enum>::value), "QFlags is only usable on enumeration types.");
0058
0059 public:
0060 #if defined(Q_CC_MSVC) || defined(Q_QDOC)
0061
0062
0063 typedef int Int;
0064 #else
0065 typedef typename std::conditional<
0066 std::is_unsigned<typename std::underlying_type<Enum>::type>::value,
0067 unsigned int,
0068 signed int
0069 >::type Int;
0070 #endif
0071 typedef Enum enum_type;
0072
0073 constexpr inline QFlags() noexcept : i(0) {}
0074 constexpr inline Q_IMPLICIT QFlags(Enum flags) noexcept : i(Int(flags)) {}
0075 constexpr inline Q_IMPLICIT QFlags(QFlag flag) noexcept : i(flag) {}
0076
0077 constexpr inline QFlags(std::initializer_list<Enum> flags) noexcept
0078 : i(initializer_list_helper(flags.begin(), flags.end())) {}
0079
0080 constexpr static inline QFlags fromInt(Int i) noexcept { return QFlags(QFlag(i)); }
0081 constexpr inline Int toInt() const noexcept { return i; }
0082
0083 #ifndef QT_TYPESAFE_FLAGS
0084 constexpr inline QFlags &operator&=(int mask) noexcept { i &= mask; return *this; }
0085 constexpr inline QFlags &operator&=(uint mask) noexcept { i &= mask; return *this; }
0086 #endif
0087 constexpr inline QFlags &operator&=(QFlags mask) noexcept { i &= mask.i; return *this; }
0088 constexpr inline QFlags &operator&=(Enum mask) noexcept { i &= Int(mask); return *this; }
0089 constexpr inline QFlags &operator|=(QFlags other) noexcept { i |= other.i; return *this; }
0090 constexpr inline QFlags &operator|=(Enum other) noexcept { i |= Int(other); return *this; }
0091 constexpr inline QFlags &operator^=(QFlags other) noexcept { i ^= other.i; return *this; }
0092 constexpr inline QFlags &operator^=(Enum other) noexcept { i ^= Int(other); return *this; }
0093
0094 #ifdef QT_TYPESAFE_FLAGS
0095 constexpr inline explicit operator Int() const noexcept { return i; }
0096 constexpr inline explicit operator bool() const noexcept { return i; }
0097
0098
0099
0100
0101 constexpr inline explicit operator QFlag() const noexcept { return QFlag(i); }
0102 #else
0103 constexpr inline Q_IMPLICIT operator Int() const noexcept { return i; }
0104 constexpr inline bool operator!() const noexcept { return !i; }
0105 #endif
0106
0107 constexpr inline QFlags operator|(QFlags other) const noexcept { return QFlags(QFlag(i | other.i)); }
0108 constexpr inline QFlags operator|(Enum other) const noexcept { return QFlags(QFlag(i | Int(other))); }
0109 constexpr inline QFlags operator^(QFlags other) const noexcept { return QFlags(QFlag(i ^ other.i)); }
0110 constexpr inline QFlags operator^(Enum other) const noexcept { return QFlags(QFlag(i ^ Int(other))); }
0111 #ifndef QT_TYPESAFE_FLAGS
0112 constexpr inline QFlags operator&(int mask) const noexcept { return QFlags(QFlag(i & mask)); }
0113 constexpr inline QFlags operator&(uint mask) const noexcept { return QFlags(QFlag(i & mask)); }
0114 #endif
0115 constexpr inline QFlags operator&(QFlags other) const noexcept { return QFlags(QFlag(i & other.i)); }
0116 constexpr inline QFlags operator&(Enum other) const noexcept { return QFlags(QFlag(i & Int(other))); }
0117 constexpr inline QFlags operator~() const noexcept { return QFlags(QFlag(~i)); }
0118
0119 constexpr inline void operator+(QFlags other) const noexcept = delete;
0120 constexpr inline void operator+(Enum other) const noexcept = delete;
0121 constexpr inline void operator+(int other) const noexcept = delete;
0122 constexpr inline void operator-(QFlags other) const noexcept = delete;
0123 constexpr inline void operator-(Enum other) const noexcept = delete;
0124 constexpr inline void operator-(int other) const noexcept = delete;
0125
0126 constexpr inline bool testFlag(Enum flag) const noexcept { return testFlags(flag); }
0127 constexpr inline bool testFlags(QFlags flags) const noexcept { return flags.i ? ((i & flags.i) == flags.i) : i == Int(0); }
0128 constexpr inline bool testAnyFlag(Enum flag) const noexcept { return testAnyFlags(flag); }
0129 constexpr inline bool testAnyFlags(QFlags flags) const noexcept { return (i & flags.i) != Int(0); }
0130 constexpr inline QFlags &setFlag(Enum flag, bool on = true) noexcept
0131 {
0132 return on ? (*this |= flag) : (*this &= ~QFlags(flag));
0133 }
0134
0135 friend constexpr inline bool operator==(QFlags lhs, QFlags rhs) noexcept
0136 { return lhs.i == rhs.i; }
0137 friend constexpr inline bool operator!=(QFlags lhs, QFlags rhs) noexcept
0138 { return lhs.i != rhs.i; }
0139 friend constexpr inline bool operator==(QFlags lhs, Enum rhs) noexcept
0140 { return lhs == QFlags(rhs); }
0141 friend constexpr inline bool operator!=(QFlags lhs, Enum rhs) noexcept
0142 { return lhs != QFlags(rhs); }
0143 friend constexpr inline bool operator==(Enum lhs, QFlags rhs) noexcept
0144 { return QFlags(lhs) == rhs; }
0145 friend constexpr inline bool operator!=(Enum lhs, QFlags rhs) noexcept
0146 { return QFlags(lhs) != rhs; }
0147
0148 #ifdef QT_TYPESAFE_FLAGS
0149
0150
0151
0152 friend constexpr inline bool operator==(QFlags flags, QtPrivate::CompareAgainstLiteralZero) noexcept
0153 { return flags.i == Int(0); }
0154 friend constexpr inline bool operator!=(QFlags flags, QtPrivate::CompareAgainstLiteralZero) noexcept
0155 { return flags.i != Int(0); }
0156 friend constexpr inline bool operator==(QtPrivate::CompareAgainstLiteralZero, QFlags flags) noexcept
0157 { return Int(0) == flags.i; }
0158 friend constexpr inline bool operator!=(QtPrivate::CompareAgainstLiteralZero, QFlags flags) noexcept
0159 { return Int(0) != flags.i; }
0160 #endif
0161
0162 private:
0163 constexpr static inline Int initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
0164 typename std::initializer_list<Enum>::const_iterator end)
0165 noexcept
0166 {
0167 return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
0168 }
0169
0170 Int i;
0171 };
0172
0173 #ifndef Q_MOC_RUN
0174 #define Q_DECLARE_FLAGS(Flags, Enum)\
0175 typedef QFlags<Enum> Flags;
0176 #endif
0177
0178 #ifdef QT_TYPESAFE_FLAGS
0179
0180
0181 #define QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags) \
0182 [[maybe_unused]] \
0183 constexpr inline Flags operator~(Flags::enum_type e) noexcept \
0184 { return ~Flags(e); } \
0185 [[maybe_unused]] \
0186 constexpr inline void operator|(Flags::enum_type f1, int f2) noexcept = delete;
0187 #else
0188 #define QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags) \
0189 [[maybe_unused]] \
0190 constexpr inline QIncompatibleFlag operator|(Flags::enum_type f1, int f2) noexcept \
0191 { return QIncompatibleFlag(int(f1) | f2); }
0192 #endif
0193
0194 #define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags) \
0195 [[maybe_unused]] \
0196 constexpr inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, Flags::enum_type f2) noexcept \
0197 { return QFlags<Flags::enum_type>(f1) | f2; } \
0198 [[maybe_unused]] \
0199 constexpr inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
0200 { return f2 | f1; } \
0201 [[maybe_unused]] \
0202 constexpr inline QFlags<Flags::enum_type> operator&(Flags::enum_type f1, Flags::enum_type f2) noexcept \
0203 { return QFlags<Flags::enum_type>(f1) & f2; } \
0204 [[maybe_unused]] \
0205 constexpr inline QFlags<Flags::enum_type> operator&(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
0206 { return f2 & f1; } \
0207 [[maybe_unused]] \
0208 constexpr inline QFlags<Flags::enum_type> operator^(Flags::enum_type f1, Flags::enum_type f2) noexcept \
0209 { return QFlags<Flags::enum_type>(f1) ^ f2; } \
0210 [[maybe_unused]] \
0211 constexpr inline QFlags<Flags::enum_type> operator^(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
0212 { return f2 ^ f1; } \
0213 constexpr inline void operator+(Flags::enum_type f1, Flags::enum_type f2) noexcept = delete; \
0214 constexpr inline void operator+(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0215 constexpr inline void operator+(int f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0216 constexpr inline void operator-(Flags::enum_type f1, Flags::enum_type f2) noexcept = delete; \
0217 constexpr inline void operator-(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0218 constexpr inline void operator-(int f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0219 constexpr inline void operator+(int f1, Flags::enum_type f2) noexcept = delete; \
0220 constexpr inline void operator+(Flags::enum_type f1, int f2) noexcept = delete; \
0221 constexpr inline void operator-(int f1, Flags::enum_type f2) noexcept = delete; \
0222 constexpr inline void operator-(Flags::enum_type f1, int f2) noexcept = delete; \
0223 QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags)
0224
0225
0226
0227 #if __cplusplus > 201702L
0228
0229 # define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS) \
0230 [[maybe_unused]] \
0231 constexpr inline Ret operator op (LHS lhs, RHS rhs) noexcept \
0232 { return static_cast<Ret>(qToUnderlying(lhs) op qToUnderlying(rhs)); } \
0233
0234 #else
0235
0236
0237 # define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS) \
0238 static_assert(std::is_same_v<decltype(std::declval<LHS>() op std::declval<RHS>()), Ret>);
0239 #endif
0240
0241 #define Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum) \
0242 Q_DECLARE_MIXED_ENUM_OPERATOR(|, Ret, Flags, Enum) \
0243 Q_DECLARE_MIXED_ENUM_OPERATOR(&, Ret, Flags, Enum) \
0244 Q_DECLARE_MIXED_ENUM_OPERATOR(^, Ret, Flags, Enum) \
0245
0246
0247 #define Q_DECLARE_MIXED_ENUM_OPERATORS_SYMMETRIC(Ret, Flags, Enum) \
0248 Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum) \
0249 Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Enum, Flags) \
0250
0251
0252
0253 QT_END_NAMESPACE
0254
0255 #endif