Async 1.8.0
Async::StateBase< ParentT, T > Class Template Reference

Implements a hierarchial state machine. More...

#include <AsyncStateMachine.h>

Inheritance diagram for Async::StateBase< ParentT, T >:

Public Member Functions

virtual const std::type_info & typeId (void) const override
 Get the typeid for this state.
 
virtual const char * name (void) const
 

Protected Member Functions

virtual void initHandler (void) override
 Handle calling the init function on state transitions.
 
virtual void entryHandler (typename ParentT::StateT *from) override
 Handle calling the entry function on state transitions.
 
virtual void exitHandler (typename ParentT::StateT *to) override
 Handle calling the exit function on state transitions.
 
void init (void)
 Called before a transition from one state to another.
 
void entry (void)
 Called when a state is entered.
 
void exit (void)
 
virtual void timeoutEvent (void) override
 An event function that will be called when a timeout occurs.
 
virtual void timeoutAtEvent (void) override
 

Detailed Description

template<class ParentT, class T>
class Async::StateBase< ParentT, T >

Implements a hierarchial state machine.

Author
Tobias Blomberg / SM0SVX
Date
2022-03-19
Parameters
ParentTThe parent state
TThe state class itself

This class should be used together with Async::StateMachine to form a state in the state machine. All states must inherit from this class.

struct StateMyState : Async::StateBase<StateTop, StateMyState> { static constexpr auto NAME = "MyState"; };

The NAME constant is only needed for state transition debugging.

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 381 of file AsyncStateMachine.h.

Member Function Documentation

◆ entry()

template<class ParentT , class T >
void Async::StateBase< ParentT, T >::entry ( void )
inlineprotected

Called when a state is entered.

The entry function is called when a state is entered. It is not allowed to initiate any state transitions in this function. That should be done in the init function or in event handlers.

The entry function will be called, from top to bottom, for all states in the hierarchy. States that are common to the source and the target states will not have the entry function called.

Examples
AsyncStateMachine_demo.cpp.

Definition at line 453 of file AsyncStateMachine.h.

Referenced by Async::StateBase< ParentT, T >::entryHandler().

◆ entryHandler()

template<class ParentT , class T >
virtual void Async::StateBase< ParentT, T >::entryHandler ( typename ParentT::StateT * from)
inlineoverrideprotectedvirtual

Handle calling the entry function on state transitions.

Parameters
fromThe state that we transition from

Definition at line 408 of file AsyncStateMachine.h.

References Async::StateBase< ParentT, T >::entry().

◆ exit()

template<class ParentT , class T >
void Async::StateBase< ParentT, T >::exit ( void )
inlineprotected

@bief Called when a state is exited

The exit function is called when a state is exited. It is not allowed to initiate any state transitions in this function. That should be done in the init function or in event handlers.

The exit function will be called, from bottom to top, for all states in the hierarchy. States that are common to the source and the target states will not have the exit function called.

Examples
AsyncStateMachine_demo.cpp.

Definition at line 466 of file AsyncStateMachine.h.

Referenced by Async::StateBase< ParentT, T >::exitHandler().

◆ exitHandler()

template<class ParentT , class T >
virtual void Async::StateBase< ParentT, T >::exitHandler ( typename ParentT::StateT * to)
inlineoverrideprotectedvirtual

Handle calling the exit function on state transitions.

Parameters
toThe state that we transition to

Definition at line 421 of file AsyncStateMachine.h.

References Async::StateBase< ParentT, T >::exit().

◆ init()

template<class ParentT , class T >
void Async::StateBase< ParentT, T >::init ( void )
inlineprotected

Called before a transition from one state to another.

The init function is called before a state transition is executed so this is the place to switch to another state using the setState function. That is typically used to select a substate to activate when a state is activated.

The init function will only be called in the specific target state.

Examples
AsyncStateMachine_demo.cpp.

Definition at line 440 of file AsyncStateMachine.h.

Referenced by Async::StateBase< ParentT, T >::initHandler().

◆ initHandler()

template<class ParentT , class T >
virtual void Async::StateBase< ParentT, T >::initHandler ( void )
inlineoverrideprotectedvirtual

Handle calling the init function on state transitions.

Definition at line 399 of file AsyncStateMachine.h.

References Async::StateBase< ParentT, T >::init().

◆ name()

template<class ParentT , class T >
virtual const char * Async::StateBase< ParentT, T >::name ( void ) const
inlinevirtual

Definition at line 393 of file AsyncStateMachine.h.

◆ timeoutAtEvent()

template<class ParentT , class T >
virtual void Async::StateBase< ParentT, T >::timeoutAtEvent ( void )
inlineoverrideprotectedvirtual

Definition at line 484 of file AsyncStateMachine.h.

◆ timeoutEvent()

template<class ParentT , class T >
virtual void Async::StateBase< ParentT, T >::timeoutEvent ( void )
inlineoverrideprotectedvirtual

An event function that will be called when a timeout occurs.

This event function will be called when a timeout, previously set up using the setTimeout function, has occurred.

As all event functions this is a virtual function which work like any other virtual function in C++. The state which is furtherest down in the hierarchy, which have the timeoutEvent function implemented, will have the function called.

Definition at line 479 of file AsyncStateMachine.h.

◆ typeId()

template<class ParentT , class T >
virtual const std::type_info & Async::StateBase< ParentT, T >::typeId ( void ) const
inlineoverridevirtual

Get the typeid for this state.

Returns
Returns the typeid for this state

Definition at line 388 of file AsyncStateMachine.h.


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