QFileInfo: Use new comparison helper macros

QFileInfo 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 QT_TEST_ALL_EQUALITY_OPS macro in unit-tests.

Task-number: QTBUG-120303
Change-Id: Ie290df230b0f608a0965dccba9184382291cad8e
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
This commit is contained in:
Rym Bouabid 2024-02-13 17:25:00 +01:00
parent 99b9eb7177
commit fec4984dc9
5 changed files with 36 additions and 17 deletions

View File

@ -926,6 +926,13 @@ bool QDir::operator==(const QDir &dir) const
return comparesEqual(*this, dir);
}
#include "qfileinfo.h" // inlined API
bool QFileInfo::operator==(const QFileInfo &fileinfo) const
{
return comparesEqual(*this, fileinfo);
}
// #include "qotherheader.h"
// // implement removed functions from qotherheader.h
// order sections alphabetically to reduce chances of merge conflicts

View File

@ -419,16 +419,18 @@ QFileInfo::~QFileInfo()
}
/*!
\fn bool QFileInfo::operator!=(const QFileInfo &fileinfo) const
\fn bool QFileInfo::operator!=(const QFileInfo &lhs, const QFileInfo &rhs)
Returns \c true if this QFileInfo refers to a different file system
entry than the one referred to by \a fileinfo; otherwise returns \c false.
Returns \c true if QFileInfo \a lhs refers to a different file system
entry than the one referred to by \a rhs; otherwise returns \c false.
\sa operator==()
*/
/*!
Returns \c true if this QFileInfo and \a fileinfo refer to the same
\fn bool QFileInfo::operator==(const QFileInfo &lhs, const QFileInfo &rhs)
Returns \c true if QFileInfo \a lhs and QFileInfo \a rhs refer to the same
entry on the file system; otherwise returns \c false.
Note that the result of comparing two empty QFileInfo objects, containing
@ -443,34 +445,31 @@ QFileInfo::~QFileInfo()
\sa operator!=()
*/
bool QFileInfo::operator==(const QFileInfo &fileinfo) const
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
{
Q_D(const QFileInfo);
// ### Qt 5: understand long and short file names on Windows
// ### (GetFullPathName()).
if (fileinfo.d_ptr == d_ptr)
if (rhs.d_ptr == lhs.d_ptr)
return true;
if (d->isDefaultConstructed || fileinfo.d_ptr->isDefaultConstructed)
if (lhs.d_ptr->isDefaultConstructed || rhs.d_ptr->isDefaultConstructed)
return false;
// Assume files are the same if path is the same
if (d->fileEntry.filePath() == fileinfo.d_ptr->fileEntry.filePath())
if (lhs.d_ptr->fileEntry.filePath() == rhs.d_ptr->fileEntry.filePath())
return true;
Qt::CaseSensitivity sensitive;
if (d->fileEngine == nullptr || fileinfo.d_ptr->fileEngine == nullptr) {
if (d->fileEngine != fileinfo.d_ptr->fileEngine) // one is native, the other is a custom file-engine
if (lhs.d_ptr->fileEngine == nullptr || rhs.d_ptr->fileEngine == nullptr) {
if (lhs.d_ptr->fileEngine != rhs.d_ptr->fileEngine) // one is native, the other is a custom file-engine
return false;
sensitive = QFileSystemEngine::isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
} else {
if (d->fileEngine->caseSensitive() != fileinfo.d_ptr->fileEngine->caseSensitive())
if (lhs.d_ptr->fileEngine->caseSensitive() != rhs.d_ptr->fileEngine->caseSensitive())
return false;
sensitive = d->fileEngine->caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
sensitive = lhs.d_ptr->fileEngine->caseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
}
// Fallback to expensive canonical path computation
return canonicalFilePath().compare(fileinfo.canonicalFilePath(), sensitive) == 0;
return lhs.canonicalFilePath().compare(rhs.canonicalFilePath(), sensitive) == 0;
}
/*!

View File

@ -4,6 +4,7 @@
#ifndef QFILEINFO_H
#define QFILEINFO_H
#include <QtCore/qcompare.h>
#include <QtCore/qfile.h>
#include <QtCore/qlist.h>
#include <QtCore/qshareddata.h>
@ -58,8 +59,10 @@ public:
void swap(QFileInfo &other) noexcept
{ d_ptr.swap(other.d_ptr); }
#if QT_CORE_REMOVED_SINCE(6, 8)
bool operator==(const QFileInfo &fileinfo) const;
inline bool operator!=(const QFileInfo &fileinfo) const { return !(operator==(fileinfo)); }
#endif
void setFile(const QString &file);
void setFile(const QFileDevice &file);
@ -171,6 +174,8 @@ protected:
QSharedDataPointer<QFileInfoPrivate> d_ptr;
private:
friend Q_CORE_EXPORT bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs);
Q_DECLARE_EQUALITY_COMPARABLE(QFileInfo)
QFileInfoPrivate* d_func();
inline const QFileInfoPrivate* d_func() const
{

View File

@ -16,6 +16,7 @@ qt_internal_add_test(tst_qfileinfo
tst_qfileinfo.cpp
LIBRARIES
Qt::CorePrivate
Qt::TestPrivate
)
# Resources:

View File

@ -2,6 +2,7 @@
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include <QTest>
#include <QtTest/private/qcomparisontesthelper_p.h>
#include <QStandardPaths>
#include <QScopeGuard>
@ -172,6 +173,7 @@ private slots:
void systemFiles();
void compareCompiles();
void compare_data();
void compare();
@ -993,6 +995,11 @@ void tst_QFileInfo::systemFiles()
QVERIFY(fi.birthTime() <= fi.lastModified());
}
void tst_QFileInfo::compareCompiles()
{
QTestPrivate::testEqualityOperatorsCompile<QFileInfo>();
}
void tst_QFileInfo::compare_data()
{
QTest::addColumn<QString>("file1");
@ -1037,7 +1044,7 @@ void tst_QFileInfo::compare()
QFETCH(QString, file2);
QFETCH(bool, same);
QFileInfo fi1(file1), fi2(file2);
QCOMPARE(fi1 == fi2, same);
QT_TEST_EQUALITY_OPS(fi1, fi2, same);
}
void tst_QFileInfo::consistent_data()