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

A high-performance finite state machine implementation. More...

#include <FSM.hpp>

Inheritance diagram for fsmgine::FSM< TEvent >:

Public Types

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

Public Member Functions

 FSM ()=default
 Default constructor.
 
 FSM (const FSM &)=delete
 
FSMoperator= (const FSM &)=delete
 
 FSM (FSM &&other) noexcept
 Move constructor.
 
FSMoperator= (FSM &&other) noexcept
 Move assignment operator.
 
FSMBuilder< TEvent > get_builder ()
 Creates a builder for fluent FSM construction.
 
void setInitialState (std::string_view state)
 Sets the initial state of the FSM.
 
void setCurrentState (std::string_view state)
 Changes the current state of the FSM.
 
std::string_view getCurrentState () const
 Gets the name of the current state.
 
bool process (const TEvent &event)
 Processes an event and potentially transitions to a new state.
 
bool process ()
 Processes a transition for event-less FSMs.
 

Friends

class FSMBuilder< TEvent >
 
class TransitionBuilder< TEvent >
 

Detailed Description

template<typename TEvent = std::monostate>
class fsmgine::FSM< TEvent >

A high-performance finite state machine implementation.

Template Parameters
TEventThe event type used for transitions (defaults to std::monostate for event-less FSMs)

The FSM class provides a flexible and efficient state machine implementation with the following features:

  • Type-safe state and event handling
  • Support for guards (predicates) and actions on transitions
  • On-enter and on-exit actions for states
  • String interning for optimized state name storage
  • Thread-safety when using the FSMgineMT library variant
Thread Safety
The thread-safety of FSM operations depends on which library variant you're using:
  • FSMgine: No thread synchronization, optimal for single-threaded applications
  • FSMgineMT: Full thread-safety with mutex protection on all operations
Example
// Define an event type
struct Event { std::string type; };
// Create an FSM
FSM<Event> machine;
machine.get_builder()
.from("Idle").to("Working").when([](const Event& e) { return e.type == "start"; })
.from("Working").to("Idle").when([](const Event& e) { return e.type == "stop"; })
.build("Idle");
// Process events
machine.process(Event{"start"}); // Transitions to "Working"
A high-performance finite state machine implementation.
Definition FSM.hpp:94
bool process(const TEvent &event)
Processes an event and potentially transitions to a new state.
Definition FSM.hpp:294
FSMBuilder< TEvent > get_builder()
Creates a builder for fluent FSM construction.
Definition FSM.hpp:227

Member Typedef Documentation

◆ Action

template<typename TEvent = std::monostate>
using fsmgine::FSM< TEvent >::Action = std::function<void(const TEvent&)>

Type alias for transition actions.

Functions executed during transitions or state changes

◆ Predicate

template<typename TEvent = std::monostate>
using fsmgine::FSM< TEvent >::Predicate = std::function<bool(const TEvent&)>

Type alias for transition predicates.

Functions that evaluate whether a transition should occur based on an event

Constructor & Destructor Documentation

◆ FSM()

template<typename TEvent = std::monostate>
fsmgine::FSM< TEvent >::FSM ( FSM< TEvent > &&  other)
inlinenoexcept

Move constructor.

Parameters
otherFSM to move from

Member Function Documentation

◆ get_builder()

template<typename TEvent >
FSMBuilder< TEvent > fsmgine::FSM< TEvent >::get_builder ( )

Creates a builder for fluent FSM construction.

Returns
A new FSMBuilder instance for this FSM
Example
fsm.get_builder()
.from("A").to("B").when([](const auto& e) { return true; })
.build("A");
Main namespace for the FSMgine library.
Definition FSM.hpp:23

◆ getCurrentState()

template<typename TEvent >
std::string_view fsmgine::FSM< TEvent >::getCurrentState ( ) const

Gets the name of the current state.

Returns
The current state name
Exceptions
FSMNotInitializedErrorif no initial state has been set

◆ operator=()

template<typename TEvent = std::monostate>
FSM & fsmgine::FSM< TEvent >::operator= ( FSM< TEvent > &&  other)
inlinenoexcept

Move assignment operator.

Parameters
otherFSM to move from
Returns
Reference to this FSM

◆ process() [1/2]

template<typename TEvent = std::monostate>
bool fsmgine::FSM< TEvent >::process ( )
inline

Processes a transition for event-less FSMs.

Returns
true if a transition occurred, false otherwise
Note
This method is only available for FSM<> or FSM<std::monostate>

◆ process() [2/2]

template<typename TEvent >
bool fsmgine::FSM< TEvent >::process ( const TEvent &  event)

Processes an event and potentially transitions to a new state.

Parameters
eventThe event to process
Returns
true if a transition occurred, false otherwise
Exceptions
FSMNotInitializedErrorif no initial state has been set
FSMStateNotFoundErrorif the current state is invalid
FSMInvalidStateErrorif a transition has no target state

◆ setCurrentState()

template<typename TEvent >
void fsmgine::FSM< TEvent >::setCurrentState ( std::string_view  state)

Changes the current state of the FSM.

Parameters
stateThe name of the state to transition to
Exceptions
FSMInvalidStateErrorif the state doesn't exist
Note
This executes on-exit actions for the current state and on-enter actions for the new state

◆ setInitialState()

template<typename TEvent >
void fsmgine::FSM< TEvent >::setInitialState ( std::string_view  state)

Sets the initial state of the FSM.

Parameters
stateThe name of the initial state
Exceptions
FSMInvalidStateErrorif the state doesn't exist
Note
This also executes any on-enter actions for the initial state

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