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:
parent
99b9eb7177
commit
fec4984dc9
@ -926,6 +926,13 @@ bool QDir::operator==(const QDir &dir) const
|
|||||||
return comparesEqual(*this, dir);
|
return comparesEqual(*this, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "qfileinfo.h" // inlined API
|
||||||
|
|
||||||
|
bool QFileInfo::operator==(const QFileInfo &fileinfo) const
|
||||||
|
{
|
||||||
|
return comparesEqual(*this, fileinfo);
|
||||||
|
}
|
||||||
|
|
||||||
// #include "qotherheader.h"
|
// #include "qotherheader.h"
|
||||||
// // implement removed functions from qotherheader.h
|
// // implement removed functions from qotherheader.h
|
||||||
// order sections alphabetically to reduce chances of merge conflicts
|
// order sections alphabetically to reduce chances of merge conflicts
|
||||||
|
@ -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
|
Returns \c true if QFileInfo \a lhs refers to a different file system
|
||||||
entry than the one referred to by \a fileinfo; otherwise returns \c false.
|
entry than the one referred to by \a rhs; otherwise returns \c false.
|
||||||
|
|
||||||
\sa operator==()
|
\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.
|
entry on the file system; otherwise returns \c false.
|
||||||
|
|
||||||
Note that the result of comparing two empty QFileInfo objects, containing
|
Note that the result of comparing two empty QFileInfo objects, containing
|
||||||
@ -443,34 +445,31 @@ QFileInfo::~QFileInfo()
|
|||||||
|
|
||||||
\sa operator!=()
|
\sa operator!=()
|
||||||
*/
|
*/
|
||||||
bool QFileInfo::operator==(const QFileInfo &fileinfo) const
|
bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs)
|
||||||
{
|
{
|
||||||
Q_D(const QFileInfo);
|
if (rhs.d_ptr == lhs.d_ptr)
|
||||||
// ### Qt 5: understand long and short file names on Windows
|
|
||||||
// ### (GetFullPathName()).
|
|
||||||
if (fileinfo.d_ptr == d_ptr)
|
|
||||||
return true;
|
return true;
|
||||||
if (d->isDefaultConstructed || fileinfo.d_ptr->isDefaultConstructed)
|
if (lhs.d_ptr->isDefaultConstructed || rhs.d_ptr->isDefaultConstructed)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
// Assume files are the same if path is the same
|
// 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;
|
return true;
|
||||||
|
|
||||||
Qt::CaseSensitivity sensitive;
|
Qt::CaseSensitivity sensitive;
|
||||||
if (d->fileEngine == nullptr || fileinfo.d_ptr->fileEngine == nullptr) {
|
if (lhs.d_ptr->fileEngine == nullptr || rhs.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 != rhs.d_ptr->fileEngine) // one is native, the other is a custom file-engine
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
sensitive = QFileSystemEngine::isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
sensitive = QFileSystemEngine::isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;
|
||||||
} else {
|
} else {
|
||||||
if (d->fileEngine->caseSensitive() != fileinfo.d_ptr->fileEngine->caseSensitive())
|
if (lhs.d_ptr->fileEngine->caseSensitive() != rhs.d_ptr->fileEngine->caseSensitive())
|
||||||
return false;
|
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
|
// Fallback to expensive canonical path computation
|
||||||
return canonicalFilePath().compare(fileinfo.canonicalFilePath(), sensitive) == 0;
|
return lhs.canonicalFilePath().compare(rhs.canonicalFilePath(), sensitive) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#ifndef QFILEINFO_H
|
#ifndef QFILEINFO_H
|
||||||
#define QFILEINFO_H
|
#define QFILEINFO_H
|
||||||
|
|
||||||
|
#include <QtCore/qcompare.h>
|
||||||
#include <QtCore/qfile.h>
|
#include <QtCore/qfile.h>
|
||||||
#include <QtCore/qlist.h>
|
#include <QtCore/qlist.h>
|
||||||
#include <QtCore/qshareddata.h>
|
#include <QtCore/qshareddata.h>
|
||||||
@ -58,8 +59,10 @@ public:
|
|||||||
void swap(QFileInfo &other) noexcept
|
void swap(QFileInfo &other) noexcept
|
||||||
{ d_ptr.swap(other.d_ptr); }
|
{ d_ptr.swap(other.d_ptr); }
|
||||||
|
|
||||||
|
#if QT_CORE_REMOVED_SINCE(6, 8)
|
||||||
bool operator==(const QFileInfo &fileinfo) const;
|
bool operator==(const QFileInfo &fileinfo) const;
|
||||||
inline bool operator!=(const QFileInfo &fileinfo) const { return !(operator==(fileinfo)); }
|
inline bool operator!=(const QFileInfo &fileinfo) const { return !(operator==(fileinfo)); }
|
||||||
|
#endif
|
||||||
|
|
||||||
void setFile(const QString &file);
|
void setFile(const QString &file);
|
||||||
void setFile(const QFileDevice &file);
|
void setFile(const QFileDevice &file);
|
||||||
@ -171,6 +174,8 @@ protected:
|
|||||||
QSharedDataPointer<QFileInfoPrivate> d_ptr;
|
QSharedDataPointer<QFileInfoPrivate> d_ptr;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
friend Q_CORE_EXPORT bool comparesEqual(const QFileInfo &lhs, const QFileInfo &rhs);
|
||||||
|
Q_DECLARE_EQUALITY_COMPARABLE(QFileInfo)
|
||||||
QFileInfoPrivate* d_func();
|
QFileInfoPrivate* d_func();
|
||||||
inline const QFileInfoPrivate* d_func() const
|
inline const QFileInfoPrivate* d_func() const
|
||||||
{
|
{
|
||||||
|
@ -16,6 +16,7 @@ qt_internal_add_test(tst_qfileinfo
|
|||||||
tst_qfileinfo.cpp
|
tst_qfileinfo.cpp
|
||||||
LIBRARIES
|
LIBRARIES
|
||||||
Qt::CorePrivate
|
Qt::CorePrivate
|
||||||
|
Qt::TestPrivate
|
||||||
)
|
)
|
||||||
|
|
||||||
# Resources:
|
# Resources:
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
|
||||||
|
|
||||||
#include <QTest>
|
#include <QTest>
|
||||||
|
#include <QtTest/private/qcomparisontesthelper_p.h>
|
||||||
#include <QStandardPaths>
|
#include <QStandardPaths>
|
||||||
#include <QScopeGuard>
|
#include <QScopeGuard>
|
||||||
|
|
||||||
@ -172,6 +173,7 @@ private slots:
|
|||||||
|
|
||||||
void systemFiles();
|
void systemFiles();
|
||||||
|
|
||||||
|
void compareCompiles();
|
||||||
void compare_data();
|
void compare_data();
|
||||||
void compare();
|
void compare();
|
||||||
|
|
||||||
@ -993,6 +995,11 @@ void tst_QFileInfo::systemFiles()
|
|||||||
QVERIFY(fi.birthTime() <= fi.lastModified());
|
QVERIFY(fi.birthTime() <= fi.lastModified());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void tst_QFileInfo::compareCompiles()
|
||||||
|
{
|
||||||
|
QTestPrivate::testEqualityOperatorsCompile<QFileInfo>();
|
||||||
|
}
|
||||||
|
|
||||||
void tst_QFileInfo::compare_data()
|
void tst_QFileInfo::compare_data()
|
||||||
{
|
{
|
||||||
QTest::addColumn<QString>("file1");
|
QTest::addColumn<QString>("file1");
|
||||||
@ -1037,7 +1044,7 @@ void tst_QFileInfo::compare()
|
|||||||
QFETCH(QString, file2);
|
QFETCH(QString, file2);
|
||||||
QFETCH(bool, same);
|
QFETCH(bool, same);
|
||||||
QFileInfo fi1(file1), fi2(file2);
|
QFileInfo fi1(file1), fi2(file2);
|
||||||
QCOMPARE(fi1 == fi2, same);
|
QT_TEST_EQUALITY_OPS(fi1, fi2, same);
|
||||||
}
|
}
|
||||||
|
|
||||||
void tst_QFileInfo::consistent_data()
|
void tst_QFileInfo::consistent_data()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user