diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2024-04-25 11:57:28 +0200 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2024-04-26 15:01:04 +0000 |
commit | ef0cbf523afce8e8ec43cb51f45125e6997e8e71 (patch) | |
tree | 9e37a71af06288010c6618feded7c0f44802db9e | |
parent | 09ebc4151f968ef9b77c1c32882c4ac524220b7a (diff) |
TaskTree: Introduce withCancel() helper
The withCancel() method takes a function returning the sender
and its signal pair. When the signal is emitted, the queued
call to the running task tree is scheduled, and when dispatched
the *this group is canceled and the returned item finishes with an
error.
Change-Id: I898a7221d91a78225c73abc6885a18a2e7c2ae45
Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
-rw-r--r-- | src/libs/solutions/tasking/tasktree.cpp | 19 | ||||
-rw-r--r-- | src/libs/solutions/tasking/tasktree.h | 15 |
2 files changed, 34 insertions, 0 deletions
diff --git a/src/libs/solutions/tasking/tasktree.cpp b/src/libs/solutions/tasking/tasktree.cpp index 8b8d1176c0..107559d6a5 100644 --- a/src/libs/solutions/tasking/tasktree.cpp +++ b/src/libs/solutions/tasking/tasktree.cpp @@ -3,6 +3,8 @@ #include "tasktree.h" +#include "barrier.h" + #include <QDebug> #include <QEventLoop> #include <QFutureWatcher> @@ -1488,6 +1490,23 @@ ExecutableItem ExecutableItem::withLog(const QString &logName) const }; } +ExecutableItem ExecutableItem::withCancelImpl( + const std::function<void(QObject *, const std::function<void()> &)> &connectWrapper) const +{ + const auto onSetup = [connectWrapper](Barrier &barrier) { + connectWrapper(&barrier, [barrierPtr = &barrier] { barrierPtr->advance(); }); + }; + return Group { + parallel, + stopOnSuccessOrError, + Group { + finishAllAndError, + BarrierTask(onSetup) + }, + *this + }; +} + class TaskTreePrivate; class TaskNode; class RuntimeContainer; diff --git a/src/libs/solutions/tasking/tasktree.h b/src/libs/solutions/tasking/tasktree.h index 29ec554815..9b1e4b9eaf 100644 --- a/src/libs/solutions/tasking/tasktree.h +++ b/src/libs/solutions/tasking/tasktree.h @@ -290,10 +290,25 @@ public: ExecutableItem withTimeout(std::chrono::milliseconds timeout, const std::function<void()> &handler = {}) const; ExecutableItem withLog(const QString &logName) const; + template <typename SenderSignalPairGetter> + ExecutableItem withCancel(SenderSignalPairGetter &&getter) const + { + const auto connectWrapper = [getter](QObject *guard, const std::function<void()> &trigger) { + const auto senderSignalPair = getter(); + QObject::connect(senderSignalPair.first, senderSignalPair.second, guard, [trigger] { + trigger(); + }, static_cast<Qt::ConnectionType>(Qt::QueuedConnection | Qt::SingleShotConnection)); + }; + return withCancelImpl(connectWrapper); + } protected: ExecutableItem() = default; ExecutableItem(const TaskHandler &handler) : GroupItem(handler) {} + +private: + ExecutableItem withCancelImpl( + const std::function<void(QObject *, const std::function<void()> &)> &connectWrapper) const; }; class TASKING_EXPORT Group : public ExecutableItem |