From 7612d5d0efce5804e39c6f8d2ef0c1e78641747b Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 2 Apr 2022 13:34:52 +0800 Subject: [PATCH 1/3] Use default operators generated/synthesized by compiler --- src/app/cmdoptions.cpp | 20 -------------------- src/base/bittorrent/infohash.cpp | 5 ----- src/base/bittorrent/infohash.h | 1 - src/base/bittorrent/tracker.cpp | 5 ----- src/base/bittorrent/tracker.h | 1 - src/base/digest32.h | 6 ------ src/base/indexrange.h | 5 ----- src/base/net/proxyconfigurationmanager.cpp | 5 ----- src/base/net/proxyconfigurationmanager.h | 1 - src/base/path.cpp | 5 ----- src/base/path.h | 1 - src/base/rss/rss_autodownloadrule.cpp | 5 ----- src/base/rss/rss_autodownloadrule.h | 2 -- src/base/utils/version.h | 11 ++--------- src/gui/properties/peerlistwidget.cpp | 7 ++----- 15 files changed, 4 insertions(+), 76 deletions(-) diff --git a/src/app/cmdoptions.cpp b/src/app/cmdoptions.cpp index 0b993c3a6..45f9cee6f 100644 --- a/src/app/cmdoptions.cpp +++ b/src/app/cmdoptions.cpp @@ -134,11 +134,6 @@ namespace } }; - bool operator==(const QString &arg, const BoolOption &option) - { - return (option == arg); - } - // Option with string value. May not have a shortcut struct StringOption : protected Option { @@ -181,11 +176,6 @@ namespace } }; - bool operator==(const QString &arg, const StringOption &option) - { - return (option == arg); - } - // Option with integer value. May not have a shortcut class IntOption : protected StringOption { @@ -233,11 +223,6 @@ namespace } }; - bool operator==(const QString &arg, const IntOption &option) - { - return (option == arg); - } - // Option that is explicitly set to true or false, and whose value is undefined when unspecified. // May not have a shortcut. class TriStateBoolOption : protected Option @@ -316,11 +301,6 @@ namespace bool m_defaultValue; }; - bool operator==(const QString &arg, const TriStateBoolOption &option) - { - return (option == arg); - } - constexpr const BoolOption SHOW_HELP_OPTION {"help", 'h'}; constexpr const BoolOption SHOW_VERSION_OPTION {"version", 'v'}; #if defined(DISABLE_GUI) && !defined(Q_OS_WIN) diff --git a/src/base/bittorrent/infohash.cpp b/src/base/bittorrent/infohash.cpp index 1a6035353..9558c4209 100644 --- a/src/base/bittorrent/infohash.cpp +++ b/src/base/bittorrent/infohash.cpp @@ -120,8 +120,3 @@ bool BitTorrent::operator==(const BitTorrent::InfoHash &left, const BitTorrent:: { return (static_cast(left) == static_cast(right)); } - -bool BitTorrent::operator!=(const BitTorrent::InfoHash &left, const BitTorrent::InfoHash &right) -{ - return !(left == right); -} diff --git a/src/base/bittorrent/infohash.h b/src/base/bittorrent/infohash.h index fa033d0ae..fbf6b8f95 100644 --- a/src/base/bittorrent/infohash.h +++ b/src/base/bittorrent/infohash.h @@ -90,7 +90,6 @@ namespace BitTorrent std::size_t qHash(const TorrentID &key, std::size_t seed = 0); bool operator==(const InfoHash &left, const InfoHash &right); - bool operator!=(const InfoHash &left, const InfoHash &right); } Q_DECLARE_METATYPE(BitTorrent::TorrentID) diff --git a/src/base/bittorrent/tracker.cpp b/src/base/bittorrent/tracker.cpp index eb6f5606b..6da1321cd 100644 --- a/src/base/bittorrent/tracker.cpp +++ b/src/base/bittorrent/tracker.cpp @@ -135,11 +135,6 @@ namespace BitTorrent return (left.uniqueID() == right.uniqueID()); } - bool operator!=(const Peer &left, const Peer &right) - { - return !(left == right); - } - std::size_t qHash(const Peer &key, const std::size_t seed) { return qHash(key.uniqueID(), seed); diff --git a/src/base/bittorrent/tracker.h b/src/base/bittorrent/tracker.h index 6a54ca3a9..892c5f3e2 100644 --- a/src/base/bittorrent/tracker.h +++ b/src/base/bittorrent/tracker.h @@ -64,7 +64,6 @@ namespace BitTorrent }; bool operator==(const Peer &left, const Peer &right); - bool operator!=(const Peer &left, const Peer &right); std::size_t qHash(const Peer &key, std::size_t seed = 0); // *Basic* Bittorrent tracker implementation diff --git a/src/base/digest32.h b/src/base/digest32.h index 11c0c314b..5fdace840 100644 --- a/src/base/digest32.h +++ b/src/base/digest32.h @@ -144,12 +144,6 @@ bool operator==(const Digest32 &left, const Digest32 &right) == static_cast::UnderlyingType>(right)); } -template -bool operator!=(const Digest32 &left, const Digest32 &right) -{ - return !(left == right); -} - template bool operator<(const Digest32 &left, const Digest32 &right) { diff --git a/src/base/indexrange.h b/src/base/indexrange.h index 65129f5ea..37931f791 100644 --- a/src/base/indexrange.h +++ b/src/base/indexrange.h @@ -107,11 +107,6 @@ public: return (*left == *right); } - friend constexpr bool operator!=(const Iterator &left, const Iterator &right) - { - return !(left == right); - } - private: IndexType m_index; }; diff --git a/src/base/net/proxyconfigurationmanager.cpp b/src/base/net/proxyconfigurationmanager.cpp index 00bc7cedd..4494280d4 100644 --- a/src/base/net/proxyconfigurationmanager.cpp +++ b/src/base/net/proxyconfigurationmanager.cpp @@ -41,11 +41,6 @@ bool Net::operator==(const ProxyConfiguration &left, const ProxyConfiguration &r && (left.hostnameLookupEnabled == right.hostnameLookupEnabled); } -bool Net::operator!=(const ProxyConfiguration &left, const ProxyConfiguration &right) -{ - return !(left == right); -} - using namespace Net; ProxyConfigurationManager *ProxyConfigurationManager::m_instance = nullptr; diff --git a/src/base/net/proxyconfigurationmanager.h b/src/base/net/proxyconfigurationmanager.h index 5444fb193..41521823d 100644 --- a/src/base/net/proxyconfigurationmanager.h +++ b/src/base/net/proxyconfigurationmanager.h @@ -57,7 +57,6 @@ namespace Net bool hostnameLookupEnabled = true; }; bool operator==(const ProxyConfiguration &left, const ProxyConfiguration &right); - bool operator!=(const ProxyConfiguration &left, const ProxyConfiguration &right); class ProxyConfigurationManager final : public QObject { diff --git a/src/base/path.cpp b/src/base/path.cpp index 75bf074ed..2ed951773 100644 --- a/src/base/path.cpp +++ b/src/base/path.cpp @@ -342,11 +342,6 @@ bool operator==(const Path &lhs, const Path &rhs) return (lhs.data().compare(rhs.data(), CASE_SENSITIVITY) == 0); } -bool operator!=(const Path &lhs, const Path &rhs) -{ - return !(lhs == rhs); -} - Path operator/(const Path &lhs, const Path &rhs) { if (rhs.isEmpty()) diff --git a/src/base/path.h b/src/base/path.h index 16bf51f71..4501e2668 100644 --- a/src/base/path.h +++ b/src/base/path.h @@ -95,7 +95,6 @@ private: Q_DECLARE_METATYPE(Path) bool operator==(const Path &lhs, const Path &rhs); -bool operator!=(const Path &lhs, const Path &rhs); Path operator+(const Path &lhs, QStringView rhs); QDataStream &operator<<(QDataStream &out, const Path &path); diff --git a/src/base/rss/rss_autodownloadrule.cpp b/src/base/rss/rss_autodownloadrule.cpp index 8caff1037..b4637cf43 100644 --- a/src/base/rss/rss_autodownloadrule.cpp +++ b/src/base/rss/rss_autodownloadrule.cpp @@ -167,11 +167,6 @@ namespace RSS return (left.m_dataPtr == right.m_dataPtr) // optimization || (*(left.m_dataPtr) == *(right.m_dataPtr)); } - - bool operator!=(const AutoDownloadRule &left, const AutoDownloadRule &right) - { - return !(left == right); - } } using namespace RSS; diff --git a/src/base/rss/rss_autodownloadrule.h b/src/base/rss/rss_autodownloadrule.h index 9e5449bc6..ff54b5faf 100644 --- a/src/base/rss/rss_autodownloadrule.h +++ b/src/base/rss/rss_autodownloadrule.h @@ -108,6 +108,4 @@ namespace RSS QSharedDataPointer m_dataPtr; }; - - bool operator!=(const AutoDownloadRule &left, const AutoDownloadRule &right); } diff --git a/src/base/utils/version.h b/src/base/utils/version.h index 98bff202f..ba2618a7b 100644 --- a/src/base/utils/version.h +++ b/src/base/utils/version.h @@ -124,13 +124,12 @@ namespace Utils return res; } - // TODO: remove manually defined operators and use compiler generated `operator<=>()` in C++20 - friend bool operator==(const ThisType &left, const ThisType &right) + friend constexpr bool operator==(const ThisType &left, const ThisType &right) { return (left.m_components == right.m_components); } - friend bool operator<(const ThisType &left, const ThisType &right) + friend constexpr bool operator<(const ThisType &left, const ThisType &right) { return (left.m_components < right.m_components); } @@ -159,12 +158,6 @@ namespace Utils std::array m_components {{}}; }; - template - constexpr bool operator!=(const Version &left, const Version &right) - { - return !(left == right); - } - template constexpr bool operator>(const Version &left, const Version &right) { diff --git a/src/gui/properties/peerlistwidget.cpp b/src/gui/properties/peerlistwidget.cpp index afae06d64..9f6b37b08 100644 --- a/src/gui/properties/peerlistwidget.cpp +++ b/src/gui/properties/peerlistwidget.cpp @@ -67,12 +67,9 @@ struct PeerEndpoint { BitTorrent::PeerAddress address; QString connectionType; // matches return type of `PeerInfo::connectionType()` -}; -bool operator==(const PeerEndpoint &left, const PeerEndpoint &right) -{ - return (left.address == right.address) && (left.connectionType == right.connectionType); -} + friend bool operator==(const PeerEndpoint &left, const PeerEndpoint &right) = default; +}; std::size_t qHash(const PeerEndpoint &peerEndpoint, const std::size_t seed = 0) { From c58aa58cba0ba749814282547f385e1674e0a9c8 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 2 Apr 2022 15:24:26 +0800 Subject: [PATCH 2/3] Use C++20 designated initializers --- src/base/bittorrent/sessionimpl.cpp | 69 +++++++++++++++++------------ 1 file changed, 40 insertions(+), 29 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 4500b11f9..1c5930260 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -1571,37 +1571,48 @@ void SessionImpl::initMetrics() return index; }; - // TODO: switch to "designated initializers" in C++20 - m_metricIndices.net.hasIncomingConnections = findMetricIndex("net.has_incoming_connections"); - m_metricIndices.net.sentPayloadBytes = findMetricIndex("net.sent_payload_bytes"); - m_metricIndices.net.recvPayloadBytes = findMetricIndex("net.recv_payload_bytes"); - m_metricIndices.net.sentBytes = findMetricIndex("net.sent_bytes"); - m_metricIndices.net.recvBytes = findMetricIndex("net.recv_bytes"); - m_metricIndices.net.sentIPOverheadBytes = findMetricIndex("net.sent_ip_overhead_bytes"); - m_metricIndices.net.recvIPOverheadBytes = findMetricIndex("net.recv_ip_overhead_bytes"); - m_metricIndices.net.sentTrackerBytes = findMetricIndex("net.sent_tracker_bytes"); - m_metricIndices.net.recvTrackerBytes = findMetricIndex("net.recv_tracker_bytes"); - m_metricIndices.net.recvRedundantBytes = findMetricIndex("net.recv_redundant_bytes"); - m_metricIndices.net.recvFailedBytes = findMetricIndex("net.recv_failed_bytes"); - - m_metricIndices.peer.numPeersConnected = findMetricIndex("peer.num_peers_connected"); - m_metricIndices.peer.numPeersDownDisk = findMetricIndex("peer.num_peers_down_disk"); - m_metricIndices.peer.numPeersUpDisk = findMetricIndex("peer.num_peers_up_disk"); - - m_metricIndices.dht.dhtBytesIn = findMetricIndex("dht.dht_bytes_in"); - m_metricIndices.dht.dhtBytesOut = findMetricIndex("dht.dht_bytes_out"); - m_metricIndices.dht.dhtNodes = findMetricIndex("dht.dht_nodes"); - - m_metricIndices.disk.diskBlocksInUse = findMetricIndex("disk.disk_blocks_in_use"); - m_metricIndices.disk.numBlocksRead = findMetricIndex("disk.num_blocks_read"); + m_metricIndices = + { + .net = + { + .hasIncomingConnections = findMetricIndex("net.has_incoming_connections"), + .sentPayloadBytes = findMetricIndex("net.sent_payload_bytes"), + .recvPayloadBytes = findMetricIndex("net.recv_payload_bytes"), + .sentBytes = findMetricIndex("net.sent_bytes"), + .recvBytes = findMetricIndex("net.recv_bytes"), + .sentIPOverheadBytes = findMetricIndex("net.sent_ip_overhead_bytes"), + .recvIPOverheadBytes = findMetricIndex("net.recv_ip_overhead_bytes"), + .sentTrackerBytes = findMetricIndex("net.sent_tracker_bytes"), + .recvTrackerBytes = findMetricIndex("net.recv_tracker_bytes"), + .recvRedundantBytes = findMetricIndex("net.recv_redundant_bytes"), + .recvFailedBytes = findMetricIndex("net.recv_failed_bytes") + }, + .peer = + { + .numPeersConnected = findMetricIndex("peer.num_peers_connected"), + .numPeersUpDisk = findMetricIndex("peer.num_peers_up_disk"), + .numPeersDownDisk = findMetricIndex("peer.num_peers_down_disk") + }, + .dht = + { + .dhtBytesIn = findMetricIndex("dht.dht_bytes_in"), + .dhtBytesOut = findMetricIndex("dht.dht_bytes_out"), + .dhtNodes = findMetricIndex("dht.dht_nodes") + }, + .disk = + { + .diskBlocksInUse = findMetricIndex("disk.disk_blocks_in_use"), + .numBlocksRead = findMetricIndex("disk.num_blocks_read"), #ifndef QBT_USES_LIBTORRENT2 - m_metricIndices.disk.numBlocksCacheHits = findMetricIndex("disk.num_blocks_cache_hits"); + .numBlocksCacheHits = findMetricIndex("disk.num_blocks_cache_hits"), #endif - m_metricIndices.disk.writeJobs = findMetricIndex("disk.num_write_ops"); - m_metricIndices.disk.readJobs = findMetricIndex("disk.num_read_ops"); - m_metricIndices.disk.hashJobs = findMetricIndex("disk.num_blocks_hashed"); - m_metricIndices.disk.queuedDiskJobs = findMetricIndex("disk.queued_disk_jobs"); - m_metricIndices.disk.diskJobTime = findMetricIndex("disk.disk_job_time"); + .writeJobs = findMetricIndex("disk.num_write_ops"), + .readJobs = findMetricIndex("disk.num_read_ops"), + .hashJobs = findMetricIndex("disk.num_blocks_hashed"), + .queuedDiskJobs = findMetricIndex("disk.queued_disk_jobs"), + .diskJobTime = findMetricIndex("disk.disk_job_time") + } + }; } lt::settings_pack SessionImpl::loadLTSettings() const From d6adebe4c0bbc51721bdef0a92098628da9f26b4 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 22 Jul 2023 02:07:06 +0800 Subject: [PATCH 3/3] Switch to efficient algorithm --- src/base/bittorrent/torrentimpl.cpp | 7 ++----- src/base/net/downloadmanager.cpp | 9 ++++----- src/base/preferences.cpp | 2 +- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 8ecd1e98a..6fb3e72bd 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -31,7 +31,6 @@ #include #include -#include #include #include @@ -551,8 +550,7 @@ QVector TorrentImpl::trackers() const void TorrentImpl::addTrackers(QVector trackers) { - // TODO: use std::erase_if() in C++20 - trackers.erase(std::remove_if(trackers.begin(), trackers.end(), [](const TrackerEntry &entry) { return entry.url.isEmpty(); }), trackers.end()); + trackers.removeIf([](const TrackerEntry &entry) { return entry.url.isEmpty(); }); const auto newTrackers = QSet(trackers.cbegin(), trackers.cend()) - QSet(m_trackerEntries.cbegin(), m_trackerEntries.cend()); @@ -596,8 +594,7 @@ void TorrentImpl::removeTrackers(const QStringList &trackers) void TorrentImpl::replaceTrackers(QVector trackers) { - // TODO: use std::erase_if() in C++20 - trackers.erase(std::remove_if(trackers.begin(), trackers.end(), [](const TrackerEntry &entry) { return entry.url.isEmpty(); }), trackers.end()); + trackers.removeIf([](const TrackerEntry &entry) { return entry.url.isEmpty(); }); std::sort(trackers.begin(), trackers.end() , [](const TrackerEntry &lhs, const TrackerEntry &rhs) { return lhs.tier < rhs.tier; }); diff --git a/src/base/net/downloadmanager.cpp b/src/base/net/downloadmanager.cpp index 55192ec60..6a955f26b 100644 --- a/src/base/net/downloadmanager.cpp +++ b/src/base/net/downloadmanager.cpp @@ -42,7 +42,6 @@ #include #include -#include "base/algorithm.h" #include "base/global.h" #include "base/logger.h" #include "base/preferences.h" @@ -63,7 +62,7 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = Preferences::instance()->getNetworkCookies(); - Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) + cookies.removeIf([&now](const QNetworkCookie &cookie) { return cookie.isSessionCookie() || (cookie.expirationDate() <= now); }); @@ -75,7 +74,7 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = allCookies(); - Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) + cookies.removeIf([&now](const QNetworkCookie &cookie) { return cookie.isSessionCookie() || (cookie.expirationDate() <= now); }); @@ -90,7 +89,7 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = QNetworkCookieJar::cookiesForUrl(url); - Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) + cookies.removeIf([&now](const QNetworkCookie &cookie) { return !cookie.isSessionCookie() && (cookie.expirationDate() <= now); }); @@ -102,7 +101,7 @@ public: { const QDateTime now = QDateTime::currentDateTime(); QList cookies = cookieList; - Algorithm::removeIf(cookies, [&now](const QNetworkCookie &cookie) + cookies.removeIf([&now](const QNetworkCookie &cookie) { return !cookie.isSessionCookie() && (cookie.expirationDate() <= now); }); diff --git a/src/base/preferences.cpp b/src/base/preferences.cpp index fe5addf6e..809826faa 100644 --- a/src/base/preferences.cpp +++ b/src/base/preferences.cpp @@ -702,7 +702,7 @@ QVector Preferences::getWebUiAuthSubnetWhitelist() const void Preferences::setWebUiAuthSubnetWhitelist(QStringList subnets) { - Algorithm::removeIf(subnets, [](const QString &subnet) + subnets.removeIf([](const QString &subnet) { return !Utils::Net::parseSubnet(subnet.trimmed()).has_value(); });