Async 1.8.0
Async::StateTopBase< ContextT, TopStateT > Class Template Referenceabstract

The base class for the top state of a state machine. More...

#include <AsyncStateMachine.h>

Public Types

using Type = StateBase<StateTopBase<ContextT, TopStateT>, TopStateT>
 A type alias to simplify declaration of the top state.
 
using StateT = TopStateT
 A type alias to access the top state type.
 
using StateMachineT = StateMachine<ContextT, StateT>
 A type alias to simplify usage of the state machine type.
 

Public Member Functions

virtual ~StateTopBase (void)
 Destructor.
 
ContextT & ctx (void)
 Get the context object.
 

Protected Member Functions

template<class NewStateT >
void setState (void)
 Transition to the given 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.
 
virtual const std::type_info & typeId (void) const =0
 Get the typeid for this state.
 
virtual void initHandler (void)=0
 Run the init functon in a state.
 
virtual void entryHandler (StateT *from)
 Run all entry handlers in the state hierarchy, top to bottom.
 
virtual void exitHandler (StateT *to)
 Run all exit handlers in the state hierarchy, bottom to top.
 
virtual void timeoutEvent (void)=0
 An event function that will be called when a timeout occurs.
 
virtual void timeoutAtEvent (void)=0
 Event function called when an absolute time timeout occurs.
 

Detailed Description

template<class ContextT, class TopStateT>
class Async::StateTopBase< ContextT, TopStateT >

The base class for the top state of a state machine.

Author
Tobias Blomberg / SM0SVX
Date
2022-03-19

This class should be used together with Async::StateMachine to form the top state of the state machine. The top state must use this class as its parent state. A type alias is available to simplify the syntax. E.g.

struct StateTop : Async::StateTopBase<Context, StateTop>::Type { static constexpr auto NAME = "Top"; };

The NAME constant is only needed for state transition debugging.

Full demo 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 513 of file AsyncStateMachine.h.

Member Typedef Documentation

◆ StateMachineT

template<class ContextT , class TopStateT >
using Async::StateTopBase< ContextT, TopStateT >::StateMachineT = StateMachine<ContextT, StateT>

A type alias to simplify usage of the state machine type.

Definition at line 529 of file AsyncStateMachine.h.

◆ StateT

template<class ContextT , class TopStateT >
using Async::StateTopBase< ContextT, TopStateT >::StateT = TopStateT

A type alias to access the top state type.

Definition at line 524 of file AsyncStateMachine.h.

◆ Type

template<class ContextT , class TopStateT >
using Async::StateTopBase< ContextT, TopStateT >::Type = StateBase<StateTopBase<ContextT, TopStateT>, TopStateT>

A type alias to simplify declaration of the top state.

Definition at line 519 of file AsyncStateMachine.h.

Constructor & Destructor Documentation

◆ ~StateTopBase()

template<class ContextT , class TopStateT >
virtual Async::StateTopBase< ContextT, TopStateT >::~StateTopBase ( void )
inlinevirtual

Destructor.

Definition at line 534 of file AsyncStateMachine.h.

Member Function Documentation

◆ clearTimeout()

template<class ContextT , class TopStateT >
void Async::StateTopBase< ContextT, TopStateT >::clearTimeout ( void )
inlineprotected

Clear a pending timeout.

Use this function to immediately cancel a running timeout timer. See setTimeout for more information.

Definition at line 581 of file AsyncStateMachine.h.

References Async::StateMachine< ContextT, StateTopT >::clearTimeout().

◆ clearTimeoutAt()

template<class ContextT , class TopStateT >
void Async::StateTopBase< ContextT, TopStateT >::clearTimeoutAt ( void )
inlineprotected

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

References Async::StateMachine< ContextT, StateTopT >::clearTimeoutAt().

◆ ctx()

template<class ContextT , class TopStateT >
ContextT & Async::StateTopBase< ContextT, TopStateT >::ctx ( void )
inline

Get the context object.

Returns
Returns a reference to the context object

Definition at line 540 of file AsyncStateMachine.h.

References Async::StateMachine< ContextT, StateTopT >::ctx().

◆ entryHandler()

template<class ContextT , class TopStateT >
virtual void Async::StateTopBase< ContextT, TopStateT >::entryHandler ( StateT * from)
inlineprotectedvirtual

Run all entry handlers in the state hierarchy, top to bottom.

Parameters
fromThe state that we transition from

Definition at line 605 of file AsyncStateMachine.h.

◆ exitHandler()

template<class ContextT , class TopStateT >
virtual void Async::StateTopBase< ContextT, TopStateT >::exitHandler ( StateT * to)
inlineprotectedvirtual

Run all exit handlers in the state hierarchy, bottom to top.

Parameters
toThe state that we transition to

Definition at line 611 of file AsyncStateMachine.h.

◆ initHandler()

template<class ContextT , class TopStateT >
virtual void Async::StateTopBase< ContextT, TopStateT >::initHandler ( void )
protectedpure virtual

Run the init functon in a state.

◆ setState()

template<class ContextT , class TopStateT >
template<class NewStateT >
void Async::StateTopBase< ContextT, TopStateT >::setState ( void )
inlineprotected

Transition to the given state.

See Async::StateMachine::setState.

Examples
AsyncStateMachine_demo.cpp.

Definition at line 549 of file AsyncStateMachine.h.

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

◆ setTimeout()

template<class ContextT , class TopStateT >
void Async::StateTopBase< ContextT, TopStateT >::setTimeout ( int timeout_ms)
inlineprotected

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

References Async::StateMachine< ContextT, StateTopT >::setTimeout().

◆ setTimeoutAt()

template<class ContextT , class TopStateT >
void Async::StateTopBase< ContextT, TopStateT >::setTimeoutAt ( struct tm & tm,
int expire_offset = 0 )
inlineprotected

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

References Async::StateMachine< ContextT, StateTopT >::setTimeoutAt().

◆ timeoutAtEvent()

template<class ContextT , class TopStateT >
virtual void Async::StateTopBase< ContextT, TopStateT >::timeoutAtEvent ( void )
protectedpure virtual

Event function called when an absolute time timeout occurs.

This event function will be called when an absolute time timeout, previously set up using the setTimeoutAt 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 timeoutAtEvent function implemented, will have the function called.

◆ timeoutEvent()

template<class ContextT , class TopStateT >
virtual void Async::StateTopBase< ContextT, TopStateT >::timeoutEvent ( void )
protectedpure virtual

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.

◆ typeId()

template<class ContextT , class TopStateT >
virtual const std::type_info & Async::StateTopBase< ContextT, TopStateT >::typeId ( void ) const
protectedpure virtual

Get the typeid for this state.


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