QDir: Use new comparison helper macros

QDir had operator==() and operator!=() defined as public member
functions, so use QT_CORE_REMOVED_SINCE and removed_api.cpp to get
rid of these methods and replace them with a hidden friend.

Use QTestPrivate::testEqualityOperators() helper function in unit-tests.

Task-number: QTBUG-120303
Change-Id: I86c2ba18b8b114efd9f62fc2fd628bc9065b04b2
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
bb10
Rym Bouabid 2024-02-09 17:37:09 +01:00
parent 3828b30951
commit 39505c86cc
5 changed files with 40 additions and 19 deletions

View File

@ -915,11 +915,17 @@ QUrl QUrl::fromEncoded(const QByteArray &input, ParsingMode mode)
#endif // QT_CORE_REMOVED_SINCE(6, 7)
#if QT_CORE_REMOVED_SINCE(6, 8)
#include "qdatastream.h" // inlined API
#include "qdir.h" // inlined API
bool QDir::operator==(const QDir &dir) const
{
return comparesEqual(*this, dir);
}
// #include "qotherheader.h"
// // implement removed functions from qotherheader.h
// order sections alphabetically to reduce chances of merge conflicts

View File

@ -1806,7 +1806,9 @@ bool QDir::makeAbsolute()
}
/*!
Returns \c true if directory \a dir and this directory have the same
\fn bool QDir::operator==(const QDir &lhs, const QDir &rhs)
Returns \c true if directory \a lhs and directory \a rhs have the same
path and their sort and filter settings are the same; otherwise
returns \c false.
@ -1814,10 +1816,10 @@ bool QDir::makeAbsolute()
\snippet code/src_corelib_io_qdir.cpp 10
*/
bool QDir::operator==(const QDir &dir) const
bool comparesEqual(const QDir &lhs, const QDir &rhs)
{
Q_D(const QDir);
const QDirPrivate *other = dir.d_ptr.constData();
const QDirPrivate *d = lhs.d_ptr.constData();
const QDirPrivate *other = rhs.d_ptr.constData();
if (d == other)
return true;
@ -1841,13 +1843,13 @@ bool QDir::operator==(const QDir &dir) const
if (d->dirEntry.filePath() == other->dirEntry.filePath())
return true;
if (exists()) {
if (!dir.exists())
if (lhs.exists()) {
if (!rhs.exists())
return false; //can't be equal if only one exists
// Both exist, fallback to expensive canonical path computation
return canonicalPath().compare(dir.canonicalPath(), sensitive) == 0;
return lhs.canonicalPath().compare(rhs.canonicalPath(), sensitive) == 0;
} else {
if (dir.exists())
if (rhs.exists())
return false; //can't be equal if only one exists
// Neither exists, compare absolute paths rather than canonical (which would be empty strings)
QString thisFilePath = d->resolveAbsoluteEntry();
@ -1877,11 +1879,10 @@ QDir &QDir::operator=(const QDir &dir)
*/
/*!
\fn bool QDir::operator!=(const QDir &dir) const
\fn bool QDir::operator!=(const QDir &lhs, const QDir &rhs)
Returns \c true if directory \a dir and this directory have different
paths or different sort or filter settings; otherwise returns
false.
Returns \c true if directory \a lhs and directory \a rhs have different
paths or different sort or filter settings; otherwise returns \c false.
Example:

View File

@ -4,6 +4,7 @@
#ifndef QDIR_H
#define QDIR_H
#include <QtCore/qcompare.h>
#include <QtCore/qstring.h>
#include <QtCore/qfile.h>
#include <QtCore/qfileinfo.h>
@ -185,8 +186,10 @@ public:
inline bool isAbsolute() const { return !isRelative(); }
bool makeAbsolute();
#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const QDir &dir) const;
inline bool operator!=(const QDir &dir) const { return !operator==(dir); }
#endif
bool remove(const QString &fileName);
bool rename(const QString &oldName, const QString &newName);
@ -237,6 +240,8 @@ protected:
QSharedDataPointer<QDirPrivate> d_ptr;
private:
friend Q_CORE_EXPORT bool comparesEqual(const QDir &lhs, const QDir &rhs);
Q_DECLARE_EQUALITY_COMPARABLE(QDir)
friend class QDirIterator;
// Q_DECLARE_PRIVATE equivalent for shared data pointers
QDirPrivate *d_func();

View File

@ -25,6 +25,7 @@ qt_internal_add_test(tst_qdir
tst_qdir.cpp
LIBRARIES
Qt::CorePrivate
Qt::TestPrivate
TESTDATA ${test_data}
)

View File

@ -3,6 +3,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QTemporaryFile>
#if QT_CONFIG(process)
#include <QProcess>
@ -126,6 +127,7 @@ private slots:
void normalizePathSegments();
#endif
void compareCompiles();
void compare();
void QDir_default();
@ -701,18 +703,24 @@ void tst_QDir::QDir_default()
QCOMPARE(dir.absolutePath(), QDir::currentPath());
}
void tst_QDir::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QDir>();
}
void tst_QDir::compare()
{
// operator==
// Not using QCOMPARE to test result of QDir::operator==
QDir dir;
dir.makeAbsolute();
QVERIFY(dir == QDir::currentPath());
QTestPrivate::testEqualityOperators(dir, QDir::currentPath(), true);
if (QTest::currentTestFailed())
return;
QCOMPARE(QDir(), QDir(QDir::currentPath()));
QVERIFY(QDir("../") == QDir(QDir::currentPath() + "/.."));
QTestPrivate::testEqualityOperators(QDir("../"), QDir(QDir::currentPath() + "/.."), true);
if (QTest::currentTestFailed())
return;
}
static QStringList filterLinks(QStringList &&list)