Loading...
Searching...
No Matches
QXmppPromise.h
// SPDX-FileCopyrightText: 2022 Linus Jahn <lnj@kaidan.im>
// SPDX-FileCopyrightText: 2022 Jonah Brüchert <jbb@kaidan.im>
//
// SPDX-License-Identifier: LGPL-2.1-or-later
#ifndef QXMPPPROMISE_H
#define QXMPPPROMISE_H
#include "QXmppTask.h"
template<typename T>
class QXmppPromise
{
static_assert(!std::is_abstract_v<T>);
public:
template<typename U = T, std::enable_if_t<std::is_void_v<U>> * = nullptr>
QXmppPromise()
: d(QXmpp::Private::TaskPrivate(nullptr))
{
}
template<typename U = T, std::enable_if_t<!std::is_void_v<U>> * = nullptr>
QXmppPromise()
: d(QXmpp::Private::TaskPrivate([](void *r) { delete static_cast<T *>(r); }))
{
}
#ifdef QXMPP_DOC
void reportFinished(T &&value)
#else
template<typename U, typename TT = T, std::enable_if_t<!std::is_void_v<TT> && std::is_same_v<TT, U>> * = nullptr>
void finish(U &&value)
#endif
{
Q_ASSERT(!d.isFinished());
d.setFinished(true);
if (d.continuation()) {
if (d.isContextAlive()) {
d.invokeContinuation(&value);
}
} else {
d.setResult(new U(std::move(value)));
}
}
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>
void finish(U &&value)
{
Q_ASSERT(!d.isFinished());
d.setFinished(true);
if (d.continuation()) {
if (d.isContextAlive()) {
T convertedValue { std::move(value) };
d.invokeContinuation(&convertedValue);
}
} else {
d.setResult(new T(std::move(value)));
}
}
template<typename U = T, std::enable_if_t<std::is_void_v<U>> * = nullptr>
void finish()
{
Q_ASSERT(!d.isFinished());
d.setFinished(true);
if (d.continuation()) {
if (d.isContextAlive()) {
d.invokeContinuation(nullptr);
}
}
}
QXmppTask<T> task()
{
return QXmppTask<T>(d);
}
private:
QXmpp::Private::TaskPrivate d;
};
#endif // QXMPPPROMISE_H
QXmppPromise
Create and update QXmppTask objects to communicate results of asynchronous operations.
Definition QXmppPromise.h:23
QXmppPromise::reportFinished
void reportFinished(T &&value)
Definition QXmppPromise.h:46
QXmppPromise::task
QXmppTask< T > task()
Definition QXmppPromise.h:96
QXmppTask
Definition QXmppTask.h:62