QXmpp: QXmppPromise.h Source File

QXmpp Version: 1.14.4

Loading...
Searching...
No Matches
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>
22 class QXmppPromise
23{
24 static_assert(!std::is_abstract_v<T>);
25
26public:
27 QXmppPromise() : d(std::make_shared<QXmpp::Private::TaskData<T>>()) { }
28
35#ifdef QXMPP_DOC
36 void reportFinished(T &&value)
37#else
38 template<typename U, typename TT = T>
39 requires(!std::is_void_v<T> && std::is_same_v<TT, U>)
40 void finish(U &&value)
41#endif
42 {
43 Q_ASSERT(!d->finished);
44 d->finished = true;
45 d->result = std::move(value);
46 if (d->continuation) {
47 d->continuation(*d);
48 // clear continuation to avoid "deadlocks" in case the user captured this QXmppTask
49 d->continuation = {};
50 }
51 }
52
54 template<typename U, typename TT = T>
55 requires(!std::is_void_v<T> && std::is_constructible_v<TT, U> && !std::is_same_v<TT, U>)
56 void finish(U &&value)
57 {
58 Q_ASSERT(!d->finished);
59 d->finished = true;
60 d->result = T { std::move(value) };
61 if (d->continuation) {
62 d->continuation(*d);
63 // clear continuation to avoid "deadlocks" in case the user captured this QXmppTask
64 d->continuation = {};
65 }
66 }
67
68 template<typename U = T>
69 requires(std::is_void_v<T>)
70 void finish()
71 {
72 Q_ASSERT(!d->finished);
73 d->finished = true;
74 if (d->continuation) {
75 d->continuation(*d);
76 // clear continuation to avoid "deadlocks" in case the user captured this QXmppTask
77 d->continuation = {};
78 }
79 }
81
86 QXmppTask<T> task() { return QXmppTask<T> { d }; }
87
88private:
89 std::shared_ptr<QXmpp::Private::TaskData<T>> d;
90};
91
92#endif // QXMPPPROMISE_H
QXmppPromise::reportFinished
void reportFinished(T &&value)
Definition QXmppPromise.h:36
QXmppPromise::task
QXmppTask< T > task()
Definition QXmppPromise.h:86
QXmppTask
Definition QXmppTask.h:46