rili::StateMachine< StateBaseType, StorageType > Class Template Reference

Public Member Functions

 StateMachine (StateBaseType const &initState, StorageType const &storage=StorageType()) noexcept
 
template<typename EventType >
void handle (EventType const &event)
 
StorageType & storage () noexcept
 
StorageType const & storage () const noexcept
 

Detailed Description

template<typename StateBaseType, typename StorageType = std::nullptr_t>
class rili::StateMachine< StateBaseType, StorageType >

This implementation assume, that in every state any possible input event should be somehow handled (even by thrown exception), so to define all possible input events and "default" handlers for them StateBaseType is used.

Other states must derive from it and define custom behaviours of handlers. In one moment in state machine is only one state. So to define whole behaviours of given state polimorphism is strongly used. From usage point of view state is just set of handlers for some events with inheritance chain for handling all other.

Example State:

struct State: public SomeOther
{
virtual ~State() = default;
virtual BaseState const& handle(int const&, StorageType&) const override;
virtual BaseState const& handle(double const&, StorageType&) const override;
virtual BaseState const& handle(std::string const&, StorageType&) const override;
static inline State const& instance(){
static State instance;
return instance;
}
private:
State();
}
Note
All 'handle' members in state should be const - non const will not be choosen. This is to force correct usage of State.

Constructor & Destructor Documentation

template<typename StateBaseType , typename StorageType = std::nullptr_t>
rili::StateMachine< StateBaseType, StorageType >::StateMachine ( StateBaseType const &  initState,
StorageType const &  storage = StorageType() 
)
inlineexplicitnoexcept

initialize instance of StateMachine using StateBaseType instance(or inherited) and StorageType storage

Parameters
initStateis used as first state to handle events
storageis used to initialize internal StateMachine storage (is copied)

Member Function Documentation

template<typename StateBaseType , typename StorageType = std::nullptr_t>
template<typename EventType >
void rili::StateMachine< StateBaseType, StorageType >::handle ( EventType const &  event)
inline

is used as sink for all events which should be handled by StateMachine.

It will choose and call one handler in current State, which can handle given event. After handling, it will set state to that, returned by handle method of state for particular event.

Parameters
event
template<typename StateBaseType , typename StorageType = std::nullptr_t>
StorageType& rili::StateMachine< StateBaseType, StorageType >::storage ( )
inlinenoexcept

give access to StateMachine storage

Returns
storage object reference
template<typename StateBaseType , typename StorageType = std::nullptr_t>
StorageType const& rili::StateMachine< StateBaseType, StorageType >::storage ( ) const
inlinenoexcept

give access to StateMachine storage

Returns
storage object reference

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