From 1289bd6045818249915028fb345ec29c4ead52e5 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 1 Jul 2019 20:07:26 +0200 Subject: Eradicate Java-style iterators and mark the module free of them Java-style iterators are scheduled to be deprecated, or at the very least banned from use in Qt's own implementation. Change-Id: I6a1aeceb22dfa13c4ed7443296455b60abed7d67 Reviewed-by: Lars Knoll --- .qmake.conf | 1 + .../qmldbg_debugger/qqmlenginedebugservice.cpp | 6 +- src/quick/util/qquickpropertychanges.cpp | 80 +++++++--------------- src/quick/util/qquickstate.cpp | 8 +-- .../tst_qqmlextensionplugin.cpp | 26 ++++--- tools/qmlcachegen/resourcefilemapper.cpp | 4 +- 6 files changed, 45 insertions(+), 80 deletions(-) diff --git a/.qmake.conf b/.qmake.conf index 8860dc9141..076c5b7874 100644 --- a/.qmake.conf +++ b/.qmake.conf @@ -2,5 +2,6 @@ load(qt_build_config) CONFIG += warning_clean DEFINES += QT_NO_LINKED_LIST +DEFINES += QT_NO_JAVA_STYLE_ITERATORS MODULE_VERSION = 5.14.0 diff --git a/src/plugins/qmltooling/qmldbg_debugger/qqmlenginedebugservice.cpp b/src/plugins/qmltooling/qmldbg_debugger/qqmlenginedebugservice.cpp index 4c104f01de..d0f9833c2e 100644 --- a/src/plugins/qmltooling/qmldbg_debugger/qqmlenginedebugservice.cpp +++ b/src/plugins/qmltooling/qmldbg_debugger/qqmlenginedebugservice.cpp @@ -233,11 +233,9 @@ QVariant QQmlEngineDebugServiceImpl::valueContents(QVariant value) const if (value.type() == QVariant::Map) { QVariantMap contents; - QMapIterator i(value.toMap()); - while (i.hasNext()) { - i.next(); + const auto map = value.toMap(); + for (auto i = map.cbegin(), end = map.cend(); i != end; ++i) contents.insert(i.key(), valueContents(i.value())); - } return contents; } diff --git a/src/quick/util/qquickpropertychanges.cpp b/src/quick/util/qquickpropertychanges.cpp index 776e7ab59a..d7c0157eee 100644 --- a/src/quick/util/qquickpropertychanges.cpp +++ b/src/quick/util/qquickpropertychanges.cpp @@ -539,9 +539,7 @@ bool QQuickPropertyChanges::containsValue(const QString &name) const Q_D(const QQuickPropertyChanges); typedef QPair PropertyEntry; - QListIterator propertyIterator(d->properties); - while (propertyIterator.hasNext()) { - const PropertyEntry &entry = propertyIterator.next(); + for (const PropertyEntry &entry : d->properties) { if (entry.first == name) { return true; } @@ -555,9 +553,7 @@ bool QQuickPropertyChanges::containsExpression(const QString &name) const Q_D(const QQuickPropertyChanges); typedef QQuickPropertyChangesPrivate::ExpressionChange ExpressionEntry; - QListIterator expressionIterator(d->expressions); - while (expressionIterator.hasNext()) { - const ExpressionEntry &entry = expressionIterator.next(); + for (const ExpressionEntry &entry : d->expressions) { if (entry.name == name) { return true; } @@ -575,13 +571,10 @@ void QQuickPropertyChanges::changeValue(const QString &name, const QVariant &val { Q_D(QQuickPropertyChanges); typedef QPair PropertyEntry; - typedef QQuickPropertyChangesPrivate::ExpressionChange ExpressionEntry; - QMutableListIterator expressionIterator(d->expressions); - while (expressionIterator.hasNext()) { - const ExpressionEntry &entry = expressionIterator.next(); - if (entry.name == name) { - expressionIterator.remove(); + for (auto it = d->expressions.begin(), end = d->expressions.end(); it != end; ++it) { + if (it->name == name) { + d->expressions.erase(it); if (state() && state()->isStateActive()) { QQmlPropertyPrivate::removeBinding(d->property(name)); d->property(name).write(value); @@ -592,11 +585,9 @@ void QQuickPropertyChanges::changeValue(const QString &name, const QVariant &val } } - QMutableListIterator propertyIterator(d->properties); - while (propertyIterator.hasNext()) { - PropertyEntry &entry = propertyIterator.next(); - if (entry.first == name) { - entry.second = value; + for (auto it = d->properties.begin(), end = d->properties.end(); it != end; ++it) { + if (it->first == name) { + it->second = value; if (state() && state()->isStateActive()) d->property(name).write(value); return; @@ -611,7 +602,7 @@ void QQuickPropertyChanges::changeValue(const QString &name, const QVariant &val action.specifiedProperty = name; action.toValue = value; - propertyIterator.insert(PropertyEntry(name, value)); + d->properties.append(PropertyEntry(name, value)); if (state() && state()->isStateActive()) { state()->addEntryToRevertList(action); QQmlAbstractBinding *oldBinding = QQmlPropertyPrivate::binding(action.property); @@ -624,26 +615,21 @@ void QQuickPropertyChanges::changeValue(const QString &name, const QVariant &val void QQuickPropertyChanges::changeExpression(const QString &name, const QString &expression) { Q_D(QQuickPropertyChanges); - typedef QPair PropertyEntry; typedef QQuickPropertyChangesPrivate::ExpressionChange ExpressionEntry; bool hadValue = false; - QMutableListIterator propertyIterator(d->properties); - while (propertyIterator.hasNext()) { - PropertyEntry &entry = propertyIterator.next(); - if (entry.first == name) { - propertyIterator.remove(); + for (auto it = d->properties.begin(), end = d->properties.end(); it != end; ++it) { + if (it->first == name) { + d->properties.erase(it); hadValue = true; break; } } - QMutableListIterator expressionIterator(d->expressions); - while (expressionIterator.hasNext()) { - ExpressionEntry &entry = expressionIterator.next(); - if (entry.name == name) { - entry.expression = expression; + for (auto it = d->expressions.begin(), end = d->expressions.end(); it != end; ++it) { + if (it->name == name) { + it->expression = expression; if (state() && state()->isStateActive()) { auto prop = d->property(name); QQmlBinding *newBinding = QQmlBinding::create( @@ -657,7 +643,7 @@ void QQuickPropertyChanges::changeExpression(const QString &name, const QString } // adding a new expression. - expressionIterator.insert(ExpressionEntry(name, nullptr, QQmlBinding::Invalid, expression, QUrl(), -1, -1)); + d->expressions.append(ExpressionEntry(name, nullptr, QQmlBinding::Invalid, expression, QUrl(), -1, -1)); if (state() && state()->isStateActive()) { if (hadValue) { @@ -713,17 +699,13 @@ QVariant QQuickPropertyChanges::property(const QString &name) const typedef QPair PropertyEntry; typedef QQuickPropertyChangesPrivate::ExpressionChange ExpressionEntry; - QListIterator propertyIterator(d->properties); - while (propertyIterator.hasNext()) { - const PropertyEntry &entry = propertyIterator.next(); + for (const PropertyEntry &entry : d->properties) { if (entry.first == name) { return entry.second; } } - QListIterator expressionIterator(d->expressions); - while (expressionIterator.hasNext()) { - const ExpressionEntry &entry = expressionIterator.next(); + for (const ExpressionEntry &entry : d->expressions) { if (entry.name == name) { return QVariant(entry.expression); } @@ -735,24 +717,18 @@ QVariant QQuickPropertyChanges::property(const QString &name) const void QQuickPropertyChanges::removeProperty(const QString &name) { Q_D(QQuickPropertyChanges); - typedef QPair PropertyEntry; - typedef QQuickPropertyChangesPrivate::ExpressionChange ExpressionEntry; - QMutableListIterator expressionIterator(d->expressions); - while (expressionIterator.hasNext()) { - const ExpressionEntry &entry = expressionIterator.next(); - if (entry.name == name) { - expressionIterator.remove(); + for (auto it = d->expressions.begin(), end = d->expressions.end(); it != end; ++it) { + if (it->name == name) { + d->expressions.erase(it); state()->removeEntryFromRevertList(object(), name); return; } } - QMutableListIterator propertyIterator(d->properties); - while (propertyIterator.hasNext()) { - const PropertyEntry &entry = propertyIterator.next(); - if (entry.first == name) { - propertyIterator.remove(); + for (auto it = d->properties.begin(), end = d->properties.end(); it != end; ++it) { + if (it->first == name) { + d->properties.erase(it); state()->removeEntryFromRevertList(object(), name); return; } @@ -764,9 +740,7 @@ QVariant QQuickPropertyChanges::value(const QString &name) const Q_D(const QQuickPropertyChanges); typedef QPair PropertyEntry; - QListIterator propertyIterator(d->properties); - while (propertyIterator.hasNext()) { - const PropertyEntry &entry = propertyIterator.next(); + for (const PropertyEntry &entry : d->properties) { if (entry.first == name) { return entry.second; } @@ -780,9 +754,7 @@ QString QQuickPropertyChanges::expression(const QString &name) const Q_D(const QQuickPropertyChanges); typedef QQuickPropertyChangesPrivate::ExpressionChange ExpressionEntry; - QListIterator expressionIterator(d->expressions); - while (expressionIterator.hasNext()) { - const ExpressionEntry &entry = expressionIterator.next(); + for (const ExpressionEntry &entry : d->expressions) { if (entry.name == name) { return entry.expression; } diff --git a/src/quick/util/qquickstate.cpp b/src/quick/util/qquickstate.cpp index d130cc1f8e..c106528f45 100644 --- a/src/quick/util/qquickstate.cpp +++ b/src/quick/util/qquickstate.cpp @@ -418,10 +418,8 @@ bool QQuickState::removeEntryFromRevertList(QObject *target, const QString &name Q_D(QQuickState); if (isStateActive()) { - QMutableListIterator revertListIterator(d->revertList); - - while (revertListIterator.hasNext()) { - QQuickSimpleAction &simpleAction = revertListIterator.next(); + for (auto it = d->revertList.begin(), end = d->revertList.end(); it != end; ++it) { + QQuickSimpleAction &simpleAction = *it; if (simpleAction.property().object() == target && simpleAction.property().name() == name) { QQmlPropertyPrivate::removeBinding(simpleAction.property()); @@ -429,7 +427,7 @@ bool QQuickState::removeEntryFromRevertList(QObject *target, const QString &name if (simpleAction.binding()) QQmlPropertyPrivate::setBinding(simpleAction.binding()); - revertListIterator.remove(); + d->revertList.erase(it); return true; } } diff --git a/tests/auto/qml/qqmlextensionplugin/tst_qqmlextensionplugin.cpp b/tests/auto/qml/qqmlextensionplugin/tst_qqmlextensionplugin.cpp index 341a49bf09..16b8fe578d 100644 --- a/tests/auto/qml/qqmlextensionplugin/tst_qqmlextensionplugin.cpp +++ b/tests/auto/qml/qqmlextensionplugin/tst_qqmlextensionplugin.cpp @@ -47,18 +47,21 @@ class tst_qqmlextensionplugin : public QObject { Q_OBJECT - bool isDuplicate(QString file, const QList & files) { -#ifndef DEBUG_SUFFIX - Q_UNUSED(file) - Q_UNUSED(files) - return false; -#else + static QStringList removeDuplicates(QStringList files) { +#ifdef DEBUG_SUFFIX + const auto isDuplicate = [files] (QString file) { # ifdef QT_DEBUG - return !file.endsWith(DEBUG_SUFFIX) && files.contains(file.replace(SUFFIX, DEBUG_SUFFIX)); + return !file.endsWith(DEBUG_SUFFIX) && files.contains(file.replace(SUFFIX, DEBUG_SUFFIX)); # else - return file.endsWith(DEBUG_SUFFIX) && files.contains(file.replace(DEBUG_SUFFIX, SUFFIX)); + return file.endsWith(DEBUG_SUFFIX) && files.contains(file.replace(DEBUG_SUFFIX, SUFFIX)); # endif + }; + + files.erase(std::remove_if(files.begin(), files.end(), isDuplicate), + files.end()); + #endif + return files; } public: @@ -84,12 +87,7 @@ void tst_qqmlextensionplugin::iidCheck_data() } } - for (QMutableListIterator it(files); it.hasNext(); ) { - QString file = it.next(); - if (isDuplicate(file, files)) { - it.remove(); - } - } + files = removeDuplicates(std::move(files)); QTest::addColumn("filePath"); foreach (const QString &file, files) { diff --git a/tools/qmlcachegen/resourcefilemapper.cpp b/tools/qmlcachegen/resourcefilemapper.cpp index 6a00b39f2e..244874717f 100644 --- a/tools/qmlcachegen/resourcefilemapper.cpp +++ b/tools/qmlcachegen/resourcefilemapper.cpp @@ -50,10 +50,8 @@ bool ResourceFileMapper::isEmpty() const QStringList ResourceFileMapper::resourcePaths(const QString &fileName) { const QString absPath = QDir::cleanPath(QDir::current().absoluteFilePath(fileName)); - QHashIterator it(qrcPathToFileSystemPath); QStringList resourcePaths; - while (it.hasNext()) { - it.next(); + for (auto it = qrcPathToFileSystemPath.cbegin(), end = qrcPathToFileSystemPath.cend(); it != end; ++it) { if (QFileInfo(it.value()) == QFileInfo(absPath)) resourcePaths.append(it.key()); } -- cgit v1.2.3