QXmppTask Class
template <typename T> class QXmppTaskHandle for an ongoing operation that finishes in the future. More...
| Header: | #include <QXmppTask.h> |
| Since: | QXmpp 1.5 |
- List of all members, including inherited members
- QXmppTask is part of Core classes.
Public Functions
| QXmppTask(const QXmppTask<T> &) | |
| QXmppTask(QXmppTask<T> &&t) | |
(since QXmpp 1.15) void | cancel() |
| bool | hasResult() const |
| bool | isFinished() const |
| QXmpp::Private::ConstRefOrVoid<T> | result() const |
| T | takeResult() |
| QXmppTask<QXmpp::Private::InvokeContinuationResult<Continuation, T>> | then(const QObject *context, Continuation continuation) |
| QFuture<T> | toFuture(const QObject *context) |
(since QXmpp 1.15) QXmppTask<T> & | withContext(const QObject *c) |
| QXmppTask<T> & | operator=(QXmppTask<T> &&t) |
| QXmppTask<T> & | operator=(const QXmppTask<T> &) |
Detailed Description
Tasks are generated by QXmppPromise and can be handled using QXmppTask::then().
Unlike QFuture, this is *not* thread-safe!! This avoids the need to do mutex locking at every access though.
Member Function Documentation
[delete] QXmppTask::QXmppTask(const QXmppTask<T> &)
Copy-constructs an instance of QXmppTask. This function is deleted.
QXmppTask::QXmppTask(QXmppTask<T> &&t)
Move constructor.
[since QXmpp 1.15] void QXmppTask::cancel()
Cancels the task
If there is a waiting coroutine, it is cancelled immediately. Any continuation set in the future also won't be executed.
This function was introduced in QXmpp 1.15.
bool QXmppTask::hasResult() const requires (!std::is_void_v<T>)
Returns whether the task is finished and the value has not been taken yet.
bool QXmppTask::isFinished() const
Returns whether the asynchronous operation is already finished.
This does not mean that the result is still stored, it might have been taken using takeResult() or handled using then().
QXmpp::Private::ConstRefOrVoid<T> QXmppTask::result() const requires (!std::is_void_v<T>)
The result of the operation.
Warning: This can only be used once the operation is finished.
T QXmppTask::takeResult() requires (!std::is_void_v<T>)
Moves the result of the operation out of the task.
Warning: This can only be used once and only after the operation has finished.
template <typename Continuation> QXmppTask<QXmpp::Private::InvokeContinuationResult<Continuation, T>> QXmppTask::then(const QObject *context, Continuation continuation)
Registers a function that will be called with the result as parameter when the asynchronous operation finishes.
If the task is already finished when calling this (and still has a result), the function will be called immediately.
.then() can only be called once.
Example usage:
QXmppTask<QString> generateSomething();
void Manager::generate()
{
generateSomething().then(this, [](QString &&result) {
// runs as soon as the result is finished
qDebug() << "Generating finished:" << result;
});
// The generation could still be in progress here and the lambda might not
// have been executed yet.
}
// Manager is derived from QObject.
context is the QObject used for unregistering the handler function when the object is deleted. This way your lambda will never be executed after your object has been deleted. continuation is a function accepting a result in the form of T &&.
Returns a new QXmppTask that finishes with the value returned by continuation, allowing further chaining via another .then() or co_await. If continuation returns void, the returned task is QXmppTask<void> which finishes once the continuation has run. Returns true on success.
QFuture<T> QXmppTask::toFuture(const QObject *context)
Converts the Task into a QFuture. Afterwards the QXmppTask object is invalid.
context.
[since QXmpp 1.15] QXmppTask<T> &QXmppTask::withContext(const QObject *c)
Sets a context object for co_await.
If this task is co_await'ed, the coroutine will only be resumed if the context object is still alive. This is very helpful for usage in member functions to assure that this still exists after an co_await statement.
Returns a reference to this task.
c.
This function was introduced in QXmpp 1.15.
[noexcept] QXmppTask<T> &QXmppTask::operator=(QXmppTask<T> &&t)
Move assignment operator, moving from t.
[delete] QXmppTask<T> &QXmppTask::operator=(const QXmppTask<T> &)
Copy-assigns other to this QXmppTask instance. This function is deleted.