Skip to content

Commit

Permalink
QMimeType: use modernize comparisons
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
qt-tatiana committed Apr 30, 2024
1 parent 9ed9ef5 commit 5dbd6a4
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 12 deletions.
7 changes: 7 additions & 0 deletions src/corelib/compat/removed_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 9 additions & 6 deletions src/corelib/mimetypes/qmimetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/*!
Expand All @@ -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.
*/

/*!
Expand Down
8 changes: 6 additions & 2 deletions src/corelib/mimetypes/qmimetype.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@ class Q_CORE_EXPORT QMimeType
}
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;
Expand Down Expand Up @@ -86,6 +86,10 @@ class Q_CORE_EXPORT QMimeType
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)
Expand Down
1 change: 1 addition & 0 deletions tests/auto/corelib/mimetypes/qmimetype/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ qt_internal_add_test(tst_qmimetype
tst_qmimetype.cpp
LIBRARIES
Qt::CorePrivate
Qt::TestPrivate
)
31 changes: 27 additions & 4 deletions tests/auto/corelib/mimetypes/qmimetype/tst_qmimetype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <QVariantMap>

#include <QTest>

#include <QtTest/private/qcomparisontesthelper_p.h>

class tst_qmimetype : public QObject
{
Expand All @@ -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();
Expand All @@ -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()) };
Expand All @@ -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;

Expand All @@ -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);
}

// ------------------------------------------------------------------------------------------------
Expand Down

0 comments on commit 5dbd6a4

Please sign in to comment.