FSMgine
High-performance finite state machine library for C++17 with single-threaded and multi-threaded variants
Loading...
Searching...
No Matches
fsmgine::Transition< TEvent > Class Template Reference

Represents a transition between states in a finite state machine. More...

#include <Transition.hpp>

Public Types

using Predicate = std::function< bool(const TEvent &)>
 Type alias for transition guard predicates.
 
using Action = std::function< void(const TEvent &)>
 Type alias for transition actions.
 

Public Member Functions

 Transition ()=default
 Default constructor.
 
 Transition (const Transition &)=delete
 
Transitionoperator= (const Transition &)=delete
 
 Transition (Transition &&)=default
 Move constructor (defaulted)
 
Transitionoperator= (Transition &&)=default
 Move assignment operator (defaulted)
 
void addPredicate (Predicate pred)
 Adds a predicate (guard condition) to this transition.
 
void addAction (Action action)
 Adds an action to execute when this transition occurs.
 
void setTargetState (std::string_view state)
 Sets the target state for this transition.
 
bool predicatesPass (const TEvent &event) const
 Evaluates all predicates for this transition.
 
void executeActions (const TEvent &event) const
 Executes all actions associated with this transition.
 
std::string_view getTargetState () const
 Gets the target state for this transition.
 
const std::vector< Action > & getActions () const
 Gets all actions associated with this transition.
 
bool hasPredicates () const
 Checks if this transition has any predicates.
 
bool hasActions () const
 Checks if this transition has any actions.
 
bool hasTargetState () const
 Checks if this transition has a target state.
 

Friends

class TransitionBuilder< TEvent >
 

Detailed Description

template<typename TEvent>
class fsmgine::Transition< TEvent >

Represents a transition between states in a finite state machine.

Template Parameters
TEventThe event type that triggers transitions

A Transition encapsulates:

  • Zero or more predicates (guard conditions) that must all pass for the transition to occur
  • Zero or more actions to execute when the transition occurs
  • The target state to transition to

Transitions are typically created using the FSMBuilder fluent API rather than directly.

Predicate Evaluation
  • If no predicates are added, the transition always passes
  • If multiple predicates are added, ALL must return true (AND logic)
  • Predicates are evaluated in the order they were added
Action Execution
  • Actions are executed in the order they were added
  • Actions are only executed if all predicates pass
  • Actions are executed before the state change occurs

Member Function Documentation

◆ addAction()

template<typename TEvent >
void fsmgine::Transition< TEvent >::addAction ( Action  action)

Adds an action to execute when this transition occurs.

Parameters
actionA function to execute during the transition
Note
Multiple actions can be added; they execute in order
Null actions are ignored
This method is primarily for use by TransitionBuilder

◆ addPredicate()

template<typename TEvent >
void fsmgine::Transition< TEvent >::addPredicate ( Predicate  pred)

Adds a predicate (guard condition) to this transition.

Parameters
predA function that returns true if the transition should be allowed
Note
Multiple predicates can be added; all must pass for the transition to occur
Null predicates are ignored
This method is primarily for use by TransitionBuilder

◆ executeActions()

template<typename TEvent >
void fsmgine::Transition< TEvent >::executeActions ( const TEvent &  event) const

Executes all actions associated with this transition.

Parameters
eventThe event that triggered the transition
Note
Actions are executed in the order they were added

◆ getActions()

template<typename TEvent >
const std::vector< typename Transition< TEvent >::Action > & fsmgine::Transition< TEvent >::getActions ( ) const

Gets all actions associated with this transition.

Returns
A const reference to the vector of actions

◆ getTargetState()

template<typename TEvent >
std::string_view fsmgine::Transition< TEvent >::getTargetState ( ) const

Gets the target state for this transition.

Returns
The target state name, or empty string_view if not set

◆ hasActions()

template<typename TEvent >
bool fsmgine::Transition< TEvent >::hasActions ( ) const

Checks if this transition has any actions.

Returns
true if at least one action exists

◆ hasPredicates()

template<typename TEvent >
bool fsmgine::Transition< TEvent >::hasPredicates ( ) const

Checks if this transition has any predicates.

Returns
true if at least one predicate exists

◆ hasTargetState()

template<typename TEvent >
bool fsmgine::Transition< TEvent >::hasTargetState ( ) const

Checks if this transition has a target state.

Returns
true if a target state has been set

◆ predicatesPass()

template<typename TEvent >
bool fsmgine::Transition< TEvent >::predicatesPass ( const TEvent &  event) const

Evaluates all predicates for this transition.

Parameters
eventThe event to evaluate predicates against
Returns
true if all predicates pass (or no predicates exist), false otherwise

◆ setTargetState()

template<typename TEvent >
void fsmgine::Transition< TEvent >::setTargetState ( std::string_view  state)

Sets the target state for this transition.

Parameters
stateThe name of the state to transition to
Note
The state should be interned using StringInterner for consistency
This method is primarily for use by TransitionBuilder

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