QMimeType: use modernize comparisons

Replace class operators operator==(), operator!=() of
QMimeType to friend method comparesEqual() and
Q_DECLARE_EQUALITY_COMPARABLE macro.

Use QT_CORE_REMOVED_SINCE and removed_api.cpp to get rid of
current comparison methods and replace them with a friend.

Task-number: QTBUG-120304
Change-Id: I9776e98c8a3b14d599733c91af61fbc12b1f0e57
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
bb10
Tatiana Borisova 2024-04-19 14:43:25 +02:00
parent 9ed9ef5502
commit 5dbd6a44d8
5 changed files with 50 additions and 12 deletions

View File

@ -1006,6 +1006,13 @@ bool QJsonValue::operator!=(const QJsonValue &other) const
return !comparesEqual(*this, other);
}
#include "qmimetype.h"
bool QMimeType::operator==(const QMimeType &other) const
{
return comparesEqual(*this, other);
}
#include "qobject.h"
int QObject::startTimer(std::chrono::milliseconds time, Qt::TimerType timerType)

View File

@ -24,6 +24,7 @@ using namespace Qt::StringLiterals;
\brief The QMimeType class describes types of file or data, represented by a MIME type string.
\since 5.0
\compares equality
For instance a file named "readme.txt" has the MIME type "text/plain".
The MIME type can be determined from the file name, or from the file
@ -111,14 +112,15 @@ QMimeType::~QMimeType()
}
/*!
\fn bool QMimeType::operator==(const QMimeType &other) const;
Returns \c true if \a other equals this QMimeType object, otherwise returns \c false.
\fn bool QMimeType::operator==(const QMimeType &lhs, const QMimeType &rhs);
Returns \c true if \a lhs equals to the \a rhs QMimeType object, otherwise
returns \c false.
The name is the unique identifier for a mimetype, so two mimetypes with
the same name, are equal.
*/
bool QMimeType::operator==(const QMimeType &other) const
bool comparesEqual(const QMimeType &lhs, const QMimeType &rhs) noexcept
{
return d == other.d || d->name == other.d->name;
return lhs.d == rhs.d || lhs.d->name == rhs.d->name;
}
/*!
@ -134,8 +136,9 @@ size_t qHash(const QMimeType &key, size_t seed) noexcept
}
/*!
\fn bool QMimeType::operator!=(const QMimeType &other) const;
Returns \c true if \a other does not equal this QMimeType object, otherwise returns \c false.
\fn bool QMimeType::operator!=(const QMimeType &lhs, const QMimeType &rhs);
Returns \c true if QMimeType \a lhs is not equal to QMimeType \a rhs,
otherwise returns \c false.
*/
/*!

View File

@ -49,14 +49,14 @@ public:
}
explicit QMimeType(const QMimeTypePrivate &dd);
~QMimeType();
#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const QMimeType &other) const;
inline bool operator!=(const QMimeType &other) const
{
return !operator==(other);
}
#endif
bool isValid() const;
bool isDefault() const;
@ -86,6 +86,10 @@ protected:
friend Q_CORE_EXPORT size_t qHash(const QMimeType &key, size_t seed) noexcept;
QExplicitlySharedDataPointer<QMimeTypePrivate> d;
private:
friend Q_CORE_EXPORT bool comparesEqual(const QMimeType &lhs, const QMimeType &rhs) noexcept;
Q_DECLARE_EQUALITY_COMPARABLE(QMimeType)
};
Q_DECLARE_SHARED(QMimeType)

View File

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

View File

@ -8,7 +8,7 @@
#include <QVariantMap>
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
class tst_qmimetype : public QObject
{
@ -17,7 +17,9 @@ class tst_qmimetype : public QObject
private slots:
void initTestCase();
void compareCompiles();
void isValid();
void compareQMimetypes();
void name();
void genericIconName();
void iconName();
@ -41,6 +43,28 @@ static QString qMimeTypeName()
// ------------------------------------------------------------------------------------------------
void tst_qmimetype::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QMimeType>();
}
// ------------------------------------------------------------------------------------------------
void tst_qmimetype::compareQMimetypes()
{
QMimeType instantiatedQMimeType{ QMimeTypePrivate(qMimeTypeName()) };
QMimeType otherQMimeType (instantiatedQMimeType);
QMimeType defaultQMimeType;
QVERIFY(!defaultQMimeType.isValid());
QT_TEST_EQUALITY_OPS(defaultQMimeType, QMimeType(), true);
QT_TEST_EQUALITY_OPS(QMimeType(), QMimeType(), true);
QT_TEST_EQUALITY_OPS(instantiatedQMimeType, QMimeType(), false);
QT_TEST_EQUALITY_OPS(otherQMimeType, defaultQMimeType, false);
}
// ------------------------------------------------------------------------------------------------
void tst_qmimetype::isValid()
{
QMimeType instantiatedQMimeType{ QMimeTypePrivate(qMimeTypeName()) };
@ -49,7 +73,7 @@ void tst_qmimetype::isValid()
QMimeType otherQMimeType (instantiatedQMimeType);
QVERIFY(otherQMimeType.isValid());
QCOMPARE(instantiatedQMimeType, otherQMimeType);
QT_TEST_EQUALITY_OPS(instantiatedQMimeType, otherQMimeType, true);
QMimeType defaultQMimeType;
@ -66,8 +90,7 @@ void tst_qmimetype::name()
// Verify that the Name is part of the equality test:
QCOMPARE(instantiatedQMimeType.name(), qMimeTypeName());
QVERIFY(instantiatedQMimeType != otherQMimeType);
QVERIFY(!(instantiatedQMimeType == otherQMimeType));
QT_TEST_EQUALITY_OPS(instantiatedQMimeType, otherQMimeType, false);
}
// ------------------------------------------------------------------------------------------------