From 7984327c40a09cdd79f03a52649484e5f0c5704c Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Mon, 31 Dec 2018 17:41:11 +0100 Subject: Network examples: cleanup foreach usage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace deprecated foreach macro with range-based for loop Change-Id: I0d1f2cfd557d02ccc48b41b3fea137baa2962fc1 Reviewed-by: André Hartmann Reviewed-by: Timur Pocheptsov --- examples/network/torrent/addtorrentdialog.cpp | 3 +- examples/network/torrent/filemanager.cpp | 7 +++-- examples/network/torrent/mainwindow.cpp | 6 ++-- examples/network/torrent/metainfo.cpp | 8 ++--- examples/network/torrent/ratecontroller.cpp | 4 +-- examples/network/torrent/torrentclient.cpp | 42 +++++++++++++-------------- examples/network/torrent/torrentserver.cpp | 2 +- 7 files changed, 37 insertions(+), 35 deletions(-) (limited to 'examples/network/torrent') diff --git a/examples/network/torrent/addtorrentdialog.cpp b/examples/network/torrent/addtorrentdialog.cpp index 0d197ec5fa..c87110ac4a 100644 --- a/examples/network/torrent/addtorrentdialog.cpp +++ b/examples/network/torrent/addtorrentdialog.cpp @@ -148,7 +148,8 @@ void AddTorrentDialog::setTorrent(const QString &torrentFile) ui.torrentContents->setHtml(metaInfo.singleFile().name); } else { QString html; - foreach (MetaInfoMultiFile file, metaInfo.multiFiles()) { + const QList multiFiles = metaInfo.multiFiles(); + for (const MetaInfoMultiFile &file : multiFiles) { QString name = metaInfo.name(); if (!name.isEmpty()) { html += name; diff --git a/examples/network/torrent/filemanager.cpp b/examples/network/torrent/filemanager.cpp index d05408cebc..69345442c7 100644 --- a/examples/network/torrent/filemanager.cpp +++ b/examples/network/torrent/filemanager.cpp @@ -77,7 +77,7 @@ FileManager::~FileManager() cond.wakeOne(); wait(); - foreach (QFile *file, files) { + for (QFile *file : qAsConst(files)) { file->close(); delete file; } @@ -285,7 +285,8 @@ bool FileManager::generateFiles() return false; } - foreach (const MetaInfoMultiFile &entry, metaInfo.multiFiles()) { + const QList multiFiles = metaInfo.multiFiles(); + for (const MetaInfoMultiFile &entry : multiFiles) { QString filePath = QFileInfo(prefix + entry.path).path(); if (!QFile::exists(filePath)) { if (!dir.mkpath(filePath)) { @@ -437,7 +438,7 @@ void FileManager::verifyFileContents() } // Verify all pending pieces - foreach (int index, newPendingVerificationRequests) + for (int index : qAsConst(newPendingVerificationRequests)) emit pieceVerified(index, verifySinglePiece(index)); } diff --git a/examples/network/torrent/mainwindow.cpp b/examples/network/torrent/mainwindow.cpp index 660472c05c..39b166101d 100644 --- a/examples/network/torrent/mainwindow.cpp +++ b/examples/network/torrent/mainwindow.cpp @@ -239,7 +239,7 @@ int MainWindow::rowOfClient(TorrentClient *client) const // Return the row that displays this client's status, or -1 if the // client is not known. int row = 0; - foreach (Job job, jobs) { + for (const Job &job : jobs) { if (job.client == client) return row; ++row; @@ -358,7 +358,7 @@ bool MainWindow::addTorrent(const QString &fileName, const QString &destinationF const QByteArray &resumeState) { // Check if the torrent is already being downloaded. - foreach (Job job, jobs) { + for (const Job &job : qAsConst(jobs)) { if (job.torrentFileName == fileName && job.destinationDirectory == destinationFolder) { QMessageBox::warning(this, tr("Already downloading"), tr("The torrent file %1 is " @@ -684,7 +684,7 @@ void MainWindow::closeEvent(QCloseEvent *) // them to signal that they have stopped. jobsToStop = 0; jobsStopped = 0; - foreach (Job job, jobs) { + for (const Job &job : qAsConst(jobs)) { ++jobsToStop; TorrentClient *client = job.client; client->disconnect(); diff --git a/examples/network/torrent/metainfo.cpp b/examples/network/torrent/metainfo.cpp index 565533e2f9..29b34b12a0 100644 --- a/examples/network/torrent/metainfo.cpp +++ b/examples/network/torrent/metainfo.cpp @@ -101,10 +101,10 @@ bool MetaInfo::parse(const QByteArray &data) QList files = info.value("files").toList(); for (int i = 0; i < files.size(); ++i) { - QMap file = qvariant_cast(files.at(i)); - QList pathElements = file.value("path").toList(); + const QMap file = qvariant_cast(files.at(i)); + const QList pathElements = file.value("path").toList(); QByteArray path; - foreach (QVariant p, pathElements) { + for (const QVariant &p : pathElements) { if (!path.isEmpty()) path += '/'; path += p.toByteArray(); @@ -221,7 +221,7 @@ qint64 MetaInfo::totalSize() const return singleFile().length; qint64 size = 0; - foreach (MetaInfoMultiFile file, multiFiles()) + for (const MetaInfoMultiFile &file : metaInfoMultiFiles) size += file.length; return size; } diff --git a/examples/network/torrent/ratecontroller.cpp b/examples/network/torrent/ratecontroller.cpp index cec05c7455..47b49dba30 100644 --- a/examples/network/torrent/ratecontroller.cpp +++ b/examples/network/torrent/ratecontroller.cpp @@ -78,7 +78,7 @@ void RateController::removeSocket(PeerWireClient *socket) void RateController::setDownloadLimit(int bytesPerSecond) { downLimit = bytesPerSecond; - foreach (PeerWireClient *socket, sockets) + for (PeerWireClient *socket : qAsConst(sockets)) socket->setReadBufferSize(downLimit * 4); } @@ -108,7 +108,7 @@ void RateController::transfer() } QSet pendingSockets; - foreach (PeerWireClient *client, sockets) { + for (PeerWireClient *client : qAsConst(sockets)) { if (client->canTransferMore()) pendingSockets << client; } diff --git a/examples/network/torrent/torrentclient.cpp b/examples/network/torrent/torrentclient.cpp index 95232646ab..d01a5f3d9e 100644 --- a/examples/network/torrent/torrentclient.cpp +++ b/examples/network/torrent/torrentclient.cpp @@ -383,7 +383,7 @@ qint64 TorrentClient::uploadedBytes() const int TorrentClient::connectedPeerCount() const { int tmp = 0; - foreach (PeerWireClient *client, d->connections) { + for (PeerWireClient *client : d->connections) { if (client->state() == QAbstractSocket::ConnectedState) ++tmp; } @@ -393,7 +393,7 @@ int TorrentClient::connectedPeerCount() const int TorrentClient::seedCount() const { int tmp = 0; - foreach (PeerWireClient *client, d->connections) { + for (PeerWireClient *client : d->connections) { if (client->availablePieces().count(true) == d->pieceCount) ++tmp; } @@ -464,7 +464,7 @@ void TorrentClient::stop() } // Abort all existing connections - foreach (PeerWireClient *client, d->connections) { + for (PeerWireClient *client : qAsConst(d->connections)) { RateController::instance()->removeSocket(client); ConnectionManager::instance()->removeConnection(client); client->abort(); @@ -487,7 +487,7 @@ void TorrentClient::setPaused(bool paused) // connections to 0. Keep the list of peers, so we can quickly // resume later. d->setState(Paused); - foreach (PeerWireClient *client, d->connections) + for (PeerWireClient *client : qAsConst(d->connections)) client->abort(); d->connections.clear(); TorrentServer::instance()->removeClient(this); @@ -622,7 +622,7 @@ void TorrentClient::pieceVerified(int pieceIndex, bool ok) } // Update the peer list so we know who's still interesting. - foreach (TorrentPeer *peer, d->peers) { + for (TorrentPeer *peer : qAsConst(d->peers)) { if (!peer->interesting) continue; bool interesting = false; @@ -642,7 +642,7 @@ void TorrentClient::pieceVerified(int pieceIndex, bool ok) d->incompletePieces.clearBit(pieceIndex); // Notify connected peers. - foreach (PeerWireClient *client, d->connections) { + for (PeerWireClient *client : qAsConst(d->connections)) { if (client->state() == QAbstractSocket::ConnectedState && !client->availablePieces().testBit(pieceIndex)) { client->sendPieceNotification(pieceIndex); @@ -720,9 +720,9 @@ QList TorrentClient::weighedFreePeers() const qint64 now = QDateTime::currentSecsSinceEpoch(); QList freePeers; QMap connectionsPerPeer; - foreach (TorrentPeer *peer, d->peers) { + for (TorrentPeer *peer : d->peers) { bool busy = false; - foreach (PeerWireClient *client, d->connections) { + for (PeerWireClient *client : d->connections) { if (client->state() == PeerWireClient::ConnectedState && client->peerAddress() == peer->address && client->peerPort() == peer->port) { @@ -742,7 +742,7 @@ QList TorrentClient::weighedFreePeers() const // Assign points based on connection speed and pieces available. QList > points; - foreach (TorrentPeer *peer, freePeers) { + for (TorrentPeer *peer : qAsConst(freePeers)) { int tmp = 0; if (peer->interesting) { tmp += peer->numCompletedPieces; @@ -765,7 +765,7 @@ QList TorrentClient::weighedFreePeers() const QMultiMap pointMap; int lowestScore = 0; int lastIndex = 0; - foreach (PointPair point, points) { + for (const PointPair &point : qAsConst(points)) { if (point.first > lowestScore) { lowestScore = point.first; ++lastIndex; @@ -816,7 +816,7 @@ void TorrentClient::setupOutgoingConnection() PeerWireClient *client = qobject_cast(sender()); // Update connection statistics. - foreach (TorrentPeer *peer, d->peers) { + for (TorrentPeer *peer : qAsConst(d->peers)) { if (peer->port == client->peerPort() && peer->address == client->peerAddress()) { peer->connectTime = peer->lastVisited - peer->connectStart; break; @@ -1085,7 +1085,7 @@ void TorrentClient::scheduleUploads() // no use in unchoking them. QList allClients = d->connections; QMultiMap transferSpeeds; - foreach (PeerWireClient *client, allClients) { + for (PeerWireClient *client : qAsConst(allClients)) { if (client->state() == QAbstractSocket::ConnectedState && client->availablePieces().count(true) != d->pieceCount) { if (d->state == Seeding) { @@ -1143,7 +1143,7 @@ void TorrentClient::scheduleDownloads() // Check what each client is doing, and assign payloads to those // who are either idle or done. - foreach (PeerWireClient *client, d->connections) + for (PeerWireClient *client : qAsConst(d->connections)) schedulePieceForClient(client); } @@ -1222,7 +1222,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client) incompletePiecesAvailableToClient &= client->availablePieces(); // Remove all pieces that this client has already requested. - foreach (int i, currentPieces) + for (int i : qAsConst(currentPieces)) incompletePiecesAvailableToClient.clearBit(i); // Only continue if more pieces can be scheduled. If no pieces @@ -1258,7 +1258,7 @@ void TorrentClient::schedulePieceForClient(PeerWireClient *client) memset(occurrences, 0, d->pieceCount * sizeof(int)); // Count how many of each piece are available. - foreach (PeerWireClient *peer, d->connections) { + for (PeerWireClient *peer : qAsConst(d->connections)) { QBitArray peerPieces = peer->availablePieces(); int peerPiecesSize = peerPieces.size(); for (int i = 0; i < peerPiecesSize; ++i) { @@ -1356,7 +1356,7 @@ void TorrentClient::requestMore(PeerWireClient *client) // Starting with the first piece that we're waiting for, request // blocks until the quota is filled up. - foreach (TorrentPiece *piece, piecesInProgress) { + for (TorrentPiece *piece : qAsConst(piecesInProgress)) { numBlocksInProgress += requestBlocks(client, piece, maxInProgress - numBlocksInProgress); if (numBlocksInProgress == maxInProgress) break; @@ -1450,8 +1450,8 @@ void TorrentClient::peerUnchoked() void TorrentClient::addToPeerList(const QList &peerList) { // Add peers we don't already know of to our list of peers. - QList addresses = QNetworkInterface::allAddresses(); - foreach (TorrentPeer peer, peerList) { + const QList addresses = QNetworkInterface::allAddresses(); + for (const TorrentPeer &peer : peerList) { if (addresses.contains(peer.address) && peer.port == TorrentServer::instance()->serverPort()) { // Skip our own server. @@ -1459,7 +1459,7 @@ void TorrentClient::addToPeerList(const QList &peerList) } bool known = false; - foreach (TorrentPeer *knownPeer, d->peers) { + for (const TorrentPeer *knownPeer : qAsConst(d->peers)) { if (knownPeer->port == peer.port && knownPeer->address == peer.address) { known = true; @@ -1486,8 +1486,8 @@ void TorrentClient::addToPeerList(const QList &peerList) if (d->peers.size() > maxPeers) { // Find what peers are currently connected & active QSet activePeers; - foreach (TorrentPeer *peer, d->peers) { - foreach (PeerWireClient *client, d->connections) { + for (TorrentPeer *peer : qAsConst(d->peers)) { + for (const PeerWireClient *client : qAsConst(d->connections)) { if (client->peer() == peer && (client->downloadSpeed() + client->uploadSpeed()) > 1024) activePeers << peer; } diff --git a/examples/network/torrent/torrentserver.cpp b/examples/network/torrent/torrentserver.cpp index da98529fe0..c68f33249c 100644 --- a/examples/network/torrent/torrentserver.cpp +++ b/examples/network/torrent/torrentserver.cpp @@ -102,7 +102,7 @@ void TorrentServer::removeClient() void TorrentServer::processInfoHash(const QByteArray &infoHash) { PeerWireClient *peer = qobject_cast(sender()); - foreach (TorrentClient *client, clients) { + for (TorrentClient *client : qAsConst(clients)) { if (client->state() >= TorrentClient::Searching && client->infoHash() == infoHash) { peer->disconnect(peer, 0, this, 0); client->setupIncomingConnection(peer); -- cgit v1.2.3 From 8cba096c2aa181b93887fa6f6086dee689dbf5ca Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Sat, 9 Feb 2019 18:43:05 +0100 Subject: QtBase: compile examples with QT_DISABLE_DEPRECATED_BEFORE=0x050d00 Replace deprecated functions to be able to compile examples with QT_DISABLE_DEPRECATED_BEFORE=0x050d00 Change-Id: If6b8de31f526320d6a0e2a20bb5f8e26c77f2353 Reviewed-by: Friedemann Kleint --- examples/network/torrent/mainwindow.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/network/torrent') diff --git a/examples/network/torrent/mainwindow.cpp b/examples/network/torrent/mainwindow.cpp index 39b166101d..704012ef6d 100644 --- a/examples/network/torrent/mainwindow.cpp +++ b/examples/network/torrent/mainwindow.cpp @@ -630,7 +630,7 @@ void MainWindow::about() QPushButton *quitButton = new QPushButton("OK"); QHBoxLayout *topLayout = new QHBoxLayout; - topLayout->setMargin(10); + topLayout->setContentsMargins(10, 10, 10, 10); topLayout->setSpacing(10); topLayout->addWidget(icon); topLayout->addWidget(text); -- cgit v1.2.3