QMetaType: use modern comparisons
Internal QPropertyBindingPrivatePtr, QArgumentType classes also have been updated; Replace class operators operator==(), operator!=() of QRegularExpression to friend method comparesEqual() and Q_DECLARE_EQUALITY_COMPARABLE macro. Task-number: QTBUG-120304 Change-Id: I010617cbcb8bd6afb7c21ee4345398648821f72c Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
parent
ced943e936
commit
5a0faa9198
@ -111,22 +111,18 @@ public:
|
|||||||
const_cast<QArgumentType *>(this)->_name = QMetaType(_type).name();
|
const_cast<QArgumentType *>(this)->_name = QMetaType(_type).name();
|
||||||
return _name;
|
return _name;
|
||||||
}
|
}
|
||||||
bool operator==(const QArgumentType &other) const
|
|
||||||
{
|
|
||||||
if (_type && other._type)
|
|
||||||
return _type == other._type;
|
|
||||||
else
|
|
||||||
return name() == other.name();
|
|
||||||
}
|
|
||||||
bool operator!=(const QArgumentType &other) const
|
|
||||||
{
|
|
||||||
if (_type && other._type)
|
|
||||||
return _type != other._type;
|
|
||||||
else
|
|
||||||
return name() != other.name();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend bool comparesEqual(const QArgumentType &lhs,
|
||||||
|
const QArgumentType &rhs) noexcept
|
||||||
|
{
|
||||||
|
if (lhs._type && rhs._type)
|
||||||
|
return lhs._type == rhs._type;
|
||||||
|
else
|
||||||
|
return lhs.name() == rhs.name();
|
||||||
|
}
|
||||||
|
Q_DECLARE_EQUALITY_COMPARABLE(QArgumentType)
|
||||||
|
|
||||||
int _type;
|
int _type;
|
||||||
QByteArray _name;
|
QByteArray _name;
|
||||||
};
|
};
|
||||||
|
@ -474,6 +474,7 @@ const char *QtMetaTypePrivate::typedefNameForType(const QtPrivate::QMetaTypeInte
|
|||||||
|
|
||||||
\ingroup objectmodel
|
\ingroup objectmodel
|
||||||
\threadsafe
|
\threadsafe
|
||||||
|
\compares equality
|
||||||
|
|
||||||
The class is used as a helper to marshall types in QVariant and
|
The class is used as a helper to marshall types in QVariant and
|
||||||
in queued signals and slots connections. It associates a type
|
in queued signals and slots connections. It associates a type
|
||||||
@ -925,20 +926,20 @@ void QMetaType::unregisterMetaType(QMetaType type)
|
|||||||
Returns the QMetaType corresponding to the type in the template parameter.
|
Returns the QMetaType corresponding to the type in the template parameter.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*! \fn bool QMetaType::operator==(QMetaType a, QMetaType b)
|
/*! \fn bool QMetaType::operator==(const QMetaType &lhs, const QMetaType &rhs)
|
||||||
\since 5.15
|
\since 5.15
|
||||||
\overload
|
\overload
|
||||||
|
|
||||||
Returns \c true if the QMetaType \a a represents the same type
|
Returns \c true if the QMetaType \a lhs represents the same type
|
||||||
as the QMetaType \a b, otherwise returns \c false.
|
as the QMetaType \a rhs, otherwise returns \c false.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*! \fn bool QMetaType::operator!=(QMetaType a, QMetaType b)
|
/*! \fn bool QMetaType::operator!=(const QMetaType &lhs, const QMetaType &rhs)
|
||||||
\since 5.15
|
\since 5.15
|
||||||
\overload
|
\overload
|
||||||
|
|
||||||
Returns \c true if the QMetaType \a a represents a different type
|
Returns \c true if the QMetaType \a lhs represents a different type
|
||||||
than the QMetaType \a b, otherwise returns \c false.
|
than the QMetaType \a rhs, otherwise returns \c false.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*! \internal */
|
/*! \internal */
|
||||||
|
@ -518,20 +518,20 @@ public:
|
|||||||
template<typename T>
|
template<typename T>
|
||||||
constexpr static QMetaType fromType();
|
constexpr static QMetaType fromType();
|
||||||
static QMetaType fromName(QByteArrayView name);
|
static QMetaType fromName(QByteArrayView name);
|
||||||
|
private:
|
||||||
friend bool operator==(QMetaType a, QMetaType b)
|
friend bool comparesEqual(const QMetaType &lhs,
|
||||||
|
const QMetaType &rhs) noexcept
|
||||||
{
|
{
|
||||||
if (a.d_ptr == b.d_ptr)
|
if (lhs.d_ptr == rhs.d_ptr)
|
||||||
return true;
|
return true;
|
||||||
if (!a.d_ptr || !b.d_ptr)
|
if (!lhs.d_ptr || !rhs.d_ptr)
|
||||||
return false; // one type is undefined, the other is defined
|
return false; // one type is undefined, the other is defined
|
||||||
// avoid id call if we already have the id
|
// avoid id call if we already have the id
|
||||||
const int aId = a.id();
|
const int aId = lhs.id();
|
||||||
const int bId = b.id();
|
const int bId = rhs.id();
|
||||||
return aId == bId;
|
return aId == bId;
|
||||||
}
|
}
|
||||||
friend bool operator!=(QMetaType a, QMetaType b) { return !(a == b); }
|
Q_DECLARE_EQUALITY_COMPARABLE(QMetaType)
|
||||||
|
|
||||||
#ifndef QT_NO_DEBUG_STREAM
|
#ifndef QT_NO_DEBUG_STREAM
|
||||||
private:
|
private:
|
||||||
friend Q_CORE_EXPORT QDebug operator<<(QDebug d, QMetaType m);
|
friend Q_CORE_EXPORT QDebug operator<<(QDebug d, QMetaType m);
|
||||||
|
@ -95,28 +95,20 @@ public:
|
|||||||
void swap(QPropertyBindingPrivatePtr &other) noexcept
|
void swap(QPropertyBindingPrivatePtr &other) noexcept
|
||||||
{ qt_ptr_swap(d, other.d); }
|
{ qt_ptr_swap(d, other.d); }
|
||||||
|
|
||||||
friend bool operator==(const QPropertyBindingPrivatePtr &p1, const QPropertyBindingPrivatePtr &p2) noexcept
|
|
||||||
{ return p1.d == p2.d; }
|
|
||||||
friend bool operator!=(const QPropertyBindingPrivatePtr &p1, const QPropertyBindingPrivatePtr &p2) noexcept
|
|
||||||
{ return p1.d != p2.d; }
|
|
||||||
friend bool operator==(const QPropertyBindingPrivatePtr &p1, const T *ptr) noexcept
|
|
||||||
{ return p1.d == ptr; }
|
|
||||||
friend bool operator!=(const QPropertyBindingPrivatePtr &p1, const T *ptr) noexcept
|
|
||||||
{ return p1.d != ptr; }
|
|
||||||
friend bool operator==(const T *ptr, const QPropertyBindingPrivatePtr &p2) noexcept
|
|
||||||
{ return ptr == p2.d; }
|
|
||||||
friend bool operator!=(const T *ptr, const QPropertyBindingPrivatePtr &p2) noexcept
|
|
||||||
{ return ptr != p2.d; }
|
|
||||||
friend bool operator==(const QPropertyBindingPrivatePtr &p1, std::nullptr_t) noexcept
|
|
||||||
{ return !p1; }
|
|
||||||
friend bool operator!=(const QPropertyBindingPrivatePtr &p1, std::nullptr_t) noexcept
|
|
||||||
{ return p1; }
|
|
||||||
friend bool operator==(std::nullptr_t, const QPropertyBindingPrivatePtr &p2) noexcept
|
|
||||||
{ return !p2; }
|
|
||||||
friend bool operator!=(std::nullptr_t, const QPropertyBindingPrivatePtr &p2) noexcept
|
|
||||||
{ return p2; }
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend bool comparesEqual(const QPropertyBindingPrivatePtr &lhs,
|
||||||
|
const QPropertyBindingPrivatePtr &rhs) noexcept
|
||||||
|
{ return lhs.d == rhs.d; }
|
||||||
|
Q_DECLARE_EQUALITY_COMPARABLE(QPropertyBindingPrivatePtr)
|
||||||
|
friend bool comparesEqual(const QPropertyBindingPrivatePtr &lhs,
|
||||||
|
const T *rhs) noexcept
|
||||||
|
{ return lhs.d == rhs; }
|
||||||
|
Q_DECLARE_EQUALITY_COMPARABLE(QPropertyBindingPrivatePtr, T*)
|
||||||
|
friend bool comparesEqual(const QPropertyBindingPrivatePtr &lhs,
|
||||||
|
std::nullptr_t) noexcept
|
||||||
|
{ return !lhs; }
|
||||||
|
Q_DECLARE_EQUALITY_COMPARABLE(QPropertyBindingPrivatePtr, std::nullptr_t)
|
||||||
|
|
||||||
QtPrivate::RefCounted *d;
|
QtPrivate::RefCounted *d;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,6 +51,7 @@ qt_internal_add_test(tst_qmetatype
|
|||||||
../../../other/qvariant_common
|
../../../other/qvariant_common
|
||||||
LIBRARIES
|
LIBRARIES
|
||||||
Qt::CorePrivate
|
Qt::CorePrivate
|
||||||
|
Qt::TestPrivate
|
||||||
Qt::Gui
|
Qt::Gui
|
||||||
qmetatype_lib1
|
qmetatype_lib1
|
||||||
qmetatype_lib2
|
qmetatype_lib2
|
||||||
|
@ -111,6 +111,7 @@ private slots:
|
|||||||
void customDebugStream();
|
void customDebugStream();
|
||||||
void unknownType();
|
void unknownType();
|
||||||
void fromType();
|
void fromType();
|
||||||
|
void compareCompiles();
|
||||||
void operatorEq_data();
|
void operatorEq_data();
|
||||||
void operatorEq();
|
void operatorEq();
|
||||||
void operatorEq2_data();
|
void operatorEq2_data();
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include "tst_qmetatype_libs.h"
|
#include "tst_qmetatype_libs.h"
|
||||||
|
|
||||||
#include <QtCore/private/qmetaobjectbuilder_p.h>
|
#include <QtCore/private/qmetaobjectbuilder_p.h>
|
||||||
|
#include <QtTest/private/qcomparisontesthelper_p.h>
|
||||||
|
|
||||||
void tst_QMetaType::constRefs()
|
void tst_QMetaType::constRefs()
|
||||||
{
|
{
|
||||||
@ -418,6 +419,11 @@ struct CharTemplate
|
|||||||
} y;
|
} y;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void tst_QMetaType::compareCompiles()
|
||||||
|
{
|
||||||
|
QTestPrivate::testEqualityOperatorsCompile<QMetaType>();
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QMetaType::operatorEq_data()
|
void tst_QMetaType::operatorEq_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<QMetaType>("typeA");
|
QTest::addColumn<QMetaType>("typeA");
|
||||||
@ -447,10 +453,7 @@ void tst_QMetaType::operatorEq()
|
|||||||
QFETCH(QMetaType, typeB);
|
QFETCH(QMetaType, typeB);
|
||||||
QFETCH(bool, eq);
|
QFETCH(bool, eq);
|
||||||
|
|
||||||
QCOMPARE(typeA == typeB, eq);
|
QT_TEST_EQUALITY_OPS(typeA, typeB, eq);
|
||||||
QCOMPARE(typeB == typeA, eq);
|
|
||||||
QCOMPARE(typeA != typeB, !eq);
|
|
||||||
QCOMPARE(typeB != typeA, !eq);
|
|
||||||
|
|
||||||
#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
|
||||||
// for built-in types or locally-defined types, this must also hold true
|
// for built-in types or locally-defined types, this must also hold true
|
||||||
@ -487,10 +490,10 @@ FOR_EACH_CORE_METATYPE(GET_METATYPE_FROM_TYPE)
|
|||||||
QCOMPARE(fromId2.id(), type);
|
QCOMPARE(fromId2.id(), type);
|
||||||
|
|
||||||
// confirm that they're all equal
|
// confirm that they're all equal
|
||||||
QCOMPARE(fromId1, fromId2);
|
QT_TEST_EQUALITY_OPS(fromId1, fromId2, true);
|
||||||
QCOMPARE(fromType1, fromType2);
|
QT_TEST_EQUALITY_OPS(fromType1, fromType2, true);
|
||||||
QCOMPARE(fromType1, fromId1);
|
QT_TEST_EQUALITY_OPS(fromType1, fromId1, true);
|
||||||
QCOMPARE(fromType2, fromId2);
|
QT_TEST_EQUALITY_OPS(fromType2, fromId2, true);
|
||||||
|
|
||||||
#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
|
||||||
// for built-in types (other than void), this must be true
|
// for built-in types (other than void), this must be true
|
||||||
@ -541,7 +544,7 @@ void tst_QMetaType::operatorEqAcrossLibs()
|
|||||||
// DO THIS FIRST:
|
// DO THIS FIRST:
|
||||||
// if this isn't a built-in type, then the QMetaTypeInterface::typeId is
|
// if this isn't a built-in type, then the QMetaTypeInterface::typeId is
|
||||||
// initially set to 0
|
// initially set to 0
|
||||||
QCOMPARE(lib1Type, lib2Type);
|
QT_TEST_EQUALITY_OPS(lib1Type, lib2Type, true);
|
||||||
|
|
||||||
int actualTypeId = localType.id();
|
int actualTypeId = localType.id();
|
||||||
bool builtinTypeExpected = builtinTypeId != QMetaType::UnknownType;
|
bool builtinTypeExpected = builtinTypeId != QMetaType::UnknownType;
|
||||||
@ -559,8 +562,8 @@ void tst_QMetaType::operatorEqAcrossLibs()
|
|||||||
QCOMPARE(lib2Type.id(), actualTypeId);
|
QCOMPARE(lib2Type.id(), actualTypeId);
|
||||||
QCOMPARE(QByteArray(lib1Type.name()), QByteArray(localType.name()));
|
QCOMPARE(QByteArray(lib1Type.name()), QByteArray(localType.name()));
|
||||||
QCOMPARE(QByteArray(lib2Type.name()), QByteArray(localType.name()));
|
QCOMPARE(QByteArray(lib2Type.name()), QByteArray(localType.name()));
|
||||||
QCOMPARE(lib1Type, localType);
|
QT_TEST_EQUALITY_OPS(lib1Type, localType, true);
|
||||||
QCOMPARE(lib2Type, localType);
|
QT_TEST_EQUALITY_OPS(lib2Type, localType, true);
|
||||||
|
|
||||||
#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY)
|
||||||
if (actualTypeId < QMetaType::FirstGuiType && actualTypeId != QMetaType::Void) {
|
if (actualTypeId < QMetaType::FirstGuiType && actualTypeId != QMetaType::Void) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user