Back to Index

Switch

Inherits from: Task

Contents

The Switch class is a control flow component in the task framework that provides multi-way conditional task execution. It evaluates a selector function and executes the corresponding case task, or a default task if no matching case is found. The class supports both string and integer selectors, making it flexible for various decision-making scenarios.

Features

Class Interface

Signals

Switch emits the following signals:

Signal Type Description Arguments
started Simple Emitted when the Switch begins execution None
finished Simple Emitted when the Switch completes execution None
caseSelected Data Emitted when a matching case is found String or int (the case value)
defaultSelected Simple Emitted when no case matches and default is used None
noMatchFound Simple Emitted when no case matches and no default exists None
error Data Reports errors during execution std::string (error message)

Usage Example

String-Based Switch Example
Integer-Based Switch Example

Control Flow

The Switch class implements multi-way conditional control flow using the following process:

  1. Selector Evaluation: The selector function is called with the provided arguments
  2. Case Matching: The result is compared against the registered cases
  3. Task Selection: If a match is found, the corresponding task is selected
  4. Default Handling: If no match is found, the default task is selected (if provided)
  5. Task Execution: The selected task is executed
Note: Unlike C/C++ switch statements, the Switch class does not support fallthrough behavior. Only one case is executed per evaluation.

Asynchronous Execution

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

This allows the switch evaluation and subsequent task execution to run in the background without blocking the main thread.

Lifecycle

A Switch task goes through the following lifecycle:

  1. Construction: Created with a selector function (string or integer)
  2. Case Definition: Cases are added via the case_() and default_() methods
  3. Execution Start: execute() or executeAsync() called, emits "started" signal
  4. Selector Evaluation: The selector function is called to determine which case to execute
  5. Case Selection: A matching case is found, or the default is used, with appropriate signals
  6. Task Execution: The selected task is executed
  7. Execution End: Switch completes, emits "finished" signal
  8. Error Handling: If exceptions occur, they are caught and reported via the "error" signal

The implementation automatically handles the proper emission of signals throughout the lifecycle, ensuring that components listening to the Switch can track its execution state.

Implementation Details

Here's a closer look at some key implementation details:

Task Selection Implementation

Best Practices

Switch works well with other components in the task framework:

Note: When choosing between If and Switch, consider the number of conditions. Use If for simple binary decisions and Switch for multi-way branching.

Additional Examples

State Machine Implementation