Async 1.8.0
Async::StateMachine< ContextT, StateTopT > Class Template Reference

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.
 
StateMachineoperator= (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.
 

Detailed Description

template<class ContextT, class StateTopT>
class Async::StateMachine< ContextT, StateTopT >

Implements a hierarchial state machine.

Author
Tobias Blomberg / SM0SVX
Date
2022-03-19
Parameters
ContextTState machine context
StateTopTThe 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.

#include <iostream>
#define ASYNC_STATE_MACHINE_DEBUG
struct Context
{
int a;
};
struct StateTop;
struct StateDisconnected;
struct StateConnected;
struct StateConnectedA;
struct StateConnectedB;
struct StateTop : Async::StateTopBase<Context, StateTop>::Type
{
static constexpr auto NAME = "Top";
void init(void)
{
std::cout << "### StateTop::init" << std::endl;
}
void entry(void)
{
std::cout << "### StateTop::entry" << std::endl;
}
void exit(void)
{
std::cout << "### StateTop::exit" << std::endl;
}
virtual void eventA(void) {}
virtual void eventB(void) {}
};
struct StateDisconnected : Async::StateBase<StateTop, StateDisconnected>
{
static constexpr auto NAME = "Disconnected";
void init(void)
{
std::cout << "### StateDisconnected::init" << std::endl;
//setState<StateDisconnected>();
}
void entry(void)
{
std::cout << "### StateDisconnected::entry" << std::endl;
}
void exit(void)
{
std::cout << "### StateDisconnected::exit" << std::endl;
}
virtual void eventA(void) override
{
std::cout << "### StateDisconnected::eventA: ctx.a="
<< ctx().a << std::endl;
ctx().a = 24;
setState<StateConnected>();
}
};
struct StateConnected : Async::StateBase<StateTop, StateConnected>
{
static constexpr auto NAME = "Connected";
void init(void)
{
std::cout << "### StateConnected::init" << std::endl;
setState<StateConnectedA>();
//setState<StateDisconnected>();
}
void entry(void)
{
std::cout << "### StateConnected::entry" << std::endl;
}
void exit(void)
{
std::cout << "### StateConnected::exit" << std::endl;
}
virtual void eventB(void) override
{
std::cout << "### StateConnected::eventB: ctx.a="
<< ctx().a << std::endl;
setState<StateConnectedB>();
}
};
struct StateConnectedA : Async::StateBase<StateConnected, StateConnectedA>
{
static constexpr auto NAME = "ConnectedA";
void init(void)
{
std::cout << "### StateConnectedA::init" << std::endl;
}
void entry(void)
{
std::cout << "### StateConnectedA::entry" << std::endl;
}
void exit(void)
{
std::cout << "### StateConnectedA::exit" << std::endl;
}
};
struct StateConnectedB : Async::StateBase<StateConnected, StateConnectedB>
{
static constexpr auto NAME = "ConnectedB";
void entry(void)
{
std::cout << "### StateConnectedB::entry" << std::endl;
}
void exit(void)
{
std::cout << "### StateConnectedB::exit" << std::endl;
}
};
int main()
{
Context ctx;
ctx.a = 42;
sm.start();
sm.state().eventA();
sm.state().eventB();
sm.setState<StateDisconnected>();
return 0;
}
A_brief_description_for_this_file.
Implements a hierarchial state machine.
void entry(void)
Called when a state is entered.
void init(void)
Called before a transition from one state to another.
Implements a hierarchial state machine.
The base class for the top state of a state machine.
void setState(void)
Transition to the given state.
Examples
AsyncStateMachine_demo.cpp.

Definition at line 156 of file AsyncStateMachine.h.

Member Typedef Documentation

◆ StateTopBaseT

template<class ContextT , class StateTopT >
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.

Constructor & Destructor Documentation

◆ StateMachine() [1/2]

template<class ContextT , class StateTopT >
Async::StateMachine< ContextT, StateTopT >::StateMachine ( ContextT * ctx)
inline

◆ StateMachine() [2/2]

template<class ContextT , class StateTopT >
Async::StateMachine< ContextT, StateTopT >::StateMachine ( const StateMachine< ContextT, StateTopT > & )
delete

Disallow copy construction.

◆ ~StateMachine()

template<class ContextT , class StateTopT >
Async::StateMachine< ContextT, StateTopT >::~StateMachine ( void )
inline

Destructor.

Definition at line 199 of file AsyncStateMachine.h.

Member Function Documentation

◆ clearTimeout()

template<class ContextT , class StateTopT >
void Async::StateMachine< ContextT, StateTopT >::clearTimeout ( void )
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().

◆ clearTimeoutAt()

template<class ContextT , class StateTopT >
void Async::StateMachine< ContextT, StateTopT >::clearTimeoutAt ( void )
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().

◆ ctx()

template<class ContextT , class StateTopT >
ContextT & Async::StateMachine< ContextT, StateTopT >::ctx ( void )
inline

Get the context object.

Returns
Returns a reference to the context object

Definition at line 221 of file AsyncStateMachine.h.

Referenced by Async::StateTopBase< ContextT, TopStateT >::ctx().

◆ isActive()

template<class ContextT , class StateTopT >
template<class T >
bool Async::StateMachine< ContextT, StateTopT >::isActive ( void ) const
inline

Check if the given state is the active one.

Returns
Returns true 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.

◆ operator=()

template<class ContextT , class StateTopT >
StateMachine & Async::StateMachine< ContextT, StateTopT >::operator= ( const StateMachine< ContextT, StateTopT > & )
delete

Disallow copy assignment.

◆ setState()

template<class ContextT , class StateTopT >
template<class NewStateT >
void Async::StateMachine< ContextT, StateTopT >::setState ( NewStateT * state = new NewStateT)
inline

Switch to the given state.

Parameters
Astate 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.

setState<NextState>();

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().

◆ setTimeout()

template<class ContextT , class StateTopT >
void Async::StateMachine< ContextT, StateTopT >::setTimeout ( int timeout_ms)
inline

Set a timeout after which the timeoutEvent is issued.

Parameters
timeout_msThe 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().

◆ setTimeoutAt()

template<class ContextT , class StateTopT >
void Async::StateMachine< ContextT, StateTopT >::setTimeoutAt ( struct tm & tm,
int expire_offset = 0 )
inline

Set a timeout after which the timeoutAtEvent is issued.

Parameters
tmThe absolute time when the timeout should occur
expire_offsetA 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().

◆ start()

template<class ContextT , class StateTopT >
void Async::StateMachine< ContextT, StateTopT >::start ( void )
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().

◆ state()

template<class ContextT , class StateTopT >
StateTopT & Async::StateMachine< ContextT, StateTopT >::state ( void )
inline

Get the active state.

Returns
Returns a reference to the active state

Definition at line 290 of file AsyncStateMachine.h.

Referenced by Async::StateMachine< ContextT, StateTopT >::setState().


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