9#include "qxmpp_export.h"
21namespace QXmpp::Private {
25 std::function<void(TaskData &)> continuation;
26 std::conditional_t<std::is_void_v<T>, std::monostate, std::optional<T>> result;
27 bool finished =
false;
87 template<
typename Continuation>
89 void then(
const QObject *context, Continuation continuation)
91 using namespace QXmpp::Private;
92 if constexpr (std::is_void_v<T>) {
93 static_assert(std::is_invocable_v<Continuation>,
"Function needs to be invocable without arguments.");
95 static_assert(std::is_invocable_v<Continuation, T &&>,
"Function needs to be invocable with T &&.");
99 if constexpr (std::is_void_v<T>) {
103 auto value = std::move(*d->result);
105 continuation(std::move(value));
109 d->continuation = [context = QPointer(context),
110 continuation = std::forward<Continuation>(continuation)](TaskData<T> &d)
mutable {
112 if constexpr (std::is_void_v<T>) {
116 auto value = std::move(*d.result);
118 continuation(std::move(value));
141 template<
typename U = T, std::enable_if_t<(!std::is_
void_v<U>)> * =
nullptr>
146 return d->result.has_value();
158 template<
typename U = T, std::enable_if_t<(!std::is_
void_v<U>)> * =
nullptr>
165 return d->result.value();
177 template<
typename U = T, std::enable_if_t<(!std::is_
void_v<U>)> * =
nullptr>
184 auto value = std::move(*d->result);
186 return std::move(value);
194 QFutureInterface<T> interface;
196 if constexpr (std::is_same_v<T, void>) {
197 then(context, [interface]()
mutable {
198 interface.reportFinished();
201 then(context, [interface](T &&val)
mutable {
202 interface.reportResult(val);
203 interface.reportFinished();
207 return interface.future();
213 explicit QXmppTask(std::shared_ptr<QXmpp::Private::TaskData<T>> data)
218 std::shared_ptr<QXmpp::Private::TaskData<T>> d;
Create and update QXmppTask objects to communicate results of asynchronous operations.
Definition QXmppPromise.h:23
Definition QXmppTask.h:46
bool hasResult() const
Definition QXmppTask.h:144
const T & result() const
Definition QXmppTask.h:156
QFuture< T > toFuture(const QObject *context)
Definition QXmppTask.h:192
bool isFinished() const
Definition QXmppTask.h:132
T takeResult()
Definition QXmppTask.h:175
void then(const QObject *context, Continuation continuation)
Definition QXmppTask.h:89