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(); }
32 if (this->isRunning()) { this->cancel(); }
35 CPromiseData(
const CPromiseData &) =
delete;
36 CPromiseData &operator=(
const CPromiseData &) =
delete;
40 template <
typename T,
typename F>
44 if (!context) { context = watcher.data(); }
46 [watcher, func = std::forward<F>(func)]()
mutable {
47 if (!watcher->isCanceled()) { func(watcher->future()); }
50 watcher->setFuture(future);
58 template <
typename T,
typename F>
87 m_data->reportFinished();
91 void setResult(
const T &value) { m_data->reportFinished(&value); }
102 template <
typename U>
105 doAfter(future,
nullptr, [*
this](
auto &&f)
mutable { setResult(f); });
110 template <
typename F>
113 setResult(std::forward<F>(func)());
134 m_data->reportFinished();
141 template <
typename U>
145 future.
isCanceled() ? abandon() : setResult();
149 template <
typename U>
152 doAfter(future,
nullptr, [
this](
auto &&f) { setResult(f); });
157 template <
typename F>
160 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.
QFuture< void > future()
Return a future that can be used to detect when the task is complete.
void setResult()
Mark the task as complete.
void chainResult(QFuture< U > future)
When the given future is ready, mark this promise as complete.
A promise-based interface to QFuture, similar to std::promise for std::future.
void chainResult(QFuture< U > future)
When the given future is ready, use its result to set the result of this promise.
QFuture< T > future()
Return a future that can be used to access the result.
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 doAfter(QFuture< void > future, QObject *context, F &&func)
Connect a slot or function to be invoked in the given context when a void QFuture is finished.
void sendPostedEvents(QObject *receiver, int event_type)
bool isCanceled() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)