Remove the last uses of Java-style iterators in QtCore

They are going to be deprecated.

Add a strategic break. This was a pre-existing problem: the comment
claims that h is invalid (though I personally don't see it), but
then goes on to check the loop condition (which, in the mutable
Java iterator case, involves calling h.cend()). We now cache the
end iterator, so if there ever was a problem, it's probably a
lesser one now, but it's still not kosher, and a debug version of
QHash would find it, so break out explicitly.

Saves ~200b in text size on optimized GCC 9.1 Linux AMD64 builds,
ie. ~100b per loop.

Change-Id: I7684485b55fb23a8cf882f89621ebb75a0e607b5
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2019-05-23 09:38:56 +02:00
parent ac608b7bd2
commit 18e7e82d3f
2 changed files with 11 additions and 12 deletions

View File

@ -110,33 +110,32 @@ QStringList QPollingFileSystemWatcherEngine::removePaths(const QStringList &path
void QPollingFileSystemWatcherEngine::timeout()
{
QMutableHashIterator<QString, FileInfo> fit(files);
while (fit.hasNext()) {
QHash<QString, FileInfo>::iterator x = fit.next();
for (auto it = files.begin(), end = files.end(); it != end; /*erasing*/) {
auto x = it++;
QString path = x.key();
QFileInfo fi(path);
if (!fi.exists()) {
fit.remove();
files.erase(x);
emit fileChanged(path, true);
} else if (x.value() != fi) {
x.value() = fi;
emit fileChanged(path, false);
}
}
QMutableHashIterator<QString, FileInfo> dit(directories);
while (dit.hasNext()) {
QHash<QString, FileInfo>::iterator x = dit.next();
for (auto it = directories.begin(), end = directories.end(); it != end; /*erasing*/) {
auto x = it++;
QString path = x.key();
QFileInfo fi(path);
if (!path.endsWith(QLatin1Char('/')))
fi = QFileInfo(path + QLatin1Char('/'));
if (!fi.exists()) {
dit.remove();
directories.erase(x);
emit directoryChanged(path, true);
} else if (x.value() != fi) {
fi.refresh();
if (!fi.exists()) {
dit.remove();
directories.erase(x);
emit directoryChanged(path, true);
} else {
x.value() = fi;

View File

@ -695,9 +695,8 @@ void QWindowsFileSystemWatcherEngineThread::run()
qErrnoWarning(error, "%ls", qUtf16Printable(msgFindNextFailed(h)));
}
QMutableHashIterator<QFileSystemWatcherPathKey, QWindowsFileSystemWatcherEngine::PathInfo> it(h);
while (it.hasNext()) {
QWindowsFileSystemWatcherEngineThread::PathInfoHash::iterator x = it.next();
for (auto it = h.begin(), end = h.end(); it != end; /*erasing*/ ) {
auto x = it++;
QString absolutePath = x.value().absolutePath;
QFileInfo fileInfo(x.value().path);
DEBUG() << "checking" << x.key();
@ -723,6 +722,7 @@ void QWindowsFileSystemWatcherEngineThread::run()
handleForDir.remove(QFileSystemWatcherPathKey(absolutePath));
// h is now invalid
break;
}
} else if (x.value().isDir) {
DEBUG() << x.key() << "directory changed!";