Async 1.8.0
|
Implements a hierarchial state machine. More...
#include <AsyncStateMachine.h>
Public Types | |
using | StateTopBaseT = StateTopBase<ContextT,StateTopT> |
A type alias simplifying access to the top state base type. | |
Public Member Functions | |
StateMachine (ContextT *ctx) | |
Constructor. | |
StateMachine (const StateMachine &)=delete | |
Disallow copy construction. | |
StateMachine & | operator= (const StateMachine &)=delete |
Disallow copy assignment. | |
~StateMachine (void) | |
Destructor. | |
void | start (void) |
Start the state machine. | |
ContextT & | ctx (void) |
Get the context object. | |
template<class NewStateT > | |
void | setState (NewStateT *state=new NewStateT) |
Switch to the given state. | |
template<class T > | |
bool | isActive (void) const |
Check if the given state is the active one. | |
StateTopT & | state (void) |
Get the active state. | |
void | setTimeout (int timeout_ms) |
Set a timeout after which the timeoutEvent is issued. | |
void | setTimeoutAt (struct tm &tm, int expire_offset=0) |
Set a timeout after which the timeoutAtEvent is issued. | |
void | clearTimeout (void) |
Clear a pending timeout. | |
void | clearTimeoutAt (void) |
Clear a pending absolute time timeout. | |
Implements a hierarchial state machine.
ContextT | State machine context |
StateTopT | The top state class |
A class that implements a Hierarchial Finite State Machine.
struct Context { Variables and functions used within the state machine }; struct StateTop : Async::StateTopBase<Context, StateTop>::Type { static constexpr auto NAME = "Top"; Event functions implemented by states that handle them virtual void eventA(void) {} virtual void eventB(void) {} }; struct StateMyStateA : Async::StateBase<StateTop, StateMyStateA> { static constexpr auto NAME = "MyStateA"; Event handler functions virtual void eventA(void) override {} }; struct StateMyStateB : Async::StateBase<StateTop, StateMyStateB> { static constexpr auto NAME = "MyStateB"; Event handler functions virtual void eventB(void) override {} }; Context ctx; Async::StateMachine<Context, StateTop> sm(&ctx); sm.start()
The NAME constant is only needed for state transition debugging. To enable debugging, define ASYNC_STATE_MACHINE_DEBUG before including this file.
Full example below.
Definition at line 156 of file AsyncStateMachine.h.
using Async::StateMachine< ContextT, StateTopT >::StateTopBaseT = StateTopBase<ContextT,StateTopT> |
A type alias simplifying access to the top state base type.
Definition at line 162 of file AsyncStateMachine.h.
|
inline |
Constructor.
ctx | The context object |
Definition at line 168 of file AsyncStateMachine.h.
References Async::StateMachine< ContextT, StateTopT >::clearTimeout(), Async::StateMachine< ContextT, StateTopT >::clearTimeoutAt(), Async::AtTimer::expired, and Async::Timer::expired.
|
delete |
Disallow copy construction.
|
inline |
Destructor.
Definition at line 199 of file AsyncStateMachine.h.
|
inline |
Clear a pending timeout.
Use this function to immediately cancel a running timeout timer. See setTimeout for more information.
Definition at line 332 of file AsyncStateMachine.h.
References Async::Timer::setEnable().
Referenced by Async::StateTopBase< ContextT, TopStateT >::clearTimeout(), and Async::StateMachine< ContextT, StateTopT >::StateMachine().
|
inline |
Clear a pending absolute time timeout.
Use this function to immediately cancel a running absolute time timeout timer. See setTimeoutAt for more information.
Definition at line 346 of file AsyncStateMachine.h.
References Async::AtTimer::stop().
Referenced by Async::StateTopBase< ContextT, TopStateT >::clearTimeoutAt(), and Async::StateMachine< ContextT, StateTopT >::StateMachine().
|
inline |
Get the context object.
Definition at line 221 of file AsyncStateMachine.h.
Referenced by Async::StateTopBase< ContextT, TopStateT >::ctx().
|
inline |
Check if the given state is the active one.
Use this function to check if the given state is the active one. The state to check is given as a template argument to the function. E.g.
if (isActive<NextState>()) {}
Definition at line 281 of file AsyncStateMachine.h.
|
delete |
Disallow copy assignment.
|
inline |
Switch to the given state.
A | state object to switch to |
Use this function to switch to the given state. The state to switch to is best given as a template argument to the function. E.g.
NOTE: The state machine implementation cannot handle state switching loops right now. It's ok to set the same state as the current one since it will just be ignored. Switching to the current state via other states init functions, will cause an infinite loop.
Definition at line 241 of file AsyncStateMachine.h.
References Async::StateMachine< ContextT, StateTopT >::state().
Referenced by Async::StateTopBase< ContextT, TopStateT >::setState(), and Async::StateMachine< ContextT, StateTopT >::start().
|
inline |
Set a timeout after which the timeoutEvent is issued.
timeout_ms | The timeout value in milliseconds |
Use this function to set a timeout to occur after the specified number of milliseconds. The timeoutEvent will be issued after the time has expired.
Definition at line 300 of file AsyncStateMachine.h.
References Async::Timer::setEnable(), and Async::Timer::setTimeout().
Referenced by Async::StateTopBase< ContextT, TopStateT >::setTimeout().
|
inline |
Set a timeout after which the timeoutAtEvent is issued.
tm | The absolute time when the timeout should occur |
expire_offset | A millisecond offset for the timer expiration |
Use this function to set a timeout to occur at the specified absolute time, plus or minus the offset value. The time is specified in local time. The timeoutAtEvent will be issued after the time has expired.
Definition at line 319 of file AsyncStateMachine.h.
References Async::AtTimer::setExpireOffset(), Async::AtTimer::setTimeout(), and Async::AtTimer::start().
Referenced by Async::StateTopBase< ContextT, TopStateT >::setTimeoutAt().
|
inline |
Start the state machine.
This function must be called after constructing the state machine. The top state will be initialized and entered. Do not call any other functions in this class until this function has been called.
Definition at line 212 of file AsyncStateMachine.h.
References Async::StateMachine< ContextT, StateTopT >::setState().
|
inline |
Get the active state.
Definition at line 290 of file AsyncStateMachine.h.
Referenced by Async::StateMachine< ContextT, StateTopT >::setState().