Back to Index

Algorithm

Inherits from: Task

Contents

The Algorithm class is a core component of the task library that provides a framework for implementing executable algorithms with standardized signaling, progress reporting, and execution control. It extends the base Task class to add algorithm-specific functionality including execution state tracking, cancellation support, and dirty state management.

Features

Class Interface

Signals

Algorithm emits the following signals:

Signal Type Description Arguments
progress data Data Reports execution progress float (0.0 to 1.0)
started simple Simple Indicates algorithm has started execution None
finished simple Simple Indicates algorithm has completed execution None
error data Data Reports execution errors std::string (error message)

Usage Example

Algorithm Implementation Example
Using the Algorithm

The Dirty State

The Algorithm class maintains a "dirty" state that indicates whether the algorithm needs to be re-executed:

This mechanism is useful for:

Note: Setting dirty to true while an algorithm is running causes a stop request to be issued.

Asynchronous Execution

The run() method provides asynchronous execution using C++ futures:

This allows algorithms to run in the background without blocking the main thread.

Cancellation Mechanism

Algorithms can be cancelled during execution:

  1. Requesting Cancellation: Call stop() to set the stop flag
  2. Checking for Cancellation: Algorithm should periodically call stopRequested()
  3. Handling Cancellation: If stop is requested, algorithm can clean up and exit
Handling Cancellation

Lifecycle

An Algorithm goes through the following lifecycle:

  1. Construction: Internal state initialized (dirty=true, running=false, stopRequested=false)
  2. Configuration: Algorithm-specific parameters set by calling its methods
  3. Execution Start: run() or runImpl() called, emits "started" signal
  4. Execution: exec() implementation runs, may report progress
  5. Execution End: Algorithm completes, emits "finished" signal
  6. Error Handling: If an exception occurs, emits "error" signal with details

This lifecycle can be repeated multiple times for the same algorithm instance.

The implementation of runImpl() automatically handles the proper emission of "started" and "finished" signals, as well as the management of the m_isRunning state flag.

Implementation Details

Here's a simplified view of the key implementation methods:

Constructor Implementation
Progress Reporting Implementation
Asynchronous Execution Implementation
Run Implementation

Best Practices

Extensions

Algorithm is extended by several specialized classes in the framework:

Note: When implementing a new algorithm, consider whether one of the existing specialized algorithm classes might provide functionality that matches your requirements.