QXmpp Version: 1.10.0
QXmppPromise.h
1// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
2// SPDX-FileCopyrightText: 2022 Jonah BrĂ¼chert <jbb@kaidan.im>
3//
4// SPDX-License-Identifier: LGPL-2.1-or-later
5
6#ifndef QXMPPPROMISE_H
7#define QXMPPPROMISE_H
8
9#include "QXmppTask.h"
10
21template<typename T>
23{
24 static_assert(!std::is_abstract_v<T>);
25
26public:
27 template<typename U = T, std::enable_if_t<std::is_void_v<U>> * = nullptr>
29 : d(QXmpp::Private::TaskPrivate(nullptr))
30 {
31 }
32
33 template<typename U = T, std::enable_if_t<!std::is_void_v<U>> * = nullptr>
35 : d(QXmpp::Private::TaskPrivate([](void *r) { delete static_cast<T *>(r); }))
36 {
37 }
38
45#ifdef QXMPP_DOC
46 void reportFinished(T &&value)
47#else
48 template<typename U, typename TT = T, std::enable_if_t<!std::is_void_v<TT> && std::is_same_v<TT, U>> * = nullptr>
49 void finish(U &&value)
50#endif
51 {
52 Q_ASSERT(!d.isFinished());
53 d.setFinished(true);
54 if (d.continuation()) {
55 if (d.isContextAlive()) {
56 d.invokeContinuation(&value);
57 }
58 } else {
59 d.setResult(new U(std::move(value)));
60 }
61 }
62
64 template<typename U, typename TT = T, std::enable_if_t<!std::is_void_v<TT> && std::is_constructible_v<TT, U> && !std::is_same_v<TT, U>> * = nullptr>
65 void finish(U &&value)
66 {
67 Q_ASSERT(!d.isFinished());
68 d.setFinished(true);
69 if (d.continuation()) {
70 if (d.isContextAlive()) {
71 T convertedValue { std::move(value) };
72 d.invokeContinuation(&convertedValue);
73 }
74 } else {
75 d.setResult(new T(std::move(value)));
76 }
77 }
78
79 template<typename U = T, std::enable_if_t<std::is_void_v<U>> * = nullptr>
80 void finish()
81 {
82 Q_ASSERT(!d.isFinished());
83 d.setFinished(true);
84 if (d.continuation()) {
85 if (d.isContextAlive()) {
86 d.invokeContinuation(nullptr);
87 }
88 }
89 }
91
97 {
98 return QXmppTask<T>(d);
99 }
100
101private:
102 QXmpp::Private::TaskPrivate d;
103};
104
105#endif // QXMPPPROMISE_H
Create and update QXmppTask objects to communicate results of asynchronous operations.
Definition: QXmppPromise.h:23
void reportFinished(T &&value)
Definition: QXmppPromise.h:46
QXmppTask< T > task()
Definition: QXmppPromise.h:96
Definition: QXmppTask.h:62