KAsync::Job

KAsync::Job< Out, In > Class Template Reference

#include <async.h>

Inherits JobBase.

Public Types

typedef Out OutType
 

Public Member Functions

 Job (JobContinuation< Out, In... > &&func)
 
 Job (AsyncContinuation< Out, In... > &&func)
 
template<typename T >
Job< Out, In... > & addToContext (const T &value)
 
template<typename OutOther = void, typename ListType = Out, typename ValueType = typename ListType::value_type, std::enable_if_t<!std::is_void< ListType >::value, int > = 0>
Job< void, In... > each (JobContinuation< void, ValueType > &&func) const
 
template<typename FirstIn >
KAsync::Future< Out > exec (FirstIn in)
 
KAsync::Future< Out > exec ()
 
Job< Out, In... > & guard (const QObject *o)
 
Job< Out, In... > onError (SyncErrorContinuation< void > &&errorFunc) const
 
template<typename... InOther>
 operator std::conditional_t< std::is_void< OutType >::value, IncompleteType, Job< void >> ()
 
template<typename OutOther = void, typename ListType = Out, typename ValueType = typename ListType::value_type, std::enable_if_t<!std::is_void< ListType >::value, int > = 0>
Job< void, In... > serialEach (JobContinuation< void, ValueType > &&func) const
 
 std::declval ()))>
 
 std::declval ()))>
 
template<typename OutOther , typename... InOther>
Job< OutOther, In... > then (const Job< OutOther, InOther... > &job) const
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t< std::is_base_of< JobBase, decltype(func(std::declval< Out >()))>::value, Job< typename decltype(func(std::declval< Out >()))::OutType, In... >>
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t< std::is_base_of< JobBase, decltype(func())>::value, Job< typename decltype(func())::OutType, In... >>
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t< std::is_base_of< JobBase, decltype(func(KAsync::Error
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t< std::is_base_of< JobBase, decltype(func(KAsync::Error
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t<!std::is_base_of< JobBase, decltype(func(std::declval< Out >()))>::value, Job< decltype(func(std::declval< Out >())), In... >>
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t<!std::is_base_of< JobBase, decltype(func())>::value, Job< decltype(func()), In... >>
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t<!std::is_base_of< JobBase, decltype(func(KAsync::Error
 
template<typename OutOther = void, typename... InOther, typename F >
auto then (F &&func) const -> std::enable_if_t<!std::is_base_of< JobBase, decltype(func(KAsync::Error
 
template<typename OutOther , typename... InOther>
Job< OutOther, In... > then (AsyncContinuation< OutOther, InOther... > &&func) const
 
template<typename OutOther , typename... InOther>
Job< OutOther, In... > then (AsyncErrorContinuation< OutOther, InOther... > &&func) const
 
return thenImpl ({JobErrorContinuation< typename ResultJob::OutType >(std::forward< F >(func))}, Private::ExecutionFlag::Always)
 
return thenImpl ({SyncErrorContinuation< ResultType >(std::forward< F >(func))}, Private::ExecutionFlag::Always)
 

Related Functions

(Note that these are not member functions.)

Job< void > doWhile (const Job< ControlFlowFlag > &body)
 
Job< void > doWhile (const JobContinuation< ControlFlowFlag > &body)
 
template<typename Out = void>
Job< Out > error (int errorCode=1, const QString &errorMessage=QString())
 
template<typename Out = void>
Job< Out > error (const char *)
 
template<typename Out = void>
Job< Out > error (const Error &)
 
template<typename List , typename ValueType = typename List::value_type>
Job< void, List > forEach (KAsync::Job< void, ValueType > job)
 
template<typename List , typename ValueType = typename List::value_type>
Job< void, List > forEach (JobContinuation< void, ValueType > &&)
 
template<typename Out = void>
Job< Out > null ()
 
template<typename List , typename ValueType = typename List::value_type>
Job< void, List > serialForEach (KAsync::Job< void, ValueType > job)
 
template<typename List , typename ValueType = typename List::value_type>
Job< void, List > serialForEach (JobContinuation< void, ValueType > &&)
 
template<typename Out = void, typename... In, typename F >
auto start (F &&func) -> std::enable_if_t<!std::is_base_of< JobBase, decltype(func(std::declval< In >()...))>::value, Job< decltype(func(std::declval< In >()...)), In... >>
 
template<typename Out >
Job< Out > value (Out)
 
Job< void > wait (int delay)
 

Detailed Description

template<typename Out, typename... In>
class KAsync::Job< Out, In >

An Asynchronous job.

A single instance of Job represents a single method that will be executed asynchronously. The Job is started by exec(), which returns Future immediatelly. The Future will be set to finished state once the asynchronous task has finished. You can use Future::waitForFinished() to wait for for the Future in blocking manner.

It is possible to chain multiple Jobs one after another in different fashion (sequential, parallel, etc.). Calling exec() will then return a pending Future, and will execute the entire chain of jobs.

auto job = Job::start<QList<int>>(
MyREST::PendingUsers *pu = MyREST::requestListOfUsers();
QObject::connect(pu, &PendingOperation::finished,
[&](PendingOperation *pu) {
future->setValue(dynamic_cast<MyREST::PendingUsers*>(pu)->userIds());
future->setFinished();
});
})
[](const int &userId, KAsync::Future<QList<MyREST::User>> &future) {
MyREST::PendingUser *pu = MyREST::requestUserDetails(userId);
QObject::connect(pu, &PendingOperation::finished,
[&](PendingOperation *pu) {
future->setValue(Qlist<MyREST::User>() << dynamic_cast<MyREST::PendingUser*>(pu)->user());
future->setFinished();
});
});
KAsync::Future<QList<MyREST::User>> usersFuture = job.exec();
usersFuture.waitForFinished();
QList<MyRest::User> users = usersFuture.value();

In the example above, calling job.exec() will first invoke the first job, which will retrieve a list of IDs and then will invoke the second function for each single entry in the list returned by the first function.

Definition at line 76 of file async.h.

Member Function Documentation

template<typename Out, typename... In>
template<typename T >
Job<Out, In ...>& KAsync::Job< Out, In >::addToContext ( const T &  value)

Adds an unnamed value to the context.

The context is guaranteed to persist until the jobs execution has finished.

Useful for setting smart pointer to manage lifetime of objects required during the execution of the job.

Definition at line 510 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename ListType = Out, typename ValueType = typename ListType::value_type, std::enable_if_t<!std::is_void< ListType >::value, int > = 0>
Job<void, In ...> KAsync::Job< Out, In >::each ( JobContinuation< void, ValueType > &&  func) const

Shorthand for a forEach loop that automatically uses the return type of this job to deduce the type exepected.

Definition at line 473 of file async.h.

template<typename Out, typename... In>
template<typename FirstIn >
KAsync::Future<Out> KAsync::Job< Out, In >::exec ( FirstIn  in)

Starts execution of the job chain.

This will start the execution of the task chain, starting from the first one. It is possible to call this function multiple times, each invocation will start a new processing and provide a new Future to watch its status.

Parameters
inArgument to be passed to the very first task
Returns
Future<Out> object which will contain result of the last task once if finishes executing. See Future documentation for more details.
See also
exec(), Future
template<typename Out, typename... In>
KAsync::Future<Out> KAsync::Job< Out, In >::exec ( )

Starts execution of the job chain.

This will start the execution of the task chain, starting from the first one. It is possible to call this function multiple times, each invocation will start a new processing and provide a new Future to watch its status.

Returns
Future<Out> object which will contain result of the last task once if finishes executing. See Future documentation for more details.
See also
exec(FirstIn in), Future
template<typename Out, typename... In>
Job<Out, In ...>& KAsync::Job< Out, In >::guard ( const QObject o)

Adds a guard.

It is guaranteed that no callback is executed after the guard vanishes.

Use this i.e. ensure you don't call-back into an already destroyed object.

Definition at line 523 of file async.h.

template<typename Out, typename... In>
Job<Out, In ...> KAsync::Job< Out, In >::onError ( SyncErrorContinuation< void > &&  errorFunc) const

Shorthand for a job that receives the error only.

template<typename Out, typename... In>
template<typename... InOther>
KAsync::Job< Out, In >::operator std::conditional_t< std::is_void< OutType >::value, IncompleteType, Job< void >> ( )

Enable implicit conversion to Job<void>.

This is necessary in assignments that only use the return value (which is the normal case). This avoids constructs like: auto job = KAsync::start<int>( ... ) .then<void, int>( ... ) .then<void>([](){}); //Necessary for the assignment without the implicit conversion

template<typename Out, typename... In>
template<typename OutOther = void, typename ListType = Out, typename ValueType = typename ListType::value_type, std::enable_if_t<!std::is_void< ListType >::value, int > = 0>
Job<void, In ...> KAsync::Job< Out, In >::serialEach ( JobContinuation< void, ValueType > &&  func) const

Shorthand for a serialForEach loop that automatically uses the return type of this job to deduce the type exepected.

Definition at line 484 of file async.h.

template<typename Out, typename... In>
template<typename OutOther , typename... InOther>
Job<OutOther, In ...> KAsync::Job< Out, In >::then ( const Job< OutOther, InOther... > &  job) const

A continuation.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<std::is_base_of<JobBase, decltype(func(std::declval<Out>()))>::value, Job<typename decltype(func(std::declval<Out>()))::OutType, In...>>

Shorthands for a job that returns another job from it's continuation.

Continuation returning job: [] (Arg) -> KAsync::Job<...> { ... }

Definition at line 372 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<std::is_base_of<JobBase, decltype(func())>::value, Job<typename decltype(func())::OutType, In...>>

Void continuation with job: [] () -> KAsync::Job<...> { ... }.

Definition at line 382 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<std::is_base_of<JobBase, decltype(func(KAsync::Error

Error continuation returning job: [] (KAsync::Error, Arg) -> KAsync::Job<...> { ... }.

Definition at line 392 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<std::is_base_of<JobBase, decltype(func(KAsync::Error

Error void continuation returning job: [] (KAsync::Error) -> KAsync::Job<...> { ... }.

Definition at line 402 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<!std::is_base_of<JobBase, decltype(func(std::declval<Out>()))>::value, Job<decltype(func(std::declval<Out>())), In...>>

Sync continuation: [] (Arg) -> void { ... }.

Definition at line 412 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<!std::is_base_of<JobBase, decltype(func())>::value, Job<decltype(func()), In...>>

Sync void continuation: [] () -> void { ... }.

Definition at line 422 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<!std::is_base_of<JobBase, decltype(func(KAsync::Error

Sync error continuation: [] (KAsync::Error, Arg) -> void { ... }.

Definition at line 432 of file async.h.

template<typename Out, typename... In>
template<typename OutOther = void, typename... InOther, typename F >
auto KAsync::Job< Out, In >::then ( F &&  func) const -> std::enable_if_t<!std::is_base_of<JobBase, decltype(func(KAsync::Error

Sync void error continuation: [] (KAsync::Error) -> void { ... }.

Definition at line 442 of file async.h.

template<typename Out, typename... In>
template<typename OutOther , typename... InOther>
Job<OutOther, In ...> KAsync::Job< Out, In >::then ( AsyncContinuation< OutOther, InOther... > &&  func) const

Shorthand for a job that receives the error and a handle.

Definition at line 452 of file async.h.

template<typename Out, typename... In>
template<typename OutOther , typename... InOther>
Job<OutOther, In ...> KAsync::Job< Out, In >::then ( AsyncErrorContinuation< OutOther, InOther... > &&  func) const

Shorthand for a job that receives the error and a handle.

Definition at line 460 of file async.h.

Friends And Related Function Documentation

template<typename Out, typename... In>
Job< void > doWhile ( const Job< ControlFlowFlag > &  body)
related

Async while loop.

Loop continues while body returns ControlFlowFlag::Continue.

template<typename Out, typename... In>
Job< void > doWhile ( const JobContinuation< ControlFlowFlag > &  body)
related

Async while loop.

Shorthand that takes a continuation.

See also
doWhile
template<typename Out = void>
Job< Out > error ( int  errorCode = 1,
const QString errorMessage = QString() 
)
related

An error job.

An async error.

template<typename Out = void>
Job< Out > error ( const char *  )
related

An error job.

An async error.

template<typename Out = void>
Job< Out > error ( const Error &  )
related

An error job.

An async error.

template<typename List , typename ValueType = typename List::value_type>
Job< void, List > forEach ( KAsync::Job< void, ValueType >  job)
related

Async foreach loop.

This will execute a job for every value in the list. Errors while not stop processing of other jobs but set an error on the wrapper job.

template<typename List , typename ValueType = typename List::value_type>
Job< void, List > forEach ( JobContinuation< void, ValueType > &&  )
related

Async foreach loop.

Shorthand that takes a continuation.

See also
serialForEach
template<typename Out = void>
Job< Out > null ( )
related

A null job.

An async noop.

template<typename List , typename ValueType = typename List::value_type>
Job< void, List > serialForEach ( KAsync::Job< void, ValueType >  job)
related

Serial Async foreach loop.

This will execute a job for every value in the list sequentially. Errors while not stop processing of other jobs but set an error on the wrapper job.

template<typename List , typename ValueType = typename List::value_type>
Job< void, List > serialForEach ( JobContinuation< void, ValueType > &&  )
related

Serial Async foreach loop.

Shorthand that takes a continuation.

See also
serialForEach
template<typename Out = void, typename... In, typename F >
auto start ( F &&  func) -> std::enable_if_t<!std::is_base_of<JobBase, decltype(func(std::declval<In>() ...))>::value, Job<decltype(func(std::declval<In>() ...)), In...>>
related

Start an asynchronous job sequence.

start() is your starting point to build a chain of jobs to be executed asynchronously.

Parameters
funcA continuation to be executed.Sync continuation without job: [] () -> T { ... }

Definition at line 106 of file async.h.

template<typename Out >
Job< Out > value ( Out  )
related

Async value.

template<typename Out, typename... In>
Job< void > wait ( int  delay)
related

Async delay.


The documentation for this class was generated from the following file:
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Aug 13 2020 23:06:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.