Standardize on unique_ptr to hold QAbstractFileEngine

This will make it possible to return it from functions in an owner,
as exemplified in the QFileInfoPrivate ctor, unlike QScopedPointer,
which lacks move special member functions.

Change-Id: I179ffa4f656e1b83c23e0f67d1542834460ff382
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2019-05-25 23:42:45 +02:00
parent 80d7ba4c49
commit 186bdec01a
5 changed files with 28 additions and 22 deletions

View File

@ -153,7 +153,7 @@ QDirPrivate::QDirPrivate(const QDirPrivate &copy)
bool QDirPrivate::exists() const
{
if (fileEngine.isNull()) {
if (!fileEngine) {
QFileSystemEngine::fillMetaData(dirEntry, metaData,
QFileSystemMetaData::ExistsAttribute | QFileSystemMetaData::DirectoryType); // always stat
return metaData.exists() && metaData.isDirectory();
@ -226,7 +226,7 @@ inline void QDirPrivate::resolveAbsoluteEntry() const
return;
QString absoluteName;
if (fileEngine.isNull()) {
if (!fileEngine) {
if (!dirEntry.isRelative() && dirEntry.isClean()) {
absoluteDirEntry = dirEntry;
return;
@ -693,7 +693,7 @@ QString QDir::absolutePath() const
QString QDir::canonicalPath() const
{
const QDirPrivate* d = d_ptr.constData();
if (d->fileEngine.isNull()) {
if (!d->fileEngine) {
QFileSystemEntry answer = QFileSystemEngine::canonicalName(d->dirEntry, d->metaData);
return answer.filePath();
}
@ -1502,7 +1502,7 @@ bool QDir::mkdir(const QString &dirName) const
}
QString fn = filePath(dirName);
if (d->fileEngine.isNull())
if (!d->fileEngine)
return QFileSystemEngine::createDirectory(QFileSystemEntry(fn), false);
return d->fileEngine->mkdir(fn, false);
}
@ -1526,7 +1526,7 @@ bool QDir::rmdir(const QString &dirName) const
}
QString fn = filePath(dirName);
if (d->fileEngine.isNull())
if (!d->fileEngine)
return QFileSystemEngine::removeDirectory(QFileSystemEntry(fn), false);
return d->fileEngine->rmdir(fn, false);
@ -1554,7 +1554,7 @@ bool QDir::mkpath(const QString &dirPath) const
}
QString fn = filePath(dirPath);
if (d->fileEngine.isNull())
if (!d->fileEngine)
return QFileSystemEngine::createDirectory(QFileSystemEntry(fn), true);
return d->fileEngine->mkdir(fn, true);
}
@ -1580,7 +1580,7 @@ bool QDir::rmpath(const QString &dirPath) const
}
QString fn = filePath(dirPath);
if (d->fileEngine.isNull())
if (!d->fileEngine)
return QFileSystemEngine::removeDirectory(QFileSystemEntry(fn), true);
return d->fileEngine->rmdir(fn, true);
}
@ -1653,7 +1653,7 @@ bool QDir::isReadable() const
{
const QDirPrivate* d = d_ptr.constData();
if (d->fileEngine.isNull()) {
if (!d->fileEngine) {
if (!d->metaData.hasFlags(QFileSystemMetaData::UserReadPermission))
QFileSystemEngine::fillMetaData(d->dirEntry, d->metaData, QFileSystemMetaData::UserReadPermission);
@ -1698,7 +1698,7 @@ bool QDir::exists() const
*/
bool QDir::isRoot() const
{
if (d_ptr->fileEngine.isNull())
if (!d_ptr->fileEngine)
return d_ptr->dirEntry.isRoot();
return d_ptr->fileEngine->fileFlags(QAbstractFileEngine::FlagsMask) & QAbstractFileEngine::RootFlag;
}
@ -1730,7 +1730,7 @@ bool QDir::isRoot() const
*/
bool QDir::isRelative() const
{
if (d_ptr->fileEngine.isNull())
if (!d_ptr->fileEngine)
return d_ptr->dirEntry.isRelative();
return d_ptr->fileEngine->isRelativePath();
}
@ -1747,7 +1747,7 @@ bool QDir::makeAbsolute()
{
const QDirPrivate *d = d_ptr.constData();
QScopedPointer<QDirPrivate> dir;
if (!d->fileEngine.isNull()) {
if (!!d->fileEngine) {
QString absolutePath = d->fileEngine->fileName(QAbstractFileEngine::AbsoluteName);
if (QDir::isRelativePath(absolutePath))
return false;
@ -1780,8 +1780,8 @@ bool QDir::operator==(const QDir &dir) const
if (d == other)
return true;
Qt::CaseSensitivity sensitive;
if (d->fileEngine.isNull() || other->fileEngine.isNull()) {
if (d->fileEngine.data() != other->fileEngine.data()) // one is native, the other is a custom file-engine
if (!d->fileEngine || !other->fileEngine) {
if (d->fileEngine.get() != other->fileEngine.get()) // one is native, the other is a custom file-engine
return false;
sensitive = QFileSystemEngine::isCaseSensitive() ? Qt::CaseSensitive : Qt::CaseInsensitive;

View File

@ -54,6 +54,8 @@
#include "qfilesystementry_p.h"
#include "qfilesystemmetadata_p.h"
#include <memory>
QT_BEGIN_NAMESPACE
class QDirPrivate : public QSharedData
@ -98,7 +100,7 @@ public:
QDir::SortFlags sort;
QDir::Filters filters;
QScopedPointer<QAbstractFileEngine> fileEngine;
std::unique_ptr<QAbstractFileEngine> fileEngine;
QFileSystemEntry dirEntry;
mutable QFileSystemEntry absoluteDirEntry;

View File

@ -107,6 +107,8 @@
#include <QtCore/private/qfilesystemengine_p.h>
#include <QtCore/private/qfileinfo_p.h>
#include <memory>
QT_BEGIN_NAMESPACE
template <class Iterator>
@ -132,7 +134,7 @@ public:
void checkAndPushDirectory(const QFileInfo &);
bool matchesFilters(const QString &fileName, const QFileInfo &fi) const;
QScopedPointer<QAbstractFileEngine> engine;
std::unique_ptr<QAbstractFileEngine> engine;
QFileSystemEntry dirEntry;
const QStringList nameFilters;
@ -435,7 +437,7 @@ bool QDirIteratorPrivate::matchesFilters(const QString &fileName, const QFileInf
QDirIterator::QDirIterator(const QDir &dir, IteratorFlags flags)
{
const QDirPrivate *other = dir.d_ptr.constData();
d.reset(new QDirIteratorPrivate(other->dirEntry, other->nameFilters, other->filters, flags, !other->fileEngine.isNull()));
d.reset(new QDirIteratorPrivate(other->dirEntry, other->nameFilters, other->filters, flags, bool(other->fileEngine)));
}
/*!

View File

@ -707,11 +707,11 @@ bool QFileInfo::exists(const QString &file)
return false;
QFileSystemEntry entry(file);
QFileSystemMetaData data;
QAbstractFileEngine *engine =
QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry, data);
std::unique_ptr<QAbstractFileEngine> engine
{QFileSystemEngine::resolveEntryAndCreateLegacyEngine(entry, data)};
// Expensive fallback to non-QFileSystemEngine implementation
if (engine)
return QFileInfo(new QFileInfoPrivate(entry, data, engine)).exists();
return QFileInfo(new QFileInfoPrivate(entry, data, std::move(engine))).exists();
QFileSystemEngine::fillMetaData(entry, data, QFileSystemMetaData::ExistsAttribute);
return data.exists();

View File

@ -61,6 +61,8 @@
#include <QtCore/private/qfilesystementry_p.h>
#include <QtCore/private/qfilesystemmetadata_p.h>
#include <memory>
QT_BEGIN_NAMESPACE
class QFileInfoPrivate : public QSharedData
@ -126,10 +128,10 @@ public:
metaData = QFileSystemMetaData();
}
inline QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data, QAbstractFileEngine *engine)
inline QFileInfoPrivate(const QFileSystemEntry &file, const QFileSystemMetaData &data, std::unique_ptr<QAbstractFileEngine> engine)
: fileEntry(file),
metaData(data),
fileEngine(engine),
fileEngine{std::move(engine)},
cachedFlags(0),
#ifndef QT_NO_FSFILEENGINE
isDefaultConstructed(false),
@ -163,7 +165,7 @@ public:
QFileSystemEntry fileEntry;
mutable QFileSystemMetaData metaData;
QScopedPointer<QAbstractFileEngine> const fileEngine;
std::unique_ptr<QAbstractFileEngine> const fileEngine;
mutable QString fileNames[QAbstractFileEngine::NFileNames];
mutable QString fileOwners[2]; // QAbstractFileEngine::FileOwner: OwnerUser and OwnerGroup