Make QFlags enum flags (C++11 strict enums) friendly
Change-Id: I9ccb3e4d281a545ca1845db4f6aa7ac6c04e8621 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
This commit is contained in:
parent
f7eea69a2a
commit
95cab90b10
@ -97,31 +97,32 @@ public:
|
|||||||
inline QFlags(const QFlags &other);
|
inline QFlags(const QFlags &other);
|
||||||
inline QFlags &operator=(const QFlags &other);
|
inline QFlags &operator=(const QFlags &other);
|
||||||
#endif
|
#endif
|
||||||
Q_DECL_CONSTEXPR inline QFlags(Enum f) : i(f) {}
|
Q_DECL_CONSTEXPR inline QFlags(Enum f) : i(Int(f)) {}
|
||||||
Q_DECL_CONSTEXPR inline QFlags(Zero = 0) : i(0) {}
|
Q_DECL_CONSTEXPR inline QFlags(Zero = 0) : i(0) {}
|
||||||
Q_DECL_CONSTEXPR inline QFlags(QFlag f) : i(f) {}
|
Q_DECL_CONSTEXPR inline QFlags(QFlag f) : i(f) {}
|
||||||
|
|
||||||
inline QFlags &operator&=(int mask) { i &= mask; return *this; }
|
inline QFlags &operator&=(int mask) { i &= mask; return *this; }
|
||||||
inline QFlags &operator&=(uint mask) { i &= mask; return *this; }
|
inline QFlags &operator&=(uint mask) { i &= mask; return *this; }
|
||||||
|
inline QFlags &operator&=(Enum mask) { i &= Int(mask); return *this; }
|
||||||
inline QFlags &operator|=(QFlags f) { i |= f.i; return *this; }
|
inline QFlags &operator|=(QFlags f) { i |= f.i; return *this; }
|
||||||
inline QFlags &operator|=(Enum f) { i |= f; return *this; }
|
inline QFlags &operator|=(Enum f) { i |= Int(f); return *this; }
|
||||||
inline QFlags &operator^=(QFlags f) { i ^= f.i; return *this; }
|
inline QFlags &operator^=(QFlags f) { i ^= f.i; return *this; }
|
||||||
inline QFlags &operator^=(Enum f) { i ^= f; return *this; }
|
inline QFlags &operator^=(Enum f) { i ^= Int(f); return *this; }
|
||||||
|
|
||||||
Q_DECL_CONSTEXPR inline operator Int() const { return i; }
|
Q_DECL_CONSTEXPR inline operator Int() const { return i; }
|
||||||
|
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator|(QFlags f) const { return QFlags(Enum(i | f.i)); }
|
Q_DECL_CONSTEXPR inline QFlags operator|(QFlags f) const { return QFlags(Enum(i | f.i)); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator|(Enum f) const { return QFlags(Enum(i | f)); }
|
Q_DECL_CONSTEXPR inline QFlags operator|(Enum f) const { return QFlags(Enum(i | Int(f))); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator^(QFlags f) const { return QFlags(Enum(i ^ f.i)); }
|
Q_DECL_CONSTEXPR inline QFlags operator^(QFlags f) const { return QFlags(Enum(i ^ f.i)); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator^(Enum f) const { return QFlags(Enum(i ^ f)); }
|
Q_DECL_CONSTEXPR inline QFlags operator^(Enum f) const { return QFlags(Enum(i ^ Int(f))); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator&(int mask) const { return QFlags(Enum(i & mask)); }
|
Q_DECL_CONSTEXPR inline QFlags operator&(int mask) const { return QFlags(Enum(i & mask)); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator&(uint mask) const { return QFlags(Enum(i & mask)); }
|
Q_DECL_CONSTEXPR inline QFlags operator&(uint mask) const { return QFlags(Enum(i & mask)); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator&(Enum f) const { return QFlags(Enum(i & f)); }
|
Q_DECL_CONSTEXPR inline QFlags operator&(Enum f) const { return QFlags(Enum(i & Int(f))); }
|
||||||
Q_DECL_CONSTEXPR inline QFlags operator~() const { return QFlags(Enum(~i)); }
|
Q_DECL_CONSTEXPR inline QFlags operator~() const { return QFlags(Enum(~i)); }
|
||||||
|
|
||||||
Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
|
Q_DECL_CONSTEXPR inline bool operator!() const { return !i; }
|
||||||
|
|
||||||
Q_DECL_CONSTEXPR inline bool testFlag(Enum f) const { return (i & f) == f && (f != 0 || i == Int(f) ); }
|
Q_DECL_CONSTEXPR inline bool testFlag(Enum f) const { return (i & Int(f)) == Int(f) && (Int(f) != 0 || i == Int(f) ); }
|
||||||
private:
|
private:
|
||||||
Int i;
|
Int i;
|
||||||
};
|
};
|
||||||
|
@ -239,6 +239,12 @@ Q_CORE_EXPORT void *qMemSet(void *dest, int c, size_t n);
|
|||||||
\overload
|
\overload
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn QFlags &QFlags::operator&=(Enum mask)
|
||||||
|
|
||||||
|
\overload
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn QFlags &QFlags::operator|=(QFlags other)
|
\fn QFlags &QFlags::operator|=(QFlags other)
|
||||||
|
|
||||||
|
@ -280,7 +280,7 @@ inline QDebug operator<<(QDebug debug, const QFlags<T> &flags)
|
|||||||
debug.nospace() << '|';
|
debug.nospace() << '|';
|
||||||
else
|
else
|
||||||
needSeparator = true;
|
needSeparator = true;
|
||||||
debug.nospace() << "0x" << QByteArray::number(T(1 << i), 16).constData();
|
debug.nospace() << "0x" << QByteArray::number(typename QFlags<T>::Int(1) << i, 16).constData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
debug << ')';
|
debug << ')';
|
||||||
|
@ -49,6 +49,7 @@ private slots:
|
|||||||
void testFlagMultiBits() const;
|
void testFlagMultiBits() const;
|
||||||
void constExpr();
|
void constExpr();
|
||||||
void signedness();
|
void signedness();
|
||||||
|
void classEnum();
|
||||||
};
|
};
|
||||||
|
|
||||||
void tst_QFlags::testFlag() const
|
void tst_QFlags::testFlag() const
|
||||||
@ -137,6 +138,121 @@ void tst_QFlags::signedness()
|
|||||||
QtPrivate::is_signed<Qt::Alignment::Int>::value));
|
QtPrivate::is_signed<Qt::Alignment::Int>::value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(Q_COMPILER_CLASS_ENUM)
|
||||||
|
enum class MyStrictEnum { StrictZero, StrictOne, StrictTwo, StrictFour=4 };
|
||||||
|
Q_DECLARE_FLAGS( MyStrictFlags, MyStrictEnum )
|
||||||
|
Q_DECLARE_OPERATORS_FOR_FLAGS( MyStrictFlags )
|
||||||
|
|
||||||
|
Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isComplex );
|
||||||
|
Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isStatic );
|
||||||
|
Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isLarge );
|
||||||
|
Q_STATIC_ASSERT( !QTypeInfo<MyStrictFlags>::isPointer );
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void tst_QFlags::classEnum()
|
||||||
|
{
|
||||||
|
#if defined(Q_COMPILER_CLASS_ENUM)
|
||||||
|
// The main aim of the test is making sure it compiles
|
||||||
|
// The QCOMPARE are there as an extra
|
||||||
|
MyStrictEnum e1 = MyStrictEnum::StrictOne;
|
||||||
|
MyStrictEnum e2 = MyStrictEnum::StrictTwo;
|
||||||
|
|
||||||
|
MyStrictFlags f1(MyStrictEnum::StrictOne);
|
||||||
|
QCOMPARE(f1, 1);
|
||||||
|
|
||||||
|
MyStrictFlags f2(e2);
|
||||||
|
QCOMPARE(f2, 2);
|
||||||
|
|
||||||
|
MyStrictFlags f0;
|
||||||
|
QCOMPARE(f0, 0);
|
||||||
|
|
||||||
|
MyStrictFlags f3(e2 | e1);
|
||||||
|
QCOMPARE(f3, 3);
|
||||||
|
|
||||||
|
QVERIFY(f3.testFlag(MyStrictEnum::StrictOne));
|
||||||
|
QVERIFY(!f1.testFlag(MyStrictEnum::StrictTwo));
|
||||||
|
|
||||||
|
QVERIFY(!f0);
|
||||||
|
|
||||||
|
QCOMPARE(f3 & int(1), 1);
|
||||||
|
QCOMPARE(f3 & uint(1), 1);
|
||||||
|
QCOMPARE(f3 & MyStrictEnum::StrictOne, 1);
|
||||||
|
|
||||||
|
MyStrictFlags aux;
|
||||||
|
aux = f3;
|
||||||
|
aux &= int(1);
|
||||||
|
QCOMPARE(aux, 1);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux &= uint(1);
|
||||||
|
QCOMPARE(aux, 1);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux &= MyStrictEnum::StrictOne;
|
||||||
|
QCOMPARE(aux, 1);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux &= f1;
|
||||||
|
QCOMPARE(aux, 1);
|
||||||
|
|
||||||
|
aux = f3 ^ f3;
|
||||||
|
QCOMPARE(aux, 0);
|
||||||
|
|
||||||
|
aux = f3 ^ f1;
|
||||||
|
QCOMPARE(aux, 2);
|
||||||
|
|
||||||
|
aux = f3 ^ f0;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = f3 ^ MyStrictEnum::StrictOne;
|
||||||
|
QCOMPARE(aux, 2);
|
||||||
|
|
||||||
|
aux = f3 ^ MyStrictEnum::StrictZero;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux ^= f3;
|
||||||
|
QCOMPARE(aux, 0);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux ^= f1;
|
||||||
|
QCOMPARE(aux, 2);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux ^= f0;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux ^= MyStrictEnum::StrictOne;
|
||||||
|
QCOMPARE(aux, 2);
|
||||||
|
|
||||||
|
aux = f3;
|
||||||
|
aux ^= MyStrictEnum::StrictZero;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = f1 | f2;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = MyStrictEnum::StrictOne | MyStrictEnum::StrictTwo;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = f1;
|
||||||
|
aux |= f2;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = MyStrictEnum::StrictOne;
|
||||||
|
aux |= MyStrictEnum::StrictTwo;
|
||||||
|
QCOMPARE(aux, 3);
|
||||||
|
|
||||||
|
aux = ~f1;
|
||||||
|
QCOMPARE(aux, -2);
|
||||||
|
|
||||||
|
// Just to make sure it compiles
|
||||||
|
if (false)
|
||||||
|
qDebug() << f3;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
// (statically) check QTypeInfo for QFlags instantiations:
|
// (statically) check QTypeInfo for QFlags instantiations:
|
||||||
enum MyEnum { Zero, One, Two, Four=4 };
|
enum MyEnum { Zero, One, Two, Four=4 };
|
||||||
Q_DECLARE_FLAGS( MyFlags, MyEnum )
|
Q_DECLARE_FLAGS( MyFlags, MyEnum )
|
||||||
|
Loading…
Reference in New Issue
Block a user