Leosac  0.8.0
Open Source Access Control
Leosac::Tasks::Task Class Referenceabstract

A base class for a tasks. More...

#include <Task.hpp>

+ Inheritance diagram for Leosac::Tasks::Task:
+ Collaboration diagram for Leosac::Tasks::Task:

Public Member Functions

 Task ()
 
virtual ~Task ()
 
 Task (const Task &)=delete
 
 Task (Task &&)=delete
 
Taskoperator= (const Task &)=delete
 
Taskoperator= (Task &&)=delete
 
bool is_complete () const
 Has the tasks completed its execution. More...
 
void wait ()
 Instead of spinlocking over is_complete() one can call wait() to hum... More...
 
void run ()
 
bool succeed () const
 
std::exception_ptr get_exception () const
 
template<typename Callback >
void set_on_completion (Callback c)
 Set a callback that will be invoked when the tasks is completed, no matter if it succeeded or not. More...
 
template<typename Callback >
void set_on_success (Callback c)
 Set a callback that will be invoked if and when the task succeed. More...
 
template<typename Callback >
void set_on_failure (Callback c)
 Set a callback that will be invoked if and when the task fails. More...
 
const std::string & get_guid () const
 

Private Member Functions

virtual bool do_run ()=0
 

Private Attributes

std::function< void(void)> on_completion_
 
std::function< void(void)> on_success_
 
std::function< void(void)> on_failure_
 
bool success_
 
std::exception_ptr eptr_
 
std::mutex mutex_
 
std::atomic_bool complete_
 
std::condition_variable cv_
 
std::string guid_
 

Detailed Description

A base class for a tasks.

It makes use of the Non Virtual Interface pattern to let subclasses implement the task code while it wraps it properly and take care of notifications.

Definition at line 37 of file Task.hpp.

Constructor & Destructor Documentation

◆ Task() [1/3]

Task::Task ( )

Definition at line 27 of file Task.cpp.

◆ ~Task()

virtual Leosac::Tasks::Task::~Task ( )
inlinevirtual

Definition at line 41 of file Task.hpp.

◆ Task() [2/3]

Leosac::Tasks::Task::Task ( const Task )
delete

◆ Task() [3/3]

Leosac::Tasks::Task::Task ( Task &&  )
delete

Member Function Documentation

◆ do_run()

◆ get_exception()

std::exception_ptr Task::get_exception ( ) const

Definition at line 93 of file Task.cpp.

◆ get_guid()

const std::string & Task::get_guid ( ) const

Definition at line 98 of file Task.cpp.

◆ is_complete()

bool Task::is_complete ( ) const

Has the tasks completed its execution.

This method is thread safe.

Definition at line 38 of file Task.cpp.

◆ operator=() [1/2]

Task& Leosac::Tasks::Task::operator= ( const Task )
delete

◆ operator=() [2/2]

Task& Leosac::Tasks::Task::operator= ( Task &&  )
delete

◆ run()

void Task::run ( )

Definition at line 43 of file Task.cpp.

◆ set_on_completion()

template<typename Callback >
void Leosac::Tasks::Task::set_on_completion ( Callback  c)
inline

Set a callback that will be invoked when the tasks is completed, no matter if it succeeded or not.

Note
: Status dependent callback (on_success/on_failure) will be called before the on_complete callback.

Definition at line 77 of file Task.hpp.

◆ set_on_failure()

template<typename Callback >
void Leosac::Tasks::Task::set_on_failure ( Callback  c)
inline

Set a callback that will be invoked if and when the task fails.

Note
: Status dependent callback (on_success/on_failure) will be called before the on_complete callback.

Definition at line 101 of file Task.hpp.

◆ set_on_success()

template<typename Callback >
void Leosac::Tasks::Task::set_on_success ( Callback  c)
inline

Set a callback that will be invoked if and when the task succeed.

Note
: Status dependent callback (on_success/on_failure) will be called before the on_complete callback.

Definition at line 89 of file Task.hpp.

◆ succeed()

bool Task::succeed ( ) const

Definition at line 88 of file Task.cpp.

◆ wait()

void Task::wait ( )

Instead of spinlocking over is_complete() one can call wait() to hum...

wait for the task's completion.

This is implemented using some condition_variable magic.

Definition at line 80 of file Task.cpp.

Member Data Documentation

◆ complete_

std::atomic_bool Leosac::Tasks::Task::complete_
private

Definition at line 119 of file Task.hpp.

◆ cv_

std::condition_variable Leosac::Tasks::Task::cv_
private

Definition at line 120 of file Task.hpp.

◆ eptr_

std::exception_ptr Leosac::Tasks::Task::eptr_
private

Definition at line 116 of file Task.hpp.

◆ guid_

std::string Leosac::Tasks::Task::guid_
private

Definition at line 121 of file Task.hpp.

◆ mutex_

std::mutex Leosac::Tasks::Task::mutex_
private

Definition at line 118 of file Task.hpp.

◆ on_completion_

std::function<void(void)> Leosac::Tasks::Task::on_completion_
private

Definition at line 111 of file Task.hpp.

◆ on_failure_

std::function<void(void)> Leosac::Tasks::Task::on_failure_
private

Definition at line 113 of file Task.hpp.

◆ on_success_

std::function<void(void)> Leosac::Tasks::Task::on_success_
private

Definition at line 112 of file Task.hpp.

◆ success_

bool Leosac::Tasks::Task::success_
private

Definition at line 115 of file Task.hpp.


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