From 6d29a3af6018d7be7601957e78b45af560c6b33e Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Fri, 11 Jan 2019 16:05:57 +0800 Subject: [PATCH 1/2] Make use of std algorithms --- src/base/bittorrent/session.cpp | 16 ++++++++-------- src/base/bittorrent/torrenthandle.cpp | 9 ++++----- src/base/http/requestparser.cpp | 12 ++++++------ src/base/http/server.cpp | 24 +++++++++--------------- src/base/rss/rss_autodownloadrule.cpp | 22 ++++++++++------------ src/base/rss/rss_folder.cpp | 11 +++++++---- src/gui/statsdialog.cpp | 10 +++++++--- src/gui/torrentcontentmodel.cpp | 10 ++++++---- src/gui/torrentcontentmodelfolder.cpp | 3 ++- src/webui/api/synccontroller.cpp | 11 ++++++++--- 10 files changed, 67 insertions(+), 61 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index f95795dff..f55c8dd5b 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -3460,18 +3460,18 @@ void Session::handleTorrentTrackerWarning(TorrentHandle *const torrent, const QS bool Session::hasPerTorrentRatioLimit() const { - for (TorrentHandle *const torrent : asConst(m_torrents)) - if (torrent->ratioLimit() >= 0) return true; - - return false; + return std::any_of(m_torrents.cbegin(), m_torrents.cend(), [](const TorrentHandle *torrent) + { + return (torrent->ratioLimit() >= 0); + }); } bool Session::hasPerTorrentSeedingTimeLimit() const { - for (TorrentHandle *const torrent : asConst(m_torrents)) - if (torrent->seedingTimeLimit() >= 0) return true; - - return false; + return std::any_of(m_torrents.cbegin(), m_torrents.cend(), [](const TorrentHandle *torrent) + { + return (torrent->seedingTimeLimit() >= 0); + }); } void Session::initResumeFolder() diff --git a/src/base/bittorrent/torrenthandle.cpp b/src/base/bittorrent/torrenthandle.cpp index ba437b5e4..f79776bf9 100644 --- a/src/base/bittorrent/torrenthandle.cpp +++ b/src/base/bittorrent/torrenthandle.cpp @@ -841,11 +841,10 @@ bool TorrentHandle::hasError() const bool TorrentHandle::hasFilteredPieces() const { const std::vector pp = m_nativeHandle.piece_priorities(); - - for (const int priority : pp) - if (priority == 0) return true; - - return false; + return std::any_of(pp.cbegin(), pp.cend(), [](const int priority) + { + return (priority == 0); + }); } int TorrentHandle::queuePosition() const diff --git a/src/base/http/requestparser.cpp b/src/base/http/requestparser.cpp index 6067cbaa0..a12ab8a95 100644 --- a/src/base/http/requestparser.cpp +++ b/src/base/http/requestparser.cpp @@ -30,6 +30,8 @@ #include "requestparser.h" +#include + #include #include #include @@ -242,12 +244,10 @@ bool RequestParser::parsePostMessage(const QByteArray &data) const QByteArray endDelimiter = QByteArray("--") + delimiter + QByteArray("--") + CRLF; multipart.push_back(viewWithoutEndingWith(multipart.takeLast(), endDelimiter)); - for (const auto &part : multipart) { - if (!parseFormData(part)) - return false; - } - - return true; + return std::all_of(multipart.cbegin(), multipart.cend(), [this](const QByteArray &part) + { + return this->parseFormData(part); + }); } qWarning() << Q_FUNC_INFO << "unknown content type:" << contentType; diff --git a/src/base/http/server.cpp b/src/base/http/server.cpp index cf83b0eb0..1a1a12ffe 100644 --- a/src/base/http/server.cpp +++ b/src/base/http/server.cpp @@ -50,22 +50,16 @@ namespace QList safeCipherList() { - const QStringList badCiphers = {"idea", "rc4"}; - const QList allCiphers = QSslSocket::supportedCiphers(); + const QStringList badCiphers {"idea", "rc4"}; + const QList allCiphers {QSslSocket::supportedCiphers()}; QList safeCiphers; - for (const QSslCipher &cipher : allCiphers) { - bool isSafe = true; - for (const QString &badCipher : badCiphers) { - if (cipher.name().contains(badCipher, Qt::CaseInsensitive)) { - isSafe = false; - break; - } - } - - if (isSafe) - safeCiphers += cipher; - } - + std::copy_if(allCiphers.cbegin(), allCiphers.cend(), std::back_inserter(safeCiphers), [&badCiphers](const QSslCipher &cipher) + { + return std::none_of(badCiphers.cbegin(), badCiphers.cend(), [&cipher](const QString &badCipher) + { + return cipher.name().contains(badCipher, Qt::CaseInsensitive); + }); + }); return safeCiphers; } } diff --git a/src/base/rss/rss_autodownloadrule.cpp b/src/base/rss/rss_autodownloadrule.cpp index 1926cf7c2..483c4f75e 100644 --- a/src/base/rss/rss_autodownloadrule.cpp +++ b/src/base/rss/rss_autodownloadrule.cpp @@ -29,6 +29,8 @@ #include "rss_autodownloadrule.h" +#include + #include #include #include @@ -237,13 +239,11 @@ bool AutoDownloadRule::matchesMustContainExpression(const QString &articleTitle) // Each expression is either a regex, or a set of wildcards separated by whitespace. // Accept if any complete expression matches. - for (const QString &expression : asConst(m_dataPtr->mustContain)) { + return std::any_of(m_dataPtr->mustContain.cbegin(), m_dataPtr->mustContain.cend(), [this, &articleTitle](const QString &expression) + { // A regex of the form "expr|" will always match, so do the same for wildcards - if (matchesExpression(articleTitle, expression)) - return true; - } - - return false; + return matchesExpression(articleTitle, expression); + }); } bool AutoDownloadRule::matchesMustNotContainExpression(const QString &articleTitle) const @@ -253,13 +253,11 @@ bool AutoDownloadRule::matchesMustNotContainExpression(const QString &articleTit // Each expression is either a regex, or a set of wildcards separated by whitespace. // Reject if any complete expression matches. - for (const QString &expression : asConst(m_dataPtr->mustNotContain)) { + return std::none_of(m_dataPtr->mustNotContain.cbegin(), m_dataPtr->mustNotContain.cend(), [this, &articleTitle](const QString &expression) + { // A regex of the form "expr|" will always match, so do the same for wildcards - if (matchesExpression(articleTitle, expression)) - return false; - } - - return true; + return matchesExpression(articleTitle, expression); + }); } bool AutoDownloadRule::matchesEpisodeFilterExpression(const QString &articleTitle) const diff --git a/src/base/rss/rss_folder.cpp b/src/base/rss/rss_folder.cpp index c7da62dcc..4616b4dde 100644 --- a/src/base/rss/rss_folder.cpp +++ b/src/base/rss/rss_folder.cpp @@ -30,6 +30,8 @@ #include "rss_folder.h" +#include + #include #include @@ -69,10 +71,11 @@ QList
Folder::articles() const int Folder::unreadCount() const { - int count = 0; - for (Item *item : asConst(items())) - count += item->unreadCount(); - return count; + const auto itemList = items(); + return std::accumulate(itemList.cbegin(), itemList.cend(), 0, [](const int acc, const Item *item) + { + return (acc + item->unreadCount()); + }); } void Folder::markAsRead() diff --git a/src/gui/statsdialog.cpp b/src/gui/statsdialog.cpp index 620e6f0b9..1e9afb74d 100644 --- a/src/gui/statsdialog.cpp +++ b/src/gui/statsdialog.cpp @@ -28,6 +28,8 @@ #include "statsdialog.h" +#include + #include "base/bittorrent/cachestatus.h" #include "base/bittorrent/session.h" #include "base/bittorrent/sessionstatus.h" @@ -89,9 +91,11 @@ void StatsDialog::update() // to complete before it receives or sends any more data on the socket. It's a metric of how disk bound you are. // num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake) - quint32 peers = 0; - for (BitTorrent::TorrentHandle *const torrent : asConst(BitTorrent::Session::instance()->torrents())) - peers += torrent->peersCount(); + const auto torrents = BitTorrent::Session::instance()->torrents(); + const quint32 peers = std::accumulate(torrents.cbegin(), torrents.cend(), 0, [](const quint32 acc, const BitTorrent::TorrentHandle *torrent) + { + return (acc + torrent->peersCount()); + }); m_ui->labelWriteStarve->setText(QString("%1%") .arg(((ss.diskWriteQueue > 0) && (peers > 0)) diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index 249af9096..200550f9d 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -28,6 +28,8 @@ #include "torrentcontentmodel.h" +#include + #include #include #include @@ -275,10 +277,10 @@ QVector TorrentContentModel::getFilePriorities() const bool TorrentContentModel::allFiltered() const { - for (const TorrentContentModelFile *fileItem : asConst(m_filesIndex)) - if (fileItem->priority() != BitTorrent::FilePriority::Ignored) - return false; - return true; + return std::all_of(m_filesIndex.cbegin(), m_filesIndex.cend(), [](const TorrentContentModelFile *fileItem) + { + return (fileItem->priority() == BitTorrent::FilePriority::Ignored); + }); } int TorrentContentModel::columnCount(const QModelIndex &parent) const diff --git a/src/gui/torrentcontentmodelfolder.cpp b/src/gui/torrentcontentmodelfolder.cpp index 8417d71e3..3851c4a93 100644 --- a/src/gui/torrentcontentmodelfolder.cpp +++ b/src/gui/torrentcontentmodelfolder.cpp @@ -86,9 +86,10 @@ TorrentContentModelItem *TorrentContentModelFolder::child(int row) const TorrentContentModelFolder *TorrentContentModelFolder::childFolderWithName(const QString &name) const { - for (TorrentContentModelItem *child : asConst(m_childItems)) + for (TorrentContentModelItem *child : asConst(m_childItems)) { if ((child->itemType() == FolderType) && (child->name() == name)) return static_cast(child); + } return nullptr; } diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index 0bc77fd14..2fe27bbae 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -28,6 +28,8 @@ #include "synccontroller.h" +#include + #include #include #include @@ -133,9 +135,12 @@ namespace map[KEY_TRANSFER_TOTAL_BUFFERS_SIZE] = cacheStatus.totalUsedBuffers * 16 * 1024; // num_peers is not reliable (adds up peers, which didn't even overcome tcp handshake) - quint32 peers = 0; - for (BitTorrent::TorrentHandle *const torrent : asConst(BitTorrent::Session::instance()->torrents())) - peers += torrent->peersCount(); + const auto torrents = BitTorrent::Session::instance()->torrents(); + const quint32 peers = std::accumulate(torrents.cbegin(), torrents.cend(), 0, [](const quint32 acc, const BitTorrent::TorrentHandle *torrent) + { + return (acc + torrent->peersCount()); + }); + map[KEY_TRANSFER_WRITE_CACHE_OVERLOAD] = ((sessionStatus.diskWriteQueue > 0) && (peers > 0)) ? Utils::String::fromDouble((100. * sessionStatus.diskWriteQueue) / peers, 2) : "0"; map[KEY_TRANSFER_READ_CACHE_OVERLOAD] = ((sessionStatus.diskReadQueue > 0) && (peers > 0)) ? Utils::String::fromDouble((100. * sessionStatus.diskReadQueue) / peers, 2) : "0"; From 364a837dbdc4555f2f3f745ee9ee921f6b480bca Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 4 Feb 2019 13:09:21 +0800 Subject: [PATCH 2/2] Revise comments about C++14 --- src/base/bittorrent/session.cpp | 2 +- src/base/indexrange.h | 2 +- src/base/tristatebool.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index f55c8dd5b..0c8ca050d 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -225,7 +225,7 @@ namespace template std::function clampValue(const T lower, const T upper) { - // TODO: change return type to `auto` when using C++14 + // TODO: change return type to `auto` when using C++17 return [lower, upper](const T value) -> T { if (value < lower) diff --git a/src/base/indexrange.h b/src/base/indexrange.h index 0bcff1ae6..a297d76ed 100644 --- a/src/base/indexrange.h +++ b/src/base/indexrange.h @@ -38,7 +38,7 @@ class IndexInterval public: using IndexType = Index; - IndexInterval(IndexType first, IndexType last) // add constexpr when using C++14 + IndexInterval(IndexType first, IndexType last) // add constexpr when using C++17 : m_first {first} , m_last {last} { diff --git a/src/base/tristatebool.h b/src/base/tristatebool.h index 269a68c1a..086bf4af4 100644 --- a/src/base/tristatebool.h +++ b/src/base/tristatebool.h @@ -48,7 +48,7 @@ public: return m_value; } - TriStateBool &operator=(const TriStateBool &other) = default; // add constexpr when using C++14 + TriStateBool &operator=(const TriStateBool &other) = default; // add constexpr when using C++17 constexpr bool operator==(const TriStateBool &other) const {