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>
41 void doAfter(QFuture<T> future, QObject *context, F &&func)
43 QSharedPointer<QFutureWatcher<T>> watcher(
new QFutureWatcher<T>, &QObject::deleteLater);
44 if (!context) { context = watcher.data(); }
45 QObject::connect(watcher.data(), &QFutureWatcher<T>::finished, context,
46 [watcher, func = std::forward<F>(func)]()
mutable {
47 if (!watcher->isCanceled()) { func(watcher->future()); }
50 watcher->setFuture(future);
51 QCoreApplication::sendPostedEvents(watcher.data());
58 template <
typename T,
typename F>
59 void doAfter(QFuture<T> future, QObject *context, F &&func)
68 void doAfter(QFuture<void> future, QObject *context, F &&func)
81 QFuture<T>
future() {
return m_data->future(); }
87 m_data->reportFinished();
91 void setResult(
const T &value) { m_data->reportFinished(&value); }
97 future.waitForFinished();
98 future.isCanceled() ? abandon() : setResult(future.result());
102 template <
typename U>
105 doAfter(future,
nullptr, [*
this](
auto &&f)
mutable { setResult(f); });
110 template <
typename F>
113 setResult(std::forward<F>(func)());
117 QSharedPointer<private_ns::CPromiseData<T>> m_data {
new private_ns::CPromiseData<T> };
128 QFuture<void>
future() {
return m_data->future(); }
134 m_data->reportFinished();
141 template <
typename U>
144 future.waitForFinished();
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)();
165 QSharedPointer<private_ns::CPromiseData<void>> m_data {
new private_ns::CPromiseData<void> };
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.