All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Radiant::FunctionTask Class Reference

This class executes the given function within BGThread. More...

#include <Radiant/Task.hpp>

Inheritance diagram for Radiant::FunctionTask:
Radiant::Task Patterns::NotCopyable

Public Member Functions

virtual void doTask ()
 The actual work the task does should be implemented in here. More...
 
 FunctionTask (std::function< void(Task &)> func)
 Construct a new FunctionTask. More...
 
- Public Member Functions inherited from Radiant::Task
bool isCanceled () const
 Returns whether a task has been canceled. More...
 
Priority priority () const
 Get the current priority of the task. More...
 
void runNow (bool finish)
 If the task isn't already finished, runs the task immediately in the calling thread. More...
 
void schedule (Radiant::TimeStamp ts)
 Schedule the next execution time for this task. More...
 
Radiant::TimeStamp scheduled () const
 Return a timestamp for the next execution of the task. More...
 
void scheduleFromNow (Radiant::TimeStamp wait)
 Schedule the next execution time for this task. More...
 
void scheduleFromNowSecs (double seconds)
 Schedule the next execution time for this task. More...
 
void setCanceled ()
 Marks the task as canceled, so it will be removed.
 
void setFinished ()
 Marks the task as finished, so it will be removed.
 
void setPriority (Priority priority)
 Sets priority for task. More...
 
State state () const
 Get the current state of the task. More...
 
 Task (Priority p=PRIORITY_NORMAL)
 Constructs a task with the given priority. More...
 
virtual ~Task ()
 Destructor.
 

Static Public Member Functions

static void executeInBGThread (std::function< void(Task &)> func)
 

Additional Inherited Members

- Public Types inherited from Radiant::Task
enum  { PRIORITY_LOW = 1, PRIORITY_NORMAL = 500, PRIORITY_HIGH = 1000, PRIORITY_URGENT = 1500 }
 Standard priorities for tasks. More...
 
enum  State { WAITING, RUNNING, DONE }
 State of the task. More...
 
- Protected Member Functions inherited from Radiant::Task
virtual void canceled ()
 Cancel the task. More...
 
virtual void finished ()
 Finished the task. More...
 
virtual void initialize ()
 Initialize the task. Called by BGThread before the task is processed.
 
void setState (State s)
 Sets the task state. More...
 

Detailed Description

This class executes the given function within BGThread.

Task is given as a parameter to the function that is being executed in task because function needs to finish task explicitly ie. call Task::setFinished when everything is ready. The following example shows how to create and use FunctionTask:

std::function<void (Task &)> taskFunction = [] (Radiant::Task& t) {
// This will do some costly computing. It is called repeatedly as
// long as task is alive.
if(ready)
t.setFinished();
};
Radiant::TaskPtr task = std::make_shared<Radiant::FunctionTask>(taskFunction);
Radiant::BGThread::instance()->addTask(task);

Constructor & Destructor Documentation

Radiant::FunctionTask::FunctionTask ( std::function< void(Task &)>  func)

Construct a new FunctionTask.

Parameters
funcfunction to execute

Member Function Documentation

virtual void Radiant::FunctionTask::doTask ( )
virtual

The actual work the task does should be implemented in here.

Override in the derived class. When the task is finished it is important to set the State of the task to State::DONE (f.ex. calling setFinished) so that BGThread can properly release the task. If the state of the task is not set to State::DONE after call to this function this task is scheduled to be executed later.

Implements Radiant::Task.