diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index 2328d2235f2..6aa0d1cee95 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -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 diff --git a/src/corelib/io/qfileinfo.cpp b/src/corelib/io/qfileinfo.cpp index a12a228964f..12f1da76347 100644 --- a/src/corelib/io/qfileinfo.cpp +++ b/src/corelib/io/qfileinfo.cpp @@ -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; } /*! diff --git a/src/corelib/io/qfileinfo.h b/src/corelib/io/qfileinfo.h index 15a119cb142..cb1ba8807d3 100644 --- a/src/corelib/io/qfileinfo.h +++ b/src/corelib/io/qfileinfo.h @@ -4,6 +4,7 @@ #ifndef QFILEINFO_H #define QFILEINFO_H +#include #include #include #include @@ -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 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 { diff --git a/tests/auto/corelib/io/qfileinfo/CMakeLists.txt b/tests/auto/corelib/io/qfileinfo/CMakeLists.txt index 24b1c3972a9..3b997e1bcad 100644 --- a/tests/auto/corelib/io/qfileinfo/CMakeLists.txt +++ b/tests/auto/corelib/io/qfileinfo/CMakeLists.txt @@ -16,6 +16,7 @@ qt_internal_add_test(tst_qfileinfo tst_qfileinfo.cpp LIBRARIES Qt::CorePrivate + Qt::TestPrivate ) # Resources: diff --git a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp index f202ad3a2f4..6f749294328 100644 --- a/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp +++ b/tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include +#include #include #include @@ -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(); +} + void tst_QFileInfo::compare_data() { QTest::addColumn("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()