QFseventsFileSystemWatcherEngine: port some Java-style iterators to ranged-for

Amends b03385f9cf.

Java-style iterators are scheduled to be deprecated.

The general pattern used in the patch is that instead of copying an
input list, then iterating over the copy with some calls to
it.remove() (which leads to quadratic-complexity loops), we simply
copy conditionally (a la remove_copy_if instead of remove_if).

To make clearer what's going on, rename the outgoing list to
'unhandled'.

To avoid having to touch too much of the loops' structure, which
sometimes is quite convoluted, use qScopeGuard to do the append to
'unhandled', unless the original code removed the element.

Change-Id: I808a939b9c816b329ee87620e0a3461fee6e3e40
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
bb10
Marc Mutz 2019-05-19 23:25:12 +02:00
parent 33fdd5c833
commit f99bbfb9ea
1 changed files with 15 additions and 15 deletions

View File

@ -50,6 +50,7 @@
#include <qfile.h>
#include <qfileinfo.h>
#include <qvarlengtharray.h>
#include <qscopeguard.h>
#undef FSEVENT_DEBUG
#ifdef FSEVENT_DEBUG
@ -338,10 +339,10 @@ QStringList QFseventsFileSystemWatcherEngine::addPaths(const QStringList &paths,
bool needsRestart = false;
WatchingState oldState = watchingState;
QStringList p = paths;
QMutableListIterator<QString> it(p);
while (it.hasNext()) {
QString origPath = it.next().normalized(QString::NormalizationForm_C);
QStringList unhandled;
for (const QString &path : paths) {
auto sg = qScopeGuard([&]{ unhandled.push_back(path); });
QString origPath = path.normalized(QString::NormalizationForm_C);
QString realPath = origPath;
if (realPath.endsWith(QDir::separator()))
realPath = realPath.mid(0, realPath.size() - 1);
@ -362,17 +363,17 @@ QStringList QFseventsFileSystemWatcherEngine::addPaths(const QStringList &paths,
continue;
directories->append(origPath);
watchedPath = realPath;
it.remove();
} else {
if (files->contains(origPath))
continue;
files->append(origPath);
it.remove();
watchedPath = fi.path();
parentPath = watchedPath;
}
sg.dismiss();
for (PathRefCounts::const_iterator i = watchingState.watchedPaths.begin(),
ei = watchingState.watchedPaths.end(); i != ei; ++i) {
if (watchedPath.startsWith(i.key() % QDir::separator())) {
@ -409,14 +410,14 @@ QStringList QFseventsFileSystemWatcherEngine::addPaths(const QStringList &paths,
// ok, something went wrong, let's try to restore the previous state
watchingState = std::move(oldState);
// and because we don't know which path caused the issue (if any), fail on all of them
p = paths;
unhandled = paths;
if (wasRunning)
startStream();
}
}
return p;
return unhandled;
}
QStringList QFseventsFileSystemWatcherEngine::removePaths(const QStringList &paths,
@ -430,10 +431,9 @@ QStringList QFseventsFileSystemWatcherEngine::removePaths(const QStringList &pat
bool needsRestart = false;
WatchingState oldState = watchingState;
QStringList p = paths;
QMutableListIterator<QString> it(p);
while (it.hasNext()) {
QString origPath = it.next();
QStringList unhandled;
for (const QString &origPath : paths) {
auto sg = qScopeGuard([&]{ unhandled.push_back(origPath); });
QString realPath = origPath;
if (realPath.endsWith(QDir::separator()))
realPath = realPath.mid(0, realPath.size() - 1);
@ -447,7 +447,7 @@ QStringList QFseventsFileSystemWatcherEngine::removePaths(const QStringList &pat
needsRestart |= derefPath(dirIt->dirInfo.watchedPath);
watchingState.watchedDirectories.erase(dirIt);
directories->removeAll(origPath);
it.remove();
sg.dismiss();
DEBUG("Removed directory '%s'", qPrintable(realPath));
}
} else {
@ -463,7 +463,7 @@ QStringList QFseventsFileSystemWatcherEngine::removePaths(const QStringList &pat
if (filesInDir.isEmpty())
watchingState.watchedFiles.erase(pIt);
files->removeAll(origPath);
it.remove();
sg.dismiss();
DEBUG("Removed file '%s'", qPrintable(realPath));
}
}
@ -479,7 +479,7 @@ QStringList QFseventsFileSystemWatcherEngine::removePaths(const QStringList &pat
}
}
return p;
return unhandled;
}
// Returns false if FSEventStream* calls failed for some mysterious reason, true if things got a