summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRym Bouabid <rym.bouabid@qt.io>2024-02-13 17:25:00 +0100
committerRym Bouabid <rym.bouabid@qt.io>2024-02-14 21:47:29 +0100
commitfec4984dc9e5ded182cab9662eca74d4eb3b12e2 (patch)
tree14ed384c6191ef7fe175b65e1024e016ca257043
parent99b9eb7177beefc70ea53579a3f88825751ca6d2 (diff)
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>
-rw-r--r--src/corelib/compat/removed_api.cpp7
-rw-r--r--src/corelib/io/qfileinfo.cpp31
-rw-r--r--src/corelib/io/qfileinfo.h5
-rw-r--r--tests/auto/corelib/io/qfileinfo/CMakeLists.txt1
-rw-r--r--tests/auto/corelib/io/qfileinfo/tst_qfileinfo.cpp9
5 files changed, 36 insertions, 17 deletions
diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp
index 2328d2235f..6aa0d1cee9 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 a12a228964..12f1da7634 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 15a119cb14..cb1ba8807d 100644
--- a/src/corelib/io/qfileinfo.h
+++ b/src/corelib/io/qfileinfo.h
@@ -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
{
diff --git a/tests/auto/corelib/io/qfileinfo/CMakeLists.txt b/tests/auto/corelib/io/qfileinfo/CMakeLists.txt
index 24b1c3972a..3b997e1bca 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 f202ad3a2f..6f74929432 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 <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()