QMetaSequence and QMetaAssociation: use new comparison helper macros

Replace public friend operators operator==(), operator!=() of
QMetaSequence and QMetaAssociation
classes to friend methods comparesEqual() and
Q_DECLARE_EQUALITY_COMPARABLE macroses.

Task-number: QTBUG-120304
Change-Id: I88e9b228220d36092437bfb71ae2f053d2e99fdf
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Tatiana Borisova 2024-04-18 19:00:03 +02:00
parent 4fe3337394
commit de0f145022
4 changed files with 36 additions and 27 deletions

View File

@ -14,6 +14,8 @@ QT_BEGIN_NAMESPACE
\ingroup objectmodel \ingroup objectmodel
\compares equality
The class provides a number of primitive container operations, using void* The class provides a number of primitive container operations, using void*
as operands. This way, you can manipulate a generic container retrieved from as operands. This way, you can manipulate a generic container retrieved from
a Variant without knowing its type. a Variant without knowing its type.
@ -790,21 +792,19 @@ void QMetaSequence::valueAtConstIterator(const void *iterator, void *result) con
} }
/*! /*!
\fn bool operator==(QMetaSequence a, QMetaSequence b) \fn bool QMetaSequence::operator==(const QMetaSequence &lhs, const QMetaSequence &rhs)
\since 6.0 \since 6.0
\relates QMetaSequence
Returns \c true if the QMetaSequence \a a represents the same container type Returns \c true if the QMetaSequence \a lhs represents the same container type
as the QMetaSequence \a b, otherwise returns \c false. as the QMetaSequence \a rhs, otherwise returns \c false.
*/ */
/*! /*!
\fn bool operator!=(QMetaSequence a, QMetaSequence b) \fn bool QMetaSequence::operator!=(const QMetaSequence &lhs, const QMetaSequence &rhs)
\since 6.0 \since 6.0
\relates QMetaSequence
Returns \c true if the QMetaSequence \a a represents a different container Returns \c true if the QMetaSequence \a lhs represents a different container
type than the QMetaSequence \a b, otherwise returns \c false. type than the QMetaSequence \a rhs, otherwise returns \c false.
*/ */

View File

@ -5,6 +5,7 @@
#define QMETACONTAINER_H #define QMETACONTAINER_H
#include <QtCore/qcontainerinfo.h> #include <QtCore/qcontainerinfo.h>
#include <QtCore/qcompare.h>
#include <QtCore/qflags.h> #include <QtCore/qflags.h>
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
@ -975,18 +976,15 @@ public:
bool canGetValueAtConstIterator() const; bool canGetValueAtConstIterator() const;
void valueAtConstIterator(const void *iterator, void *result) const; void valueAtConstIterator(const void *iterator, void *result) const;
friend bool operator==(const QMetaSequence &a, const QMetaSequence &b)
{
return a.d() == b.d();
}
friend bool operator!=(const QMetaSequence &a, const QMetaSequence &b)
{
return a.d() != b.d();
}
const QtMetaContainerPrivate::QMetaSequenceInterface *iface() const { return d(); } const QtMetaContainerPrivate::QMetaSequenceInterface *iface() const { return d(); }
private: private:
friend bool comparesEqual(const QMetaSequence &lhs, const QMetaSequence &rhs) noexcept
{
return lhs.d() == rhs.d();
}
Q_DECLARE_EQUALITY_COMPARABLE(QMetaSequence)
template<typename T> template<typename T>
struct MetaSequence struct MetaSequence
{ {
@ -1171,18 +1169,15 @@ public:
return nullptr; return nullptr;
} }
friend bool operator==(const QMetaAssociation &a, const QMetaAssociation &b)
{
return a.d() == b.d();
}
friend bool operator!=(const QMetaAssociation &a, const QMetaAssociation &b)
{
return a.d() != b.d();
}
const QtMetaContainerPrivate::QMetaAssociationInterface *iface() const { return d(); } const QtMetaContainerPrivate::QMetaAssociationInterface *iface() const { return d(); }
private: private:
friend bool comparesEqual(const QMetaAssociation &lhs, const QMetaAssociation &rhs) noexcept
{
return lhs.d() == rhs.d();
}
Q_DECLARE_EQUALITY_COMPARABLE(QMetaAssociation)
template<typename T> template<typename T>
struct MetaAssociation struct MetaAssociation
{ {

View File

@ -16,4 +16,5 @@ qt_internal_add_test(tst_qmetacontainer
tst_qmetacontainer.cpp tst_qmetacontainer.cpp
LIBRARIES LIBRARIES
Qt::CorePrivate Qt::CorePrivate
Qt::TestPrivate
) )

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QtTest/qtest.h> #include <QtTest/qtest.h>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QtCore/qcontainerinfo.h> #include <QtCore/qcontainerinfo.h>
#include <QtCore/qmetacontainer.h> #include <QtCore/qmetacontainer.h>
#include <QtCore/QMap> #include <QtCore/QMap>
@ -157,6 +158,7 @@ private:
private slots: private slots:
void init(); void init();
void compareCompiles();
void testSequence_data(); void testSequence_data();
void testSequence(); void testSequence();
@ -203,6 +205,12 @@ void tst_QMetaContainer::init()
}; };
} }
void tst_QMetaContainer::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QMetaSequence>();
QTestPrivate::testEqualityOperatorsCompile<QMetaAssociation>();
}
void tst_QMetaContainer::cleanup() void tst_QMetaContainer::cleanup()
{ {
qvector.clear(); qvector.clear();
@ -501,6 +509,9 @@ void tst_QMetaContainer::testSequence()
QVERIFY(metaSequence.iface() != nullptr); QVERIFY(metaSequence.iface() != nullptr);
QMetaSequence defaultConstructed; QMetaSequence defaultConstructed;
QVERIFY(defaultConstructed.iface() == nullptr); QVERIFY(defaultConstructed.iface() == nullptr);
QT_TEST_EQUALITY_OPS(QMetaSequence(), defaultConstructed, true);
QT_TEST_EQUALITY_OPS(QMetaSequence(), QMetaSequence(), true);
QT_TEST_EQUALITY_OPS(defaultConstructed, metaSequence, false);
} }
void tst_QMetaContainer::testAssociation_data() void tst_QMetaContainer::testAssociation_data()
@ -728,8 +739,10 @@ void tst_QMetaContainer::testAssociation()
metaAssociation.destroyConstIterator(constEnd); metaAssociation.destroyConstIterator(constEnd);
QVERIFY(metaAssociation.iface() != nullptr); QVERIFY(metaAssociation.iface() != nullptr);
QMetaSequence defaultConstructed; QMetaAssociation defaultConstructed;
QVERIFY(defaultConstructed.iface() == nullptr); QVERIFY(defaultConstructed.iface() == nullptr);
QT_TEST_EQUALITY_OPS(QMetaAssociation(), QMetaAssociation(), true);
QT_TEST_EQUALITY_OPS(QMetaAssociation(), metaAssociation, false);
} }
QTEST_MAIN(tst_QMetaContainer) QTEST_MAIN(tst_QMetaContainer)