Loading...
Searching...
No Matches
QXmppTask< T > Class Template Reference
Core classes

`#include <QXmppTask.h>`

## Public Member Functions

void | then (const QObject *context, Continuation continuation)
bool | isFinished () const
bool | hasResult () const
const T & | result () const
T | 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

## hasResult()

template<typename T>

bool QXmppTask< T >::hasResult | ( |  | ) | const

inline nodiscard

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

## isFinished()

template<typename T>

bool QXmppTask< T >::isFinished | ( |  | ) | const

inline nodiscard

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>

const T & QXmppTask< T >::result | ( |  | ) | const

inline nodiscard

The result of the operation.

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

## takeResult()

template<typename T>

T QXmppTask< T >::takeResult | ( |  | ) |

inline nodiscard

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>

void QXmppTask< T >::then | ( | const QObject * | context,
|  | Continuation | continuation )

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 be called multiple times if and only if T is copy-constructible. For copyable types all continuations will be called in the order they were set. For move-only types only the latest continuation will be called.

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::result
const T & result() const
Definition QXmppTask.h:156

Note
  Support for multiple continuations with copy-constructible types was added in QXmpp 1.11.

Parameters

  context | 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 | A function accepting a result in the form of T &&.

## toFuture()

template<typename T>

QFuture< T > QXmppTask< T >::toFuture | ( | const QObject * | context | ) |

inline

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

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

- QXmppTask.h