DynExp
Highly flexible laboratory automation for dynamically changing experiments.
Util::StateMachine< StateMachineStateT > Class Template Reference

This class models a state machine. It keeps track of the current state and allows to invoke its associated state function. The return value of the state function determines the new state to transition into. Additionally, a context of type StateMachineContext can be assigned to the state machine. Contexts allow for replacing states with other states. This makes sense to write inner protocols BeginState -> StateA -> ... -> StateN -> EndState and to embed them into outer protocols. The outer protocol can transition into BeginState and take control back by replacing EndState with one of its own states by setting a respective context. All states (of type StateMachineState) have to be registered upon construction of the StateMachine instance. More...

Public Types

using StateType = StateMachineStateT
 
using StateEnumType = typename StateType::StateEnumType
 Refer to class StateMachineState. More...
 
using ContextType = StateMachineContext< StateMachineStateT >
 

Public Member Functions

template<typename... StateMachineStateTs>
 StateMachine (const StateType &InitialState, const StateMachineStateTs &... States)
 Constructs a state machine assigning possible states to it. Automatically also adds InitialState, so don't include it into States. More...
 
const StateTypeGetCurrentState () const noexcept
 Returns a pointer to the current state. More...
 
const ContextTypeGetContext () const noexcept
 Returns a pointer to the current context. More...
 
void SetCurrentState (StateEnumType NewState)
 Sets the current state as identified by an element from StateEnumType. More...
 
void SetContext (const ContextType *NewContext)
 Sets the current state machine context. More...
 
void ResetContext ()
 Removes the current state machine context. More...
 
template<typename... ArgTs>
void Invoke (instance_of_t< typename StateType::CallableType > &Instance, ArgTs &&... Args)
 Invokes the state function associated with the current state machine state on an instance of the class the state functions are member functions of. Refer to class StateMachineState. Also sets the state machine's state to the return value of the state function. More...
 

Private Attributes

const std::unordered_map< StateEnumType, const StateType * > StatesList
 Map of possible states. All states are uniquely identified by an element from StateEnumType. Refer to StateMachineState. More...
 
Atomic members for thread-safety

Atomic to allow reading the current state and context from one thread and setting the current state and context from another thread without further synchronization.

std::atomic< const StateType * > CurrentState
 
std::atomic< const ContextType * > CurrentContext
 

Detailed Description

template<typename StateMachineStateT>
class Util::StateMachine< StateMachineStateT >

This class models a state machine. It keeps track of the current state and allows to invoke its associated state function. The return value of the state function determines the new state to transition into. Additionally, a context of type StateMachineContext can be assigned to the state machine. Contexts allow for replacing states with other states. This makes sense to write inner protocols BeginState -> StateA -> ... -> StateN -> EndState and to embed them into outer protocols. The outer protocol can transition into BeginState and take control back by replacing EndState with one of its own states by setting a respective context. All states (of type StateMachineState) have to be registered upon construction of the StateMachine instance.

Template Parameters
StateMachineStateTState type of type StateMachineState the state machine operates on

Definition at line 1434 of file Util.h.

Member Typedef Documentation

◆ ContextType

template<typename StateMachineStateT >
using Util::StateMachine< StateMachineStateT >::ContextType = StateMachineContext<StateMachineStateT>

Definition at line 1439 of file Util.h.

◆ StateEnumType

template<typename StateMachineStateT >
using Util::StateMachine< StateMachineStateT >::StateEnumType = typename StateType::StateEnumType

Refer to class StateMachineState.

Definition at line 1438 of file Util.h.

◆ StateType

template<typename StateMachineStateT >
using Util::StateMachine< StateMachineStateT >::StateType = StateMachineStateT

Definition at line 1437 of file Util.h.

Constructor & Destructor Documentation

◆ StateMachine()

template<typename StateMachineStateT >
template<typename... StateMachineStateTs>
Util::StateMachine< StateMachineStateT >::StateMachine ( const StateType InitialState,
const StateMachineStateTs &...  States 
)
inline

Constructs a state machine assigning possible states to it. Automatically also adds InitialState, so don't include it into States.

Template Parameters
...StateMachineStateTsType of the States parameter pack
Parameters
InitialStateInitial state of the state machine.
...StatesPossible states the state machine can transition into. Pass states by reference to prevent from null pointers.

Definition at line 1450 of file Util.h.

Member Function Documentation

◆ GetContext()

template<typename StateMachineStateT >
const ContextType* Util::StateMachine< StateMachineStateT >::GetContext ( ) const
inlinenoexcept

Returns a pointer to the current context.

Definition at line 1456 of file Util.h.

◆ GetCurrentState()

template<typename StateMachineStateT >
const StateType* Util::StateMachine< StateMachineStateT >::GetCurrentState ( ) const
inlinenoexcept

Returns a pointer to the current state.

Definition at line 1455 of file Util.h.

◆ Invoke()

template<typename StateMachineStateT >
template<typename... ArgTs>
void Util::StateMachine< StateMachineStateT >::Invoke ( instance_of_t< typename StateType::CallableType > &  Instance,
ArgTs &&...  Args 
)
inline

Invokes the state function associated with the current state machine state on an instance of the class the state functions are member functions of. Refer to class StateMachineState. Also sets the state machine's state to the return value of the state function.

Template Parameters
...ArgTsType of the parameter pack to forward to the state function
Parameters
InstanceInstance to invoke the state function on
...ArgsParameters to forward to the state function
Exceptions
InvalidStateExceptionis thrown if the current state is nullptr.

Definition at line 1492 of file Util.h.

◆ ResetContext()

template<typename StateMachineStateT >
void Util::StateMachine< StateMachineStateT >::ResetContext ( )
inline

Removes the current state machine context.

Definition at line 1480 of file Util.h.

◆ SetContext()

template<typename StateMachineStateT >
void Util::StateMachine< StateMachineStateT >::SetContext ( const ContextType NewContext)
inline

Sets the current state machine context.

Parameters
NewContextContext to set

Definition at line 1475 of file Util.h.

◆ SetCurrentState()

template<typename StateMachineStateT >
void Util::StateMachine< StateMachineStateT >::SetCurrentState ( StateEnumType  NewState)
inline

Sets the current state as identified by an element from StateEnumType.

Parameters
NewStateIdentifier of the new state machine state
Exceptions
throwstd::out_of_range is thrown if NewState does not exist in StatesList.

Definition at line 1463 of file Util.h.

Member Data Documentation

◆ CurrentContext

template<typename StateMachineStateT >
std::atomic<const ContextType*> Util::StateMachine< StateMachineStateT >::CurrentContext
private

Definition at line 1516 of file Util.h.

◆ CurrentState

template<typename StateMachineStateT >
std::atomic<const StateType*> Util::StateMachine< StateMachineStateT >::CurrentState
private

Definition at line 1515 of file Util.h.

◆ StatesList

template<typename StateMachineStateT >
const std::unordered_map<StateEnumType, const StateType*> Util::StateMachine< StateMachineStateT >::StatesList
private

Map of possible states. All states are uniquely identified by an element from StateEnumType. Refer to StateMachineState.

Definition at line 1508 of file Util.h.


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