From 21b03676296356ece74c4902de190beccc119c7b Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 12 Oct 2024 15:15:39 +0800 Subject: [PATCH] Avoid heavy weight function object Also, by switching to template we can avoid the cost of converting to some specific type and perfectly forward the parameter to the final function. PR #21572. --- src/base/bittorrent/resumedatastorage.cpp | 2 -- src/base/bittorrent/sessionimpl.cpp | 6 ------ src/base/bittorrent/sessionimpl.h | 8 ++++++-- src/webui/api/torrentscontroller.cpp | 5 ++++- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/base/bittorrent/resumedatastorage.cpp b/src/base/bittorrent/resumedatastorage.cpp index 9cb1f6f75..f21d3c832 100644 --- a/src/base/bittorrent/resumedatastorage.cpp +++ b/src/base/bittorrent/resumedatastorage.cpp @@ -35,8 +35,6 @@ #include #include -#include "base/global.h" - const int TORRENTIDLIST_TYPEID = qRegisterMetaType>(); BitTorrent::ResumeDataStorage::ResumeDataStorage(const Path &path, QObject *parent) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index e5747774e..17a2ce074 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -71,7 +71,6 @@ #include #include #include -#include #include #include @@ -3013,11 +3012,6 @@ void SessionImpl::removeMappedPorts(const QSet &ports) }); } -void SessionImpl::invokeAsync(std::function func) -{ - m_asyncWorker->start(std::move(func)); -} - // Add a torrent to libtorrent session in hidden mode // and force it to download its metadata bool SessionImpl::downloadMetadata(const TorrentDescriptor &torrentDescr) diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index d83bbb767..9fe3c7ab6 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -44,6 +44,7 @@ #include #include #include +#include #include "base/path.h" #include "base/settingvalue.h" @@ -57,7 +58,6 @@ #include "trackerentrystatus.h" class QString; -class QThreadPool; class QTimer; class QUrl; @@ -482,7 +482,11 @@ namespace BitTorrent QMetaObject::invokeMethod(this, std::forward(func), Qt::QueuedConnection); } - void invokeAsync(std::function func); + template + void invokeAsync(Func &&func) + { + m_asyncWorker->start(std::forward(func)); + } signals: void addTorrentAlertsReceived(qsizetype count); diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index e4dfe402b..15407cf00 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -28,6 +28,7 @@ #include "torrentscontroller.h" +#include #include #include @@ -135,7 +136,9 @@ namespace const QSet SUPPORTED_WEB_SEED_SCHEMES {u"http"_s, u"https"_s, u"ftp"_s}; - void applyToTorrents(const QStringList &idList, const std::function &func) + template + void applyToTorrents(const QStringList &idList, Func func) + requires std::invocable { if ((idList.size() == 1) && (idList[0] == u"all")) {