Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-20 09:21:47

0001 // Copyright (C) 2016 The Qt Company Ltd.
0002 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
0003 // Qt-Security score:significant reason:default
0004 
0005 #ifndef QFLAGS_H
0006 #define QFLAGS_H
0007 
0008 #include <QtCore/qcompare_impl.h>
0009 #include <QtCore/qtypeinfo.h>
0010 
0011 #include <algorithm>
0012 #include <initializer_list>
0013 
0014 QT_BEGIN_NAMESPACE
0015 
0016 template<typename Enum> class QFlags;
0017 
0018 class QFlag
0019 {
0020     int i;
0021 public:
0022     constexpr inline Q_IMPLICIT QFlag(int value) noexcept : i(value) {}
0023     constexpr inline Q_IMPLICIT operator int() const noexcept { return i; }
0024 
0025 #if !defined(Q_CC_MSVC)
0026     // Microsoft Visual Studio has buggy behavior when it comes to
0027     // unsigned enums: even if the enum is unsigned, the enum tags are
0028     // always signed
0029 #  if !defined(__LP64__) && !defined(Q_QDOC)
0030     constexpr inline Q_IMPLICIT QFlag(long value) noexcept : i(int(value)) {}
0031     constexpr inline Q_IMPLICIT QFlag(ulong value) noexcept : i(int(long(value))) {}
0032 #  endif
0033     constexpr inline Q_IMPLICIT QFlag(uint value) noexcept : i(int(value)) {}
0034     constexpr inline Q_IMPLICIT QFlag(short value) noexcept : i(int(value)) {}
0035     constexpr inline Q_IMPLICIT QFlag(ushort value) noexcept : i(int(uint(value))) {}
0036     constexpr inline Q_IMPLICIT operator uint() const noexcept { return uint(i); }
0037 #endif
0038 };
0039 Q_DECLARE_TYPEINFO(QFlag, Q_PRIMITIVE_TYPE);
0040 
0041 class QIncompatibleFlag
0042 {
0043     int i;
0044 public:
0045     constexpr inline explicit QIncompatibleFlag(int i) noexcept;
0046     constexpr inline Q_IMPLICIT operator int() const noexcept { return i; }
0047 };
0048 Q_DECLARE_TYPEINFO(QIncompatibleFlag, Q_PRIMITIVE_TYPE);
0049 
0050 constexpr inline QIncompatibleFlag::QIncompatibleFlag(int value) noexcept : i(value) {}
0051 
0052 namespace QtPrivate {
0053 template <typename T> struct IsQFlags : std::false_type {};
0054 template <typename E> struct IsQFlags<QFlags<E>> : std::true_type {};
0055 
0056 template<typename Enum>
0057 class QFlagsStorage
0058 {
0059     static_assert(sizeof(Enum) <= sizeof(quint64),
0060                   "Only enumerations 64 bits or smaller are supported.");
0061     static_assert((std::is_enum<Enum>::value), "QFlags is only usable on enumeration types.");
0062 
0063     static constexpr size_t IntegerSize = (std::max)(sizeof(Enum), sizeof(int));
0064     using Integers = QIntegerForSize<IntegerSize>;
0065 
0066 protected:
0067     typedef typename std::conditional<
0068             std::is_unsigned<typename std::underlying_type<Enum>::type>::value,
0069             typename Integers::Unsigned,
0070             typename Integers::Signed
0071         >::type Int;
0072 
0073     Int i = 0;
0074 
0075     QT_DECLARE_RO5_SMF_AS_DEFAULTED(QFlagsStorage)
0076 public:
0077     constexpr inline QFlagsStorage() noexcept = default;
0078     constexpr inline explicit QFlagsStorage(std::in_place_t, Int v) : i(v) {}
0079 };
0080 
0081 template <typename Enum, int Size = sizeof(QFlagsStorage<Enum>)>
0082 struct QFlagsStorageHelper : QFlagsStorage<Enum>
0083 {
0084     using QFlagsStorage<Enum>::QFlagsStorage;
0085 protected:
0086     QT_DECLARE_RO5_SMF_AS_DEFAULTED(QFlagsStorageHelper)
0087 };
0088 template <typename Enum> struct QFlagsStorageHelper<Enum, sizeof(int)> : QFlagsStorage<Enum>
0089 {
0090     using QFlagsStorage<Enum>::QFlagsStorage;
0091 
0092     // For compatibility with Qt 3, moc goes through QFlag in order to
0093     // read/write properties of type QFlags; so a conversion to QFlag is also
0094     // needed here. (It otherwise goes through a QFlags->int->QFlag conversion
0095     // sequence.)
0096     constexpr inline Q_IMPLICIT QFlagsStorageHelper(QFlag flag) noexcept
0097         : QFlagsStorage<Enum>(std::in_place, flag) {}
0098 #ifdef QT_TYPESAFE_FLAGS
0099     constexpr inline explicit operator QFlag() const noexcept { return QFlag(this->i); }
0100 #endif
0101 protected:
0102     QT_DECLARE_RO5_SMF_AS_DEFAULTED(QFlagsStorageHelper)
0103 };
0104 } // namespace QtPrivate
0105 
0106 template<typename Enum>
0107 class QFlags : public QtPrivate::QFlagsStorageHelper<Enum>
0108 {
0109     using Base = QtPrivate::QFlagsStorageHelper<Enum>;
0110 public:
0111     typedef Enum enum_type;
0112     using Int = typename Base::Int;
0113     using Base::Base;
0114 
0115     // compiler-generated copy/move ctor/assignment operators are fine!
0116     constexpr inline QFlags() noexcept = default;
0117 
0118     constexpr inline Q_IMPLICIT QFlags(Enum flags) noexcept : Base(std::in_place, Int(flags)) {}
0119 
0120 #ifdef Q_QDOC
0121     constexpr inline Q_IMPLICIT QFlags(std::in_place_t, Int flags) noexcept;
0122     constexpr inline Q_IMPLICIT QFlags(QFlag flag) noexcept
0123         requires(sizeof(Enum) == sizeof(int));
0124 #endif
0125 
0126     constexpr inline QFlags(std::initializer_list<Enum> flags) noexcept
0127         : Base(std::in_place, initializer_list_helper(flags.begin(), flags.end())) {}
0128 
0129     constexpr static inline QFlags fromInt(Int i) noexcept { return QFlags(std::in_place, i); }
0130     constexpr inline Int toInt() const noexcept { return i; }
0131 
0132 #ifndef QT_TYPESAFE_FLAGS
0133     constexpr inline QFlags &operator&=(int mask) noexcept { i &= mask; return *this; }
0134     constexpr inline QFlags &operator&=(uint mask) noexcept { i &= mask; return *this; }
0135 #endif
0136     constexpr inline QFlags &operator&=(QFlags mask) noexcept { i &= mask.i; return *this; }
0137     constexpr inline QFlags &operator&=(Enum mask) noexcept { i &= Int(mask); return *this; }
0138     constexpr inline QFlags &operator|=(QFlags other) noexcept { i |= other.i; return *this; }
0139     constexpr inline QFlags &operator|=(Enum other) noexcept { i |= Int(other); return *this; }
0140     constexpr inline QFlags &operator^=(QFlags other) noexcept { i ^= other.i; return *this; }
0141     constexpr inline QFlags &operator^=(Enum other) noexcept { i ^= Int(other); return *this; }
0142 
0143 #ifdef QT_TYPESAFE_FLAGS
0144     constexpr inline explicit operator Int() const noexcept { return i; }
0145     constexpr inline explicit operator bool() const noexcept { return i; }
0146 #else
0147     constexpr inline Q_IMPLICIT operator Int() const noexcept { return i; }
0148     constexpr inline bool operator!() const noexcept { return !i; }
0149 #endif
0150 
0151     constexpr inline QFlags operator|(QFlags other) const noexcept { return QFlags(std::in_place, i | other.i); }
0152     constexpr inline QFlags operator|(Enum other) const noexcept { return QFlags(std::in_place, i | Int(other)); }
0153     constexpr inline QFlags operator^(QFlags other) const noexcept { return QFlags(std::in_place, i ^ other.i); }
0154     constexpr inline QFlags operator^(Enum other) const noexcept { return QFlags(std::in_place, i ^ Int(other)); }
0155 #ifndef QT_TYPESAFE_FLAGS
0156     constexpr inline QFlags operator&(int mask) const noexcept { return QFlags(std::in_place, i & mask); }
0157     constexpr inline QFlags operator&(uint mask) const noexcept { return QFlags(std::in_place, i & mask); }
0158 #endif
0159     constexpr inline QFlags operator&(QFlags other) const noexcept { return QFlags(std::in_place, i & other.i); }
0160     constexpr inline QFlags operator&(Enum other) const noexcept { return QFlags(std::in_place, i & Int(other)); }
0161     constexpr inline QFlags operator~() const noexcept { return QFlags(std::in_place, ~i); }
0162 
0163     constexpr inline void operator+(QFlags other) const noexcept = delete;
0164     constexpr inline void operator+(Enum other) const noexcept = delete;
0165     constexpr inline void operator+(int other) const noexcept = delete;
0166     constexpr inline void operator-(QFlags other) const noexcept = delete;
0167     constexpr inline void operator-(Enum other) const noexcept = delete;
0168     constexpr inline void operator-(int other) const noexcept = delete;
0169 
0170     constexpr inline bool testFlag(Enum flag) const noexcept { return testFlags(flag); }
0171     constexpr inline bool testFlags(QFlags flags) const noexcept { return flags.i ? ((i & flags.i) == flags.i) : i == Int(0); }
0172     constexpr inline bool testAnyFlag(Enum flag) const noexcept { return testAnyFlags(flag); }
0173     constexpr inline bool testAnyFlags(QFlags flags) const noexcept { return (i & flags.i) != Int(0); }
0174     constexpr inline QFlags &setFlag(Enum flag, bool on = true) noexcept
0175     {
0176         return on ? (*this |= flag) : (*this &= ~QFlags(flag));
0177     }
0178 
0179     friend constexpr inline bool operator==(QFlags lhs, QFlags rhs) noexcept
0180     { return lhs.i == rhs.i; }
0181     friend constexpr inline bool operator!=(QFlags lhs, QFlags rhs) noexcept
0182     { return lhs.i != rhs.i; }
0183     friend constexpr inline bool operator==(QFlags lhs, Enum rhs) noexcept
0184     { return lhs == QFlags(rhs); }
0185     friend constexpr inline bool operator!=(QFlags lhs, Enum rhs) noexcept
0186     { return lhs != QFlags(rhs); }
0187     friend constexpr inline bool operator==(Enum lhs, QFlags rhs) noexcept
0188     { return QFlags(lhs) == rhs; }
0189     friend constexpr inline bool operator!=(Enum lhs, QFlags rhs) noexcept
0190     { return QFlags(lhs) != rhs; }
0191 
0192 #ifdef QT_TYPESAFE_FLAGS
0193     // Provide means of comparing flags against a literal 0; opt-in
0194     // because otherwise they're ambiguous against operator==(int,int)
0195     // after a QFlags->int conversion.
0196     friend constexpr inline bool operator==(QFlags flags, QtPrivate::CompareAgainstLiteralZero) noexcept
0197     { return flags.i == Int(0); }
0198     friend constexpr inline bool operator!=(QFlags flags, QtPrivate::CompareAgainstLiteralZero) noexcept
0199     { return flags.i != Int(0); }
0200     friend constexpr inline bool operator==(QtPrivate::CompareAgainstLiteralZero, QFlags flags) noexcept
0201     { return Int(0) == flags.i; }
0202     friend constexpr inline bool operator!=(QtPrivate::CompareAgainstLiteralZero, QFlags flags) noexcept
0203     { return Int(0) != flags.i; }
0204 #endif
0205 
0206 private:
0207     constexpr static inline Int initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
0208                                                                typename std::initializer_list<Enum>::const_iterator end)
0209     noexcept
0210     {
0211         return (it == end ? Int(0) : (Int(*it) | initializer_list_helper(it + 1, end)));
0212     }
0213 
0214     using Base::i;
0215 };
0216 
0217 #ifndef Q_MOC_RUN
0218 #define Q_DECLARE_FLAGS(Flags, Enum)\
0219 typedef QFlags<Enum> Flags;
0220 #endif
0221 
0222 #ifdef QT_TYPESAFE_FLAGS
0223 
0224 // These are opt-in, for backwards compatibility
0225 #define QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags) \
0226 [[maybe_unused]] \
0227 constexpr inline Flags operator~(Flags::enum_type e) noexcept \
0228 { return ~Flags(e); } \
0229 [[maybe_unused]] \
0230 constexpr inline void operator|(Flags::enum_type f1, int f2) noexcept = delete;
0231 #else
0232 #define QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags) \
0233 [[maybe_unused]] \
0234 constexpr inline QIncompatibleFlag operator|(Flags::enum_type f1, int f2) noexcept \
0235 { return QIncompatibleFlag(int(f1) | f2); }
0236 #endif
0237 
0238 #define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags) \
0239 [[maybe_unused]] \
0240 constexpr inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, Flags::enum_type f2) noexcept \
0241 { return QFlags<Flags::enum_type>(f1) | f2; } \
0242 [[maybe_unused]] \
0243 constexpr inline QFlags<Flags::enum_type> operator|(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
0244 { return f2 | f1; } \
0245 [[maybe_unused]] \
0246 constexpr inline QFlags<Flags::enum_type> operator&(Flags::enum_type f1, Flags::enum_type f2) noexcept \
0247 { return QFlags<Flags::enum_type>(f1) & f2; } \
0248 [[maybe_unused]] \
0249 constexpr inline QFlags<Flags::enum_type> operator&(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
0250 { return f2 & f1; } \
0251 [[maybe_unused]] \
0252 constexpr inline QFlags<Flags::enum_type> operator^(Flags::enum_type f1, Flags::enum_type f2) noexcept \
0253 { return QFlags<Flags::enum_type>(f1) ^ f2; } \
0254 [[maybe_unused]] \
0255 constexpr inline QFlags<Flags::enum_type> operator^(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept \
0256 { return f2 ^ f1; } \
0257 constexpr inline void operator+(Flags::enum_type f1, Flags::enum_type f2) noexcept = delete; \
0258 constexpr inline void operator+(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0259 constexpr inline void operator+(int f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0260 constexpr inline void operator-(Flags::enum_type f1, Flags::enum_type f2) noexcept = delete; \
0261 constexpr inline void operator-(Flags::enum_type f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0262 constexpr inline void operator-(int f1, QFlags<Flags::enum_type> f2) noexcept = delete; \
0263 constexpr inline void operator+(int f1, Flags::enum_type f2) noexcept = delete; \
0264 constexpr inline void operator+(Flags::enum_type f1, int f2) noexcept = delete; \
0265 constexpr inline void operator-(int f1, Flags::enum_type f2) noexcept = delete; \
0266 constexpr inline void operator-(Flags::enum_type f1, int f2) noexcept = delete; \
0267 QT_DECLARE_TYPESAFE_OPERATORS_FOR_FLAGS_ENUM(Flags)
0268 
0269 // restore bit-wise enum-enum operators deprecated in C++20,
0270 // but used in a few places in the API
0271 #if __cplusplus > 201702L // assume compilers don't warn if in C++17 mode
0272   // in C++20 mode, provide user-defined operators to override the deprecated operations:
0273 # define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS) \
0274     [[maybe_unused]] \
0275     constexpr inline Ret operator op (LHS lhs, RHS rhs) noexcept \
0276     { return static_cast<Ret>(qToUnderlying(lhs) op qToUnderlying(rhs)); } \
0277     /* end */
0278 #else
0279   // in C++17 mode, statically-assert that this compiler's result of the
0280   // operation is the same that the C++20 version would produce:
0281 # define Q_DECLARE_MIXED_ENUM_OPERATOR(op, Ret, LHS, RHS) \
0282     static_assert(std::is_same_v<decltype(std::declval<LHS>() op std::declval<RHS>()), Ret>);
0283 #endif
0284 
0285 #define Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum) \
0286     Q_DECLARE_MIXED_ENUM_OPERATOR(|, Ret, Flags, Enum) \
0287     Q_DECLARE_MIXED_ENUM_OPERATOR(&, Ret, Flags, Enum) \
0288     Q_DECLARE_MIXED_ENUM_OPERATOR(^, Ret, Flags, Enum) \
0289     /* end */
0290 
0291 #define Q_DECLARE_MIXED_ENUM_OPERATORS_SYMMETRIC(Ret, Flags, Enum) \
0292     Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Flags, Enum) \
0293     Q_DECLARE_MIXED_ENUM_OPERATORS(Ret, Enum, Flags) \
0294     /* end */
0295 
0296 
0297 QT_END_NAMESPACE
0298 
0299 #endif // QFLAGS_H