From 5342805920064dfe64c23690cd1d895a95478154 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 18 Jun 2019 08:56:16 +0200 Subject: [PATCH] QCoreWlanEngine: port away from Java-style iterators (and other fixes) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The main goal of this patch was to port away from Java-style iterators, to make QtBase QT_NO_JAVA_STYLE_ITERATORS-clean. And this the patch achieves. But I couldn't resist a few drive-by fixes, too, to wit: - Use qDeleteAll() instead of while(!isEmpty()) delete takeFirst() - Use QMap::last() instead of iterating to the end, remembering the last-seen value - Use QMap::contains() instead of QMap::keys().contains() - Use qExchange() instead of copy+clear - Make some functions const (requires the mutex member to be marked as mutable, which is common for mutex members) I am almost certain that getSsidFromNetworkName() cannot work correctly. But this patch does not attempt to change the algorithm. Change-Id: Ifa04d7837bdc0837036c3a7a73f8c51f4e681f42 Reviewed-by: Tor Arne Vestbø Reviewed-by: Timur Pocheptsov --- src/plugins/bearer/corewlan/qcorewlanengine.h | 8 +-- .../bearer/corewlan/qcorewlanengine.mm | 61 +++++-------------- 2 files changed, 20 insertions(+), 49 deletions(-) diff --git a/src/plugins/bearer/corewlan/qcorewlanengine.h b/src/plugins/bearer/corewlan/qcorewlanengine.h index 4a431b886e..6dddee66a4 100644 --- a/src/plugins/bearer/corewlan/qcorewlanengine.h +++ b/src/plugins/bearer/corewlan/qcorewlanengine.h @@ -122,9 +122,9 @@ public: QString interfaceName; QMap configurationInterface; void getUserConfigurations(); - QString getNetworkNameFromSsid(const QString &ssid); - QString getSsidFromNetworkName(const QString &name); - bool isKnownSsid(const QString &ssid); + QString getNetworkNameFromSsid(const QString &ssid) const; + QString getSsidFromNetworkName(const QString &name) const; + bool isKnownSsid(const QString &ssid) const; QMap > userProfiles; signals: @@ -135,7 +135,7 @@ protected: private: QList fetchedConfigurations; - QMutex mutex; + mutable QMutex mutex; QStringList foundNetwork(const QString &id, const QString &ssid, const QNetworkConfiguration::StateFlags state, const QString &interfaceName, const QNetworkConfiguration::Purpose purpose); }; diff --git a/src/plugins/bearer/corewlan/qcorewlanengine.mm b/src/plugins/bearer/corewlan/qcorewlanengine.mm index c3dd49ff3e..4644b5af9f 100644 --- a/src/plugins/bearer/corewlan/qcorewlanengine.mm +++ b/src/plugins/bearer/corewlan/qcorewlanengine.mm @@ -203,9 +203,7 @@ void QScanThread::run() } } // add known configurations that are not around. - QMapIterator > i(userProfiles); - while (i.hasNext()) { - i.next(); + for (auto i = userProfiles.cbegin(), end = userProfiles.cend(); i != end; ++i) { QString networkName = i.key(); const QString id = QString::number(qHash(QLatin1String("corewlan:") + networkName)); @@ -215,11 +213,8 @@ void QScanThread::run() const QString ssidId = QString::number(qHash(QLatin1String("corewlan:") + networkSsid)); QNetworkConfiguration::StateFlags state = QNetworkConfiguration::Undefined; QString interfaceName; - QMapIterator ij(i.value()); - while (ij.hasNext()) { - ij.next(); - interfaceName = ij.value(); - } + if (!i.value().isEmpty()) + interfaceName = i.value().last(); if (currentInterfaceServiceActive) { if (networkSsid == QString::fromNSString([currentInterface ssid])) { @@ -269,11 +264,7 @@ QStringList QScanThread::foundNetwork(const QString &id, const QString &name, co QList QScanThread::getConfigurations() { QMutexLocker locker(&mutex); - - QList foundConfigurations = fetchedConfigurations; - fetchedConfigurations.clear(); - - return foundConfigurations; + return qExchange(fetchedConfigurations, {}); } void QScanThread::getUserConfigurations() @@ -363,17 +354,12 @@ void QScanThread::getUserConfigurations() } } -QString QScanThread::getSsidFromNetworkName(const QString &name) +QString QScanThread::getSsidFromNetworkName(const QString &name) const { QMutexLocker locker(&mutex); - QMapIterator > i(userProfiles); - while (i.hasNext()) { - i.next(); - QMap map = i.value(); - QMapIterator ij(i.value()); - while (ij.hasNext()) { - ij.next(); + for (auto i = userProfiles.cbegin(), end = userProfiles.cend(); i != end; ++i) { + for (auto ij = i.value().cbegin(), end = i.value().cend(); ij != end; ++ij) { const QString networkNameHash = QString::number(qHash(QLatin1String("corewlan:") +i.key())); if(name == i.key() || name == networkNameHash) { return ij.key(); @@ -383,36 +369,24 @@ QString QScanThread::getSsidFromNetworkName(const QString &name) return QString(); } -QString QScanThread::getNetworkNameFromSsid(const QString &ssid) +QString QScanThread::getNetworkNameFromSsid(const QString &ssid) const { QMutexLocker locker(&mutex); - QMapIterator > i(userProfiles); - while (i.hasNext()) { - i.next(); - QMap map = i.value(); - QMapIterator ij(i.value()); - while (ij.hasNext()) { - ij.next(); - if(ij.key() == ssid) { - return i.key(); - } - } + for (auto i = userProfiles.cbegin(), end = userProfiles.cend(); i != end; ++i) { + if (i.value().contains(ssid)) + return i.key(); } return QString(); } -bool QScanThread::isKnownSsid(const QString &ssid) +bool QScanThread::isKnownSsid(const QString &ssid) const { QMutexLocker locker(&mutex); - QMapIterator > i(userProfiles); - while (i.hasNext()) { - i.next(); - QMap map = i.value(); - if(map.keys().contains(ssid)) { + for (auto i = userProfiles.cbegin(), end = userProfiles.cend(); i != end; ++i) { + if (i.value().contains(ssid)) return true; - } } return false; } @@ -430,8 +404,7 @@ QCoreWlanEngine::~QCoreWlanEngine() { scanThread->wait(); - while (!foundConfigurations.isEmpty()) - delete foundConfigurations.takeFirst(); + qDeleteAll(qExchange(foundConfigurations, {})); [listener remove]; [listener release]; } @@ -486,9 +459,7 @@ void QCoreWlanEngine::connectToId(const QString &id) const QString idHash2 = QString::number(qHash(QLatin1String("corewlan:") + scanThread->getNetworkNameFromSsid(ptr->name))); QString wantedNetwork; - QMapIterator > i(scanThread->userProfiles); - while (i.hasNext()) { - i.next(); + for (auto i = scanThread->userProfiles.cbegin(), end = scanThread->userProfiles.cend(); i != end; ++i) { wantedNetwork = i.key(); const QString networkNameHash = QString::number(qHash(QLatin1String("corewlan:") + wantedNetwork)); if (id == networkNameHash) {