Adpat to changes in QAbstractFileEngine/Iterator in qtbase

- Replace hasNext()/next() with a `bool advance()` method
- Replace QAFE::FileTime with QFile::FileTime, they are identical
- QAFE methods now return std::unique_ptr, which matches reality as call
  sites stored the pointers in a std::unique_ptr anyway

FileIteratorWrapper:
State::BaseIteratorEnd is only needed when using hasNext()/next(),
hasNext() changes m_status to State::BaseIteratorEnd, then next() checks
that and changes m_status to State::Ended; this isn't needed with
advance() since it's only one method.

Change-Id: I4414f334715237a2fc13ace6f4733d975e2cfaa3
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
Ahmad Samir
2023-12-26 10:51:11 +02:00
parent faa595e38b
commit ba3f0bc841
9 changed files with 155 additions and 18 deletions

View File

@@ -8,6 +8,7 @@
#include <QFileInfo>
#include <QString>
#include <QtVersionChecks>
#include <QtCore/private/qabstractfileengine_p.h>
@@ -17,14 +18,32 @@ namespace Internal {
class DirIterator : public QAbstractFileEngineIterator
{
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
DirIterator(FilePaths paths,
const QString &path,
QDir::Filters filters,
const QStringList &filterNames)
: QAbstractFileEngineIterator(path, filters, filterNames)
#else
DirIterator(FilePaths paths)
: QAbstractFileEngineIterator({}, {})
#endif
, m_filePaths(std::move(paths))
, it(m_filePaths.begin())
{}
// QAbstractFileEngineIterator interface
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool advance() override
{
if (!m_filePaths.empty() && m_filePaths.end() != it + 1) {
++it;
return true;
}
return false;
}
#else
QString next() override
{
if (it == m_filePaths.end())
@@ -35,6 +54,7 @@ public:
}
bool hasNext() const override { return !m_filePaths.empty() && m_filePaths.end() != it + 1; }
#endif // QT_VERSION_CHECK(6, 8, 0)
QString currentFileName() const override
{

View File

@@ -6,11 +6,13 @@
#include "../filepath.h"
#include "../hostosinfo.h"
#include <QtVersionChecks>
#include <QtCore/private/qabstractfileengine_p.h>
namespace Utils {
namespace Internal {
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
// Based on http://bloglitb.blogspot.com/2011/12/access-to-private-members-safer.htm
template<typename Tag, typename Tag::type M>
struct PrivateAccess
@@ -25,6 +27,7 @@ struct QAFEITag
};
template struct PrivateAccess<QAFEITag, &QAbstractFileEngineIterator::setPath>;
#endif
class FileIteratorWrapper : public QAbstractFileEngineIterator
{
@@ -37,11 +40,38 @@ class FileIteratorWrapper : public QAbstractFileEngineIterator
public:
FileIteratorWrapper(std::unique_ptr<QAbstractFileEngineIterator> &&baseIterator)
: QAbstractFileEngineIterator(baseIterator->filters(), baseIterator->nameFilters())
: QAbstractFileEngineIterator(
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
baseIterator->path(),
#endif
baseIterator->filters(), baseIterator->nameFilters())
, m_baseIterator(std::move(baseIterator))
{}
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
// Can be called in the constructor since the iterator path
// has already been set
setStatus();
#endif
}
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
bool advance() override
{
if (m_status == State::Ended)
return false;
const bool res = m_baseIterator->advance();
if (m_status == State::IteratingRoot && !res) {
// m_baseIterator finished, but we need to advance one last time, so that
// currentFileName() returns FilePath::specialRootPath().
m_status = State::Ended;
return true;
}
return res;
}
#else
QString next() override
{
if (m_status == State::Ended)
@@ -71,6 +101,7 @@ public:
return res;
}
#endif
QString currentFileName() const override
{
return m_status == State::Ended ? FilePath::specialRootPath()
@@ -94,6 +125,7 @@ private:
return p;
}
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
void setPath() const
{
if (!m_hasSetPath) {
@@ -102,10 +134,15 @@ private:
m_hasSetPath = true;
}
}
#endif
private:
std::unique_ptr<QAbstractFileEngineIterator> m_baseIterator;
#if QT_VERSION < QT_VERSION_CHECK(6, 8, 0)
mutable bool m_hasSetPath{false};
#endif
mutable State m_status{State::NotIteratingRoot};
};

View File

@@ -9,6 +9,7 @@
#include "../stringutils.h"
#include <QtCore/private/qabstractfileengine_p.h>
#include <QtVersionChecks>
namespace Utils {
namespace Internal {
@@ -63,10 +64,20 @@ public:
return QAbstractFileEngine::fileName(file);
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
IteratorUniquePtr beginEntryList(const QString &path,
QDir::Filters filters,
const QStringList &filterNames) override
{
return std::make_unique<DirIterator>(m_children, path, filters, filterNames);
}
#else
Iterator *beginEntryList(QDir::Filters /*filters*/, const QStringList & /*filterNames*/) override
{
return new DirIterator(m_children);
}
#endif
};
} // namespace Internal

View File

@@ -295,8 +295,14 @@ bool FSEngineImpl::cloneTo(QAbstractFileEngine *target)
return QAbstractFileEngine::cloneTo(target);
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
QAbstractFileEngine::IteratorUniquePtr FSEngineImpl::beginEntryList(const QString &path,
QDir::Filters filters,
const QStringList &filterNames)
#else
QAbstractFileEngine::Iterator *FSEngineImpl::beginEntryList(QDir::Filters filters,
const QStringList &filterNames)
#endif
{
FilePaths paths{m_filePath.pathAppended(".")};
m_filePath.iterateDirectory(
@@ -310,12 +316,11 @@ QAbstractFileEngine::Iterator *FSEngineImpl::beginEntryList(QDir::Filters filter
},
{filterNames, filters});
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::make_unique<DirIterator>(std::move(paths), path, filters, filterNames);
#else
return new DirIterator(std::move(paths));
}
QAbstractFileEngine::Iterator *FSEngineImpl::endEntryList()
{
return nullptr;
#endif
}
qint64 FSEngineImpl::read(char *data, qint64 maxlen)

View File

@@ -8,6 +8,7 @@
#include <QtCore/private/qabstractfileengine_p.h>
#include <QTemporaryFile>
#include <QtVersionChecks>
namespace Utils {
namespace Internal {
@@ -46,13 +47,25 @@ public:
QString fileName(FileName file) const override;
uint ownerId(FileOwner) const override;
QString owner(FileOwner) const override;
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
using FileTime = QFile::FileTime;
#endif
bool setFileTime(const QDateTime &newDate, FileTime time) override;
QDateTime fileTime(FileTime time) const override;
void setFileName(const QString &file) override;
int handle() const override;
bool cloneTo(QAbstractFileEngine *target) override;
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
IteratorUniquePtr beginEntryList(const QString &path, QDir::Filters filters,
const QStringList &filterNames) override;
IteratorUniquePtr endEntryList() override { return {}; }
#else
Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames) override;
Iterator *endEntryList() override;
Iterator *endEntryList() override { return nullptr; }
#endif
qint64 read(char *data, qint64 maxlen) override;
qint64 readLine(char *data, qint64 maxlen) override;
qint64 write(const char *data, qint64 len) override;

View File

@@ -46,10 +46,15 @@ static bool isRootPath(const QString &fileName)
return fileName.size() == 1 && fileName[0] == '/';
}
QAbstractFileEngine *FSEngineHandler::create(const QString &fileName) const
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine>
#else
QAbstractFileEngine *
#endif
FSEngineHandler::create(const QString &fileName) const
{
if (fileName.startsWith(':'))
return nullptr;
return {};
static const QString rootPath = FilePath::specialRootPath();
static const FilePath rootFilePath = FilePath::fromString(rootPath);
@@ -62,7 +67,11 @@ QAbstractFileEngine *FSEngineHandler::create(const QString &fileName) const
return rootFilePath.pathAppended(scheme);
});
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::make_unique<FixedListFSEngine>(removeDoubleSlash(fileName), paths);
#else
return new FixedListFSEngine(removeDoubleSlash(fileName), paths);
#endif
}
if (fixedFileName.startsWith(rootPath)) {
@@ -74,20 +83,35 @@ QAbstractFileEngine *FSEngineHandler::create(const QString &fileName) const
return root.scheme() == scheme;
});
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::make_unique<FixedListFSEngine>(removeDoubleSlash(fileName),
filteredRoots);
#else
return new FixedListFSEngine(removeDoubleSlash(fileName), filteredRoots);
#endif
}
}
FilePath fixedPath = FilePath::fromString(fixedFileName);
if (fixedPath.needsDevice())
if (fixedPath.needsDevice()) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::make_unique<FSEngineImpl>(removeDoubleSlash(fileName));
#else
return new FSEngineImpl(removeDoubleSlash(fileName));
#endif
}
}
if (isRootPath(fixedFileName))
if (isRootPath(fixedFileName)) {
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::make_unique<RootInjectFSEngine>(fileName);
#else
return new RootInjectFSEngine(fileName);
#endif
}
return nullptr;
return {};
}
} // Utils::Internal

View File

@@ -10,7 +10,11 @@ namespace Utils::Internal {
class FSEngineHandler : public QAbstractFileEngineHandler
{
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine> create(const QString &fileName) const override;
#else
QAbstractFileEngine *create(const QString &fileName) const override;
#endif
};
} // Utils::Internal

View File

@@ -16,12 +16,22 @@ public:
using QFSFileEngine::QFSFileEngine;
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
IteratorUniquePtr beginEntryList(const QString &path,
QDir::Filters filters,
const QStringList &filterNames) override
{
return std::make_unique<FileIteratorWrapper>(
QFSFileEngine::beginEntryList(path, filters, filterNames));
}
#else
Iterator *beginEntryList(QDir::Filters filters, const QStringList &filterNames) override
{
std::unique_ptr<QAbstractFileEngineIterator> baseIterator(
QFSFileEngine::beginEntryList(filters, filterNames));
return new FileIteratorWrapper(std::move(baseIterator));
}
#endif
};
} // namespace Internal

View File

@@ -473,16 +473,25 @@ ComponentCompleteDisabler::~ComponentCompleteDisabler()
class QrcEngineHandler : public QAbstractFileEngineHandler
{
public:
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine> create(const QString &fileName) const final;
#else
QAbstractFileEngine *create(const QString &fileName) const final;
#endif
};
QAbstractFileEngine *QrcEngineHandler::create(const QString &fileName) const
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
std::unique_ptr<QAbstractFileEngine>
#else
QAbstractFileEngine *
#endif
QrcEngineHandler::create(const QString &fileName) const
{
if (fileName.startsWith(":/qt-project.org"))
return nullptr;
return {};
if (fileName.startsWith(":/qtquickplugin"))
return nullptr;
return {};
if (fileName.startsWith(":/")) {
const QStringList searchPaths = qmlDesignerRCPath().split(';');
@@ -493,18 +502,22 @@ QAbstractFileEngine *QrcEngineHandler::create(const QString &fileName) const
fixedPath.replace(":" + qrcDefintion.first(), qrcDefintion.last() + '/');
if (fileName == fixedPath)
return nullptr;
return {};
if (QFileInfo::exists(fixedPath)) {
fixedPath.replace("//", "/");
fixedPath.replace('\\', '/');
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
return std::make_unique<QFSFileEngine>(fixedPath);
#else
return new QFSFileEngine(fixedPath);
#endif
}
}
}
}
return nullptr;
return {};
}
static QrcEngineHandler* s_qrcEngineHandler = nullptr;