Sched module

Task

Scheduled task with an attached trigger and scheduler association.

Task

#include <icy/sched/task.h>

Inherits: Task, ISerializable

Scheduled task with an attached trigger and scheduler association.

Public Methods

ReturnNameDescription
TaskConstructs a detached task without an associated scheduler. A trigger must be set before scheduling.
TaskConstructs a task associated with the given scheduler.
voidserialize virtualSerializes the task to JSON.
voiddeserialize virtualDeserializes the task from JSON.
T *createTrigger inlineCreates a trigger of type T, attaches it to this task, and returns a raw pointer to it. Ownership of the trigger is transferred to this task.
voidsetTriggerReplaces the current trigger with trigger.
sched::Trigger &triggerReturns a reference to the associated sched::Trigger or throws an exception.
Scheduler &schedulerReturns a reference to the associated Scheduler or throws an exception.
std::int64_tremaining constReturns the milliseconds remaining until the next scheduled timeout. An sched::Trigger must be associated or an exception will be thrown.
std::stringtype constReturns the registered type string for this task.
std::stringname constReturns the human-readable display name of this task.
voidsetNameSets the human-readable display name.

Task

Task(const std::string & type, const std::string & name)

Constructs a detached task without an associated scheduler. A trigger must be set before scheduling.

Parameters

  • type Registered type name used by TaskFactory.

  • name Human-readable display name.


Task

Task(Scheduler & scheduler, const std::string & type, const std::string & name)

Constructs a task associated with the given scheduler.

Parameters

  • scheduler Scheduler that will own and run this task.

  • type Registered type name used by TaskFactory.

  • name Human-readable display name.


serialize

virtual

virtual void serialize(json::Value & root)

Serializes the task to JSON.


deserialize

virtual

virtual void deserialize(json::Value & root)

Deserializes the task from JSON.


createTrigger

inline

template<typename T> inline T * createTrigger()

Creates a trigger of type T, attaches it to this task, and returns a raw pointer to it. Ownership of the trigger is transferred to this task.

Parameters

Returns

Raw pointer to the newly created trigger (still owned by this task).


setTrigger

void setTrigger(std::unique_ptr< sched::Trigger > trigger)

Replaces the current trigger with trigger.

Parameters

  • trigger Owning pointer to the new trigger; must not be null before calling.

trigger

sched::Trigger & trigger()

Returns a reference to the associated sched::Trigger or throws an exception.


scheduler

Scheduler & scheduler()

Returns a reference to the associated Scheduler or throws an exception.


remaining

const

std::int64_t remaining() const

Returns the milliseconds remaining until the next scheduled timeout. An sched::Trigger must be associated or an exception will be thrown.


type

const

std::string type() const

Returns the registered type string for this task.


name

const

std::string name() const

Returns the human-readable display name of this task.


setName

void setName(const std::string & name)

Sets the human-readable display name.

Parameters

  • name New display name.

Protected Attributes

ReturnNameDescription
std::string_type
std::string_name
sched::Scheduler *_scheduler
std::unique_ptr< sched::Trigger >_trigger
std::mutex_mutex

_type

std::string _type

_name

std::string _name

_scheduler

sched::Scheduler * _scheduler

_trigger

std::unique_ptr< sched::Trigger > _trigger

_mutex

std::mutex _mutex

Protected Methods

ReturnNameDescription
boolbeforeRun virtual
voidrunCalled by the TaskRunner to run the task. Override this method to implement task action. Returning true means the task should be called again, and false will cause the task to be destroyed. The task will similarly be destroyed if destroy() was called during the current task iteration.
boolafterRun virtual

beforeRun

virtual

virtual bool beforeRun()

run

void run()

Called by the TaskRunner to run the task. Override this method to implement task action. Returning true means the task should be called again, and false will cause the task to be destroyed. The task will similarly be destroyed if destroy() was called during the current task iteration.


afterRun

virtual

virtual bool afterRun()