QXmpp Version: 1.15.0
Loading...
Searching...
No Matches
QXmppTask< T > Class Template Reference

#include <QXmppTask.h>

Public Member Functions

 QXmppTask (QXmppTask &&t)
 Move constructor.
 QXmppTask (const QXmppTask &)=delete
QXmppTaskoperator= (QXmppTask &&t) noexcept
 Move assignment operator.
QXmppTaskoperator= (const QXmppTask &)=delete
template<typename Continuation>
auto then (const QObject *context, Continuation continuation) -> QXmppTask< QXmpp::Private::InvokeContinuationResult< Continuation, T > >
QXmppTask< T > & withContext (const QObject *c)
void cancel ()
bool isFinished () const
bool hasResult () const
QXmpp::Private::ConstRefOrVoid< T > result () const
takeResult ()
QFuture< T > toFuture (const QObject *context)

Friends

class QXmppPromise< T >

Detailed Description

template<typename T>
class QXmppTask< T >

Handle for an ongoing operation that finishes in the future.

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.

Since
QXmpp 1.5

Member Function Documentation

◆ cancel()

template<typename T>
void QXmppTask< T >::cancel ( )
inline

Cancels the task

If there is a waiting coroutine, it is cancelled immediately. Any continuation set in the future also won't be executed.

Since
QXmpp 1.11

◆ hasResult()

template<typename T>
bool QXmppTask< T >::hasResult ( ) const
inlinenodiscard

Returns whether the task is finished and the value has not been taken yet.

◆ isFinished()

template<typename T>
bool QXmppTask< T >::isFinished ( ) const
inlinenodiscard

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().

◆ result()

template<typename T>
QXmpp::Private::ConstRefOrVoid< T > QXmppTask< T >::result ( ) const
inlinenodiscard

The result of the operation.

Warning
This can only be used once the operation is finished.

◆ takeResult()

template<typename T>
T QXmppTask< T >::takeResult ( )
inlinenodiscard

Moves the result of the operation out of the task.

Warning
This can only be used once and only after the operation has finished.

◆ then()

template<typename T>
template<typename Continuation>
auto QXmppTask< T >::then ( const QObject * context,
Continuation continuation ) -> QXmppTask< QXmpp::Private::InvokeContinuationResult< Continuation, T > >
inline

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.
QXmppTask(QXmppTask &&t)
Move constructor.
Definition QXmppTask.h:343
QXmpp::Private::ConstRefOrVoid< T > result() const
Definition QXmppTask.h:543
Parameters
contextQObject 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.
continuationA function accepting a result in the form of T &&.

◆ toFuture()

template<typename T>
QFuture< T > QXmppTask< T >::toFuture ( const QObject * context)
inlinenodiscard

Converts the Task into a QFuture. Afterwards the QXmppTask object is invalid.

◆ withContext()

template<typename T>
QXmppTask< T > & QXmppTask< T >::withContext ( const QObject * c)
inline

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
reference to this task
Since
QXmpp 1.11

The documentation for this class was generated from the following file: