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
\compares equality
The class provides a number of primitive container operations, using void*
as operands. This way, you can manipulate a generic container retrieved from
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
\relates QMetaSequence
Returns \c true if the QMetaSequence \a a represents the same container type
as the QMetaSequence \a b, otherwise returns \c false.
Returns \c true if the QMetaSequence \a lhs represents the same container type
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
\relates QMetaSequence
Returns \c true if the QMetaSequence \a a represents a different container
type than the QMetaSequence \a b, otherwise returns \c false.
Returns \c true if the QMetaSequence \a lhs represents a different container
type than the QMetaSequence \a rhs, otherwise returns \c false.
*/

View File

@ -5,6 +5,7 @@
#define QMETACONTAINER_H
#include <QtCore/qcontainerinfo.h>
#include <QtCore/qcompare.h>
#include <QtCore/qflags.h>
#include <QtCore/qglobal.h>
@ -975,18 +976,15 @@ public:
bool canGetValueAtConstIterator() 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(); }
private:
friend bool comparesEqual(const QMetaSequence &lhs, const QMetaSequence &rhs) noexcept
{
return lhs.d() == rhs.d();
}
Q_DECLARE_EQUALITY_COMPARABLE(QMetaSequence)
template<typename T>
struct MetaSequence
{
@ -1171,18 +1169,15 @@ public:
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(); }
private:
friend bool comparesEqual(const QMetaAssociation &lhs, const QMetaAssociation &rhs) noexcept
{
return lhs.d() == rhs.d();
}
Q_DECLARE_EQUALITY_COMPARABLE(QMetaAssociation)
template<typename T>
struct MetaAssociation
{

View File

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

View File

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