QtNetwork: eradicate Q_FOREACH loops [rvalues]

... by replacing them with C++11 range-for loops.

This is the simplest of the patch series: Q_FOREACH took a
copy, so we do, too. Except we don't, since we're just
catching the return value that comes out of the function
(RVO). We can't feed the rvalues into range-for, because
they are non-const and would thus detach.

Change-Id: I42c9c44d948ab1512a69d42890187bc3cf2d7e58
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
This commit is contained in:
Marc Mutz 2016-01-26 14:38:54 +01:00
parent 347832d759
commit f3af7fce4a
17 changed files with 46 additions and 25 deletions

View File

@ -1148,7 +1148,8 @@ void QHttpNetworkConnectionPrivate::_q_hostLookupFinished(const QHostInfo &info)
if (networkLayerState == IPv4 || networkLayerState == IPv6 || networkLayerState == IPv4or6) if (networkLayerState == IPv4 || networkLayerState == IPv6 || networkLayerState == IPv4or6)
return; return;
foreach (const QHostAddress &address, info.addresses()) { const auto addresses = info.addresses();
for (const QHostAddress &address : addresses) {
const QAbstractSocket::NetworkLayerProtocol protocol = address.protocol(); const QAbstractSocket::NetworkLayerProtocol protocol = address.protocol();
if (protocol == QAbstractSocket::IPv4Protocol) { if (protocol == QAbstractSocket::IPv4Protocol) {
if (!foundAddress) { if (!foundAddress) {

View File

@ -121,7 +121,8 @@ void QNetworkAccessFtpBackend::open()
{ {
#ifndef QT_NO_NETWORKPROXY #ifndef QT_NO_NETWORKPROXY
QNetworkProxy proxy; QNetworkProxy proxy;
foreach (const QNetworkProxy &p, proxyList()) { const auto proxies = proxyList();
for (const QNetworkProxy &p : proxies) {
// use the first FTP proxy // use the first FTP proxy
// or no proxy at all // or no proxy at all
if (p.type() == QNetworkProxy::FtpCachingProxy if (p.type() == QNetworkProxy::FtpCachingProxy

View File

@ -189,7 +189,8 @@ QIODevice *QNetworkDiskCache::prepare(const QNetworkCacheMetaData &metaData)
return 0; return 0;
} }
foreach (const QNetworkCacheMetaData::RawHeader &header, metaData.rawHeaders()) { const auto headers = metaData.rawHeaders();
for (const auto &header : headers) {
if (header.first.toLower() == "content-length") { if (header.first.toLower() == "content-length") {
const qint64 size = header.second.toLongLong(); const qint64 size = header.second.toLongLong();
if (size > (maximumCacheSize() * 3)/4) if (size > (maximumCacheSize() * 3)/4)
@ -639,7 +640,8 @@ bool QCacheItem::canCompress() const
{ {
bool sizeOk = false; bool sizeOk = false;
bool typeOk = false; bool typeOk = false;
foreach (const QNetworkCacheMetaData::RawHeader &header, metaData.rawHeaders()) { const auto headers = metaData.rawHeaders();
for (const auto &header : headers) {
if (header.first.toLower() == "content-length") { if (header.first.toLower() == "content-length") {
qint64 size = header.second.toLongLong(); qint64 size = header.second.toLongLong();
if (size > MAX_COMPRESSION_SIZE) if (size > MAX_COMPRESSION_SIZE)

View File

@ -648,7 +648,8 @@ void QNetworkReplyHttpImplPrivate::postRequest(const QNetworkRequest &newHttpReq
QNetworkProxy transparentProxy, cacheProxy; QNetworkProxy transparentProxy, cacheProxy;
// FIXME the proxy stuff should be done in the HTTP thread // FIXME the proxy stuff should be done in the HTTP thread
foreach (const QNetworkProxy &p, managerPrivate->queryProxy(QNetworkProxyQuery(newHttpRequest.url()))) { const auto proxies = managerPrivate->queryProxy(QNetworkProxyQuery(newHttpRequest.url()));
for (const QNetworkProxy &p : proxies) {
// use the first proxy that works // use the first proxy that works
// for non-encrypted connections, any transparent or HTTP proxy // for non-encrypted connections, any transparent or HTTP proxy
// for encrypted, only transparent proxies // for encrypted, only transparent proxies

View File

@ -365,7 +365,8 @@ QNetworkReplyNSURLConnectionImpl::QNetworkReplyNSURLConnectionImpl(QObject *pare
cachePolicy:NSURLRequestUseProtocolCachePolicy cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0]; timeoutInterval:60.0];
// copy headers // copy headers
foreach (const QByteArray &header, request.rawHeaderList()) { const auto headers = request.rawHeaderList();
for (const QByteArray &header : headers) {
QByteArray headerValue = request.rawHeader(header); QByteArray headerValue = request.rawHeader(header);
[nsRequest addValue:QString::fromUtf8(headerValue).toNSString() [nsRequest addValue:QString::fromUtf8(headerValue).toNSString()
forHTTPHeaderField:QString::fromUtf8(header).toNSString()]; forHTTPHeaderField:QString::fromUtf8(header).toNSString()];

View File

@ -249,7 +249,8 @@ QNetworkSession::QNetworkSession(const QNetworkConfiguration &connectionConfig,
{ {
// invalid configuration // invalid configuration
if (!connectionConfig.identifier().isEmpty()) { if (!connectionConfig.identifier().isEmpty()) {
foreach (QBearerEngine *engine, qNetworkConfigurationManagerPrivate()->engines()) { const auto engines = qNetworkConfigurationManagerPrivate()->engines();
for (QBearerEngine *engine : engines) {
if (engine->hasIdentifier(connectionConfig.identifier())) { if (engine->hasIdentifier(connectionConfig.identifier())) {
d = engine->createSessionBackend(); d = engine->createSessionBackend();
d->q = this; d->q = this;

View File

@ -75,7 +75,8 @@ void MyObject::handleServers()
} }
// Handle the results. // Handle the results.
foreach (const QDnsServiceRecord &record, dns->serviceRecords()) { const auto records = dns->serviceRecords();
for (const QDnsServiceRecord &record : records) {
... ...
} }
dns->deleteLater(); dns->deleteLater();

View File

@ -78,7 +78,8 @@ void MyWidget::lookedUp(const QHostInfo &host)
return; return;
} }
foreach (const QHostAddress &address, host.addresses()) const auto addresses = host.addresses();
for (const QHostAddress &address : addresses)
qDebug() << "Found address:" << address.toString(); qDebug() << "Found address:" << address.toString();
} }
//! [3] //! [3]

View File

@ -49,9 +49,9 @@
****************************************************************************/ ****************************************************************************/
//! [0] //! [0]
foreach (const QSslCertificate &cert, QSslCertificate::fromPath("C:/ssl/certificate.*.pem", const auto certs = QSslCertificate::fromPath("C:/ssl/certificate.*.pem",
QSsl::Pem, QSsl::Pem, QRegExp::Wildcard);
QRegExp::Wildcard)) { for (const QSslCertificate &cert : certs) {
qDebug() << cert.issuerInfo(QSslCertificate::Organization); qDebug() << cert.issuerInfo(QSslCertificate::Organization);
} }
//! [0] //! [0]

View File

@ -200,8 +200,10 @@ static bool isBypassed(const QString &host, const QStringList &bypassList)
return true; return true;
if (isIpAddress) { if (isIpAddress) {
//exclude all local subnets //exclude all local subnets
foreach (const QNetworkInterface &iface, QNetworkInterface::allInterfaces()) { const auto ifaces = QNetworkInterface::allInterfaces();
foreach (const QNetworkAddressEntry netaddr, iface.addressEntries()) { for (const QNetworkInterface &iface : ifaces) {
const auto netaddrs = iface.addressEntries();
for (const QNetworkAddressEntry &netaddr : netaddrs) {
if (ipAddress.isInSubnet(netaddr.ip(), netaddr.prefixLength())) { if (ipAddress.isInSubnet(netaddr.ip(), netaddr.prefixLength())) {
return true; return true;
} }

View File

@ -999,10 +999,12 @@ void QAbstractSocketPrivate::_q_startConnecting(const QHostInfo &hostInfo)
if (preferredNetworkLayerProtocol == QAbstractSocket::UnknownNetworkLayerProtocol || preferredNetworkLayerProtocol == QAbstractSocket::AnyIPProtocol) { if (preferredNetworkLayerProtocol == QAbstractSocket::UnknownNetworkLayerProtocol || preferredNetworkLayerProtocol == QAbstractSocket::AnyIPProtocol) {
addresses = hostInfo.addresses(); addresses = hostInfo.addresses();
} else { } else {
foreach (const QHostAddress &address, hostInfo.addresses()) const auto candidates = hostInfo.addresses();
for (const QHostAddress &address : candidates) {
if (address.protocol() == preferredNetworkLayerProtocol) if (address.protocol() == preferredNetworkLayerProtocol)
addresses += address; addresses += address;
} }
}
#if defined(QABSTRACTSOCKET_DEBUG) #if defined(QABSTRACTSOCKET_DEBUG)

View File

@ -497,9 +497,9 @@ void QHttpSocketEngine::slotSocketConnected()
data += "Host: " + peerAddress + "\r\n"; data += "Host: " + peerAddress + "\r\n";
if (!d->proxy.hasRawHeader("User-Agent")) if (!d->proxy.hasRawHeader("User-Agent"))
data += "User-Agent: Mozilla/5.0\r\n"; data += "User-Agent: Mozilla/5.0\r\n";
foreach (const QByteArray &header, d->proxy.rawHeaderList()) { const auto headers = d->proxy.rawHeaderList();
for (const QByteArray &header : headers)
data += header + ": " + d->proxy.rawHeader(header) + "\r\n"; data += header + ": " + d->proxy.rawHeader(header) + "\r\n";
}
QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(d->authenticator); QAuthenticatorPrivate *priv = QAuthenticatorPrivate::getPrivate(d->authenticator);
//qDebug() << "slotSocketConnected: priv=" << priv << (priv ? (int)priv->method : -1); //qDebug() << "slotSocketConnected: priv=" << priv << (priv ? (int)priv->method : -1);
if (priv && priv->method != QAuthenticatorPrivate::None) { if (priv && priv->method != QAuthenticatorPrivate::None) {

View File

@ -451,7 +451,8 @@ bool QSslCertificatePrivate::parseExtension(const QByteArray &data, QSslCertific
if (!val.read(valElem.value()) || val.type() != QAsn1Element::SequenceType) if (!val.read(valElem.value()) || val.type() != QAsn1Element::SequenceType)
return false; return false;
QVariantMap result; QVariantMap result;
foreach (const QAsn1Element &el, val.toVector()) { const auto elems = val.toVector();
for (const QAsn1Element &el : elems) {
QVector<QAsn1Element> items = el.toVector(); QVector<QAsn1Element> items = el.toVector();
if (items.size() != 2) if (items.size() != 2)
return false; return false;
@ -495,7 +496,8 @@ bool QSslCertificatePrivate::parseExtension(const QByteArray &data, QSslCertific
if (!val.read(valElem.value()) || val.type() != QAsn1Element::SequenceType) if (!val.read(valElem.value()) || val.type() != QAsn1Element::SequenceType)
return false; return false;
QVariantMap result; QVariantMap result;
foreach (const QAsn1Element &el, val.toVector()) { const auto elems = val.toVector();
for (const QAsn1Element &el : elems) {
if (el.type() == 0x80) { if (el.type() == 0x80) {
result[QStringLiteral("keyid")] = el.value().toHex(); result[QStringLiteral("keyid")] = el.value().toHex();
} else if (el.type() == 0x82) { } else if (el.type() == 0x82) {

View File

@ -90,7 +90,8 @@ QSslCipher::QSslCipher()
QSslCipher::QSslCipher(const QString &name) QSslCipher::QSslCipher(const QString &name)
: d(new QSslCipherPrivate) : d(new QSslCipherPrivate)
{ {
foreach (const QSslCipher &cipher, QSslConfiguration::supportedCiphers()) { const auto ciphers = QSslConfiguration::supportedCiphers();
for (const QSslCipher &cipher : ciphers) {
if (cipher.name() == name) { if (cipher.name() == name) {
*this = cipher; *this = cipher;
return; return;
@ -111,7 +112,8 @@ QSslCipher::QSslCipher(const QString &name)
QSslCipher::QSslCipher(const QString &name, QSsl::SslProtocol protocol) QSslCipher::QSslCipher(const QString &name, QSsl::SslProtocol protocol)
: d(new QSslCipherPrivate) : d(new QSslCipherPrivate)
{ {
foreach (const QSslCipher &cipher, QSslConfiguration::supportedCiphers()) { const auto ciphers = QSslConfiguration::supportedCiphers();
for (const QSslCipher &cipher : ciphers) {
if (cipher.name() == name && cipher.protocol() == protocol) { if (cipher.name() == name && cipher.protocol() == protocol) {
*this = cipher; *this = cipher;
return; return;

View File

@ -224,7 +224,8 @@ init_context:
const QDateTime now = QDateTime::currentDateTimeUtc(); const QDateTime now = QDateTime::currentDateTimeUtc();
// Add all our CAs to this store. // Add all our CAs to this store.
foreach (const QSslCertificate &caCertificate, sslContext->sslConfiguration.caCertificates()) { const auto caCertificates = sslContext->sslConfiguration.caCertificates();
for (const QSslCertificate &caCertificate : caCertificates) {
// From https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html: // From https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html:
// //
// If several CA certificates matching the name, key identifier, and // If several CA certificates matching the name, key identifier, and

View File

@ -1249,7 +1249,8 @@ void QSslSocket::setCiphers(const QString &ciphers)
{ {
Q_D(QSslSocket); Q_D(QSslSocket);
d->configuration.ciphers.clear(); d->configuration.ciphers.clear();
foreach (const QString &cipherName, ciphers.split(QLatin1Char(':'), QString::SkipEmptyParts)) { const auto cipherNames = ciphers.split(QLatin1Char(':'), QString::SkipEmptyParts);
for (const QString &cipherName : cipherNames) {
QSslCipher cipher(cipherName); QSslCipher cipher(cipherName);
if (!cipher.isNull()) if (!cipher.isNull())
d->configuration.ciphers << cipher; d->configuration.ciphers << cipher;

View File

@ -286,7 +286,8 @@ int q_X509Callback(int ok, X509_STORE_CTX *ctx)
qCDebug(lcSsl) << "verification error: dumping bad certificate"; qCDebug(lcSsl) << "verification error: dumping bad certificate";
qCDebug(lcSsl) << QSslCertificatePrivate::QSslCertificate_from_X509(q_X509_STORE_CTX_get_current_cert(ctx)).toPem(); qCDebug(lcSsl) << QSslCertificatePrivate::QSslCertificate_from_X509(q_X509_STORE_CTX_get_current_cert(ctx)).toPem();
qCDebug(lcSsl) << "dumping chain"; qCDebug(lcSsl) << "dumping chain";
foreach (QSslCertificate cert, QSslSocketBackendPrivate::STACKOFX509_to_QSslCertificates(q_X509_STORE_CTX_get_chain(ctx))) { const auto certs = QSslSocketBackendPrivate::STACKOFX509_to_QSslCertificates(q_X509_STORE_CTX_get_chain(ctx));
for (const QSslCertificate &cert : certs) {
qCDebug(lcSsl) << "Issuer:" << "O=" << cert.issuerInfo(QSslCertificate::Organization) qCDebug(lcSsl) << "Issuer:" << "O=" << cert.issuerInfo(QSslCertificate::Organization)
<< "CN=" << cert.issuerInfo(QSslCertificate::CommonName) << "CN=" << cert.issuerInfo(QSslCertificate::CommonName)
<< "L=" << cert.issuerInfo(QSslCertificate::LocalityName) << "L=" << cert.issuerInfo(QSslCertificate::LocalityName)
@ -1625,7 +1626,8 @@ QList<QSslError> QSslSocketBackendPrivate::verify(const QList<QSslCertificate> &
} }
const QDateTime now = QDateTime::currentDateTimeUtc(); const QDateTime now = QDateTime::currentDateTimeUtc();
foreach (const QSslCertificate &caCertificate, QSslConfiguration::defaultConfiguration().caCertificates()) { const auto caCertificates = QSslConfiguration::defaultConfiguration().caCertificates();
for (const QSslCertificate &caCertificate : caCertificates) {
// From https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html: // From https://www.openssl.org/docs/ssl/SSL_CTX_load_verify_locations.html:
// //
// If several CA certificates matching the name, key identifier, and // If several CA certificates matching the name, key identifier, and