6#ifndef SWIFT_MISC_PROMISE_H
7#define SWIFT_MISC_PROMISE_H
11#include <QCoreApplication>
13#include <QFutureWatcher>
15#include <QSharedPointer>
26 class CPromiseData final :
public QFutureInterface<T>
29 CPromiseData() { this->reportStarted(); }
30 ~CPromiseData()
override
32 if (this->isRunning())
38 CPromiseData(
const CPromiseData &) =
delete;
39 CPromiseData &operator=(
const CPromiseData &) =
delete;
43 template <
typename T,
typename F>
44 void doAfter(QFuture<T> future, QObject *context, F &&func)
49 context = watcher.data();
52 [watcher, func = std::forward<F>(func)]()
mutable {
53 if (!watcher->isCanceled())
55 func(watcher->future());
59 watcher->setFuture(future);
67 template <
typename T,
typename F>
70 private_ns::doAfter(future, context, std::forward<F>(func));
79 private_ns::doAfter(future, context, [func = std::forward<F>(func)](
auto &&) { func(); });
96 m_data->reportFinished();
100 void setResult(
const T &value) { m_data->reportFinished(&value); }
103 template <
typename U>
111 template <
typename U>
119 template <
typename F>
143 m_data->reportFinished();
150 template <
typename U>
158 template <
typename U>
166 template <
typename F>
169 std::forward<F>(func)();
void setResultFrom(F &&func)
Invoke a functor and mark the task as complete.
void setResult(QFuture< U > future)
Wait for the given future, then mark the task as complete.
void abandon()
Mark the task as cancelled.
void setResult()
Mark the task as complete.
void chainResult(QFuture< U > future)
When the given future is ready, mark this promise as complete.
QFuture< void > future()
Return a future that can be used to detect when the task is complete.
A promise-based interface to QFuture, similar to std::promise for std::future.
QFuture< T > future()
Return a future that can be used to access the result.
void chainResult(QFuture< U > future)
When the given future is ready, use its result to set the result of this promise.
void setResultFrom(F &&func)
Invoke a functor and use its return value to set the result.
void setResult(QFuture< U > future)
Set the result value from the given future. Will block if future is not ready.
void setResult(const T &value)
Set the result value that will be made available through the future.
void abandon()
Mark the result as cancelled.
Free functions in swift::misc.
void doAfter(QFuture< T > future, QObject *context, F &&func)
Connect a slot or function to be invoked in the given context when a QFuture is finished.
void sendPostedEvents(QObject *receiver, int event_type)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)