From 74bc4617dd5a38abb60aff8b252001cb2ff9c7b8 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 6 Sep 2016 17:54:23 +0200 Subject: Eradicate Q_FOREACH loops and mark the module as Q_FOREACH-free In BearerCloud::timerEvent(), bite the bullet and use a std::vector instead of a QList to avoid repeated qAsConst() applications. In BearerCloud::configurationRemoved() and configurationChanged(), instead of iterating over QMultiMap::uniqueKeys() and QMap::remove(), iterate directly over the QMultiMap and remove the desired elements. Cache a QString. In BearerCloud::updateConfigurations(), instead of using while !isEmpty() takeFirst() on a local copy of a QList, simply iterate over the QList (now made const) using C++11 ranged for. In tst_QSvgGenerator, replace a QList of statically-known size with a C array. Change-Id: Ic0dd1c67d0819fe6167f2bce248f9b910be65803 Reviewed-by: Edward Welbourne --- examples/svg/network/bearercloud/bearercloud.cpp | 39 +++++++++++++++--------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'examples/svg/network/bearercloud/bearercloud.cpp') diff --git a/examples/svg/network/bearercloud/bearercloud.cpp b/examples/svg/network/bearercloud/bearercloud.cpp index 1ec711e..bf2a444 100644 --- a/examples/svg/network/bearercloud/bearercloud.cpp +++ b/examples/svg/network/bearercloud/bearercloud.cpp @@ -113,17 +113,19 @@ void BearerCloud::cloudMoved() void BearerCloud::timerEvent(QTimerEvent *) { - QList clouds; - foreach (QGraphicsItem *item, items()) { + std::vector clouds; + const auto graphicsItems = items(); + clouds.reserve(graphicsItems.size()); + for (QGraphicsItem *item : graphicsItems) { if (Cloud *cloud = qgraphicsitem_cast(item)) - clouds << cloud; + clouds.push_back(cloud); } - foreach (Cloud *cloud, clouds) + for (Cloud *cloud : clouds) cloud->calculateForces(); bool cloudsMoved = false; - foreach (Cloud *cloud, clouds) + for (Cloud *cloud : clouds) cloudsMoved |= cloud->advance(); if (!cloudsMoved) { @@ -158,8 +160,13 @@ void BearerCloud::configurationAdded(const QNetworkConfiguration &config) //! [3] void BearerCloud::configurationRemoved(const QNetworkConfiguration &config) { - foreach (const QNetworkConfiguration::StateFlags &state, configStates.uniqueKeys()) - configStates.remove(state, config.identifier()); + const auto id = config.identifier(); + for (auto it = configStates.begin(), end = configStates.end(); it != end; /* erasing */) { + if (it.value() == id) + it = configStates.erase(it); + else + ++it; + } Cloud *item = configurations.take(config.identifier()); @@ -173,10 +180,15 @@ void BearerCloud::configurationRemoved(const QNetworkConfiguration &config) //! [4] void BearerCloud::configurationChanged(const QNetworkConfiguration &config) { - foreach (const QNetworkConfiguration::StateFlags &state, configStates.uniqueKeys()) - configStates.remove(state, config.identifier()); + const auto id = config.identifier(); + for (auto it = configStates.begin(), end = configStates.end(); it != end; /* erasing */) { + if (it.value() == id) + it = configStates.erase(it); + else + ++it; + } - configStates.insert(config.state(), config.identifier()); + configStates.insert(config.state(), id); cloudMoved(); } @@ -185,10 +197,9 @@ void BearerCloud::configurationChanged(const QNetworkConfiguration &config) //! [1] void BearerCloud::updateConfigurations() { - QList allConfigurations = manager.allConfigurations(); - - while (!allConfigurations.isEmpty()) - configurationAdded(allConfigurations.takeFirst()); + const auto allConfigurations = manager.allConfigurations(); + for (const QNetworkConfiguration &config : allConfigurations) + configurationAdded(config); cloudMoved(); } -- cgit v1.2.1