Zth (libzth)
zth::Fsm< State_, Input_, FsmImpl_ > Class Template Referenceabstract

A Finite-state machine. More...

#include <fsm.h>

Inheritance diagram for zth::Fsm< State_, Input_, FsmImpl_ >:
zth::UniqueID< Fsm< void, void, void > > zth::UniqueIDBase

Public Types

typedef choose_type< FsmImpl_, Fsm >::type FsmImpl
 
typedef State_ State
 
typedef Input_ Input
 
typedef FsmCompiler< FsmImplCompiler
 
typedef FsmDescription< FsmImplDescription[]
 

Public Member Functions

 Fsm (Compiler const &compiler, char const *name="FSM")
 
 Fsm (Description description, char const *name="FSM")
 
virtual ~Fsm ()
 
State const & state () const
 
Timestamp const & t () const
 
TimeInterval dt () const
 
State const & next () const
 
void reset ()
 
bool entry () const
 
bool exit () const
 
bool lockstep () const
 Check if lockstep mode is enabled. More...
 
void setLockstep (bool enable)
 Enable/disable lockstep mode. More...
 
bool guardLock ()
 Check if a lock was requested while running in lockstep mode. More...
 
bool guardStep ()
 Check if a step was requested while paused in lockstep mode. More...
 
void step ()
 Allow the Fsm to proceed one step, if in lockstep mode. More...
 
void input (Input i)
 
bool clearInput (Input i)
 
void clearInputs ()
 
void setInputsCapacity (size_t capacity)
 
bool hasInput (Input i) const
 
TimeInterval eval (bool alwaysDoCallback=false)
 
void run ()
 
void trigger ()
 
- Public Member Functions inherited from zth::UniqueID< Fsm< void, void, void > >
 UniqueID (UniqueID const &)=delete
 
 UniqueID (UniqueID &&u) noexcept
 
 UniqueID (string const &name)
 
 UniqueID (string &&name)
 
 UniqueID (char const *name=nullptr)
 
UniqueIDoperator= (UniqueID const &)=delete
 
UniqueIDoperator= (UniqueID &&u) noexcept
 
virtual ~UniqueID ()=default
 
void const * normptr () const noexcept
 
uint64_t id () const noexcept
 
string const & name () const noexcept
 
void setName (string const &name)
 
void setName (char const *name)
 
void setName (string &&name)
 
virtual char const * id_str () const override
 

Protected Types

enum  EvalState {
  evalCompile , evalInit , evalReset , evalIdle ,
  evalState , evalRecurse
}
 
enum  Lockstep {
  lockstepNormal , lockstepLock , lockstepStep , lockstepSteppingNext ,
  lockstepStepping
}
 
typedef FsmDescription< FsmImpl > const * StateAddr
 

Protected Member Functions

virtual void callback ()=0
 This function is called when the FSM has to execute the code belonging by the current state. More...
 
- Protected Member Functions inherited from zth::UniqueIDBase
virtual ~UniqueIDBase ()=default
 

Additional Inherited Members

- Static Public Member Functions inherited from zth::UniqueID< Fsm< void, void, void > >
static uint64_t getID () noexcept
 

Detailed Description

template<typename State_, typename Input_ = int, typename FsmImpl_ = void>
class zth::Fsm< State_, Input_, FsmImpl_ >

A Finite-state machine.

Template Parameters
State_the type of a state, which can be anything, as long as it is default-constructable and assignable, and comparable with std:less.
Input_the type of inputs, as used by Fsm::input(), which has the same type requirements as State_.
FsmImpl_the actual subclass type of Fsm, which is used for the zth::FsmDescription. So, if you derive from Fsm, pass your class to FsmImpl_, such that every guard gets your actual Fsm type reference. When void, assume that there is no subclass, and just use the Fsm reference instead.

Definition at line 304 of file fsm.h.

Member Typedef Documentation

◆ Compiler

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
typedef FsmCompiler<FsmImpl> zth::Fsm< State_, Input_, FsmImpl_ >::Compiler

Definition at line 310 of file fsm.h.

◆ Description

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
typedef FsmDescription<FsmImpl> zth::Fsm< State_, Input_, FsmImpl_ >::Description[]

Definition at line 311 of file fsm.h.

◆ FsmImpl

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
typedef choose_type<FsmImpl_, Fsm>::type zth::Fsm< State_, Input_, FsmImpl_ >::FsmImpl

Definition at line 307 of file fsm.h.

◆ Input

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
typedef Input_ zth::Fsm< State_, Input_, FsmImpl_ >::Input

Definition at line 309 of file fsm.h.

◆ State

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
typedef State_ zth::Fsm< State_, Input_, FsmImpl_ >::State

Definition at line 308 of file fsm.h.

◆ StateAddr

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
typedef FsmDescription<FsmImpl> const* zth::Fsm< State_, Input_, FsmImpl_ >::StateAddr
protected

Definition at line 315 of file fsm.h.

Member Enumeration Documentation

◆ EvalState

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
enum zth::Fsm::EvalState
protected
Enumerator
evalCompile 
evalInit 
evalReset 
evalIdle 
evalState 
evalRecurse 

Definition at line 314 of file fsm.h.

◆ Lockstep

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
enum zth::Fsm::Lockstep
protected
Enumerator
lockstepNormal 
lockstepLock 
lockstepStep 
lockstepSteppingNext 
lockstepStepping 

Definition at line 317 of file fsm.h.

Constructor & Destructor Documentation

◆ Fsm() [1/2]

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
zth::Fsm< State_, Input_, FsmImpl_ >::Fsm ( Compiler const &  compiler,
char const *  name = "FSM" 
)
inlineexplicit

Definition at line 326 of file fsm.h.

◆ Fsm() [2/2]

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
zth::Fsm< State_, Input_, FsmImpl_ >::Fsm ( Description  description,
char const *  name = "FSM" 
)
inlineexplicit

Definition at line 337 of file fsm.h.

◆ ~Fsm()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
virtual zth::Fsm< State_, Input_, FsmImpl_ >::~Fsm ( )
inlinevirtual

Definition at line 344 of file fsm.h.

Member Function Documentation

◆ callback()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
virtual void zth::Fsm< State_, Input_, FsmImpl_ >::callback ( )
protectedpure virtual

This function is called when the FSM has to execute the code belonging by the current state.

It is called upon entry and exit of a state, and arbitrary times during a state.

Implemented in zth::FsmCallback< State_, void, Input_, FsmImpl_ >, and zth::FsmCallback< State_, CallbackArg_, Input_, FsmImpl_ >.

◆ clearInput()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::clearInput ( Input  i)
inline

Definition at line 527 of file fsm.h.

◆ clearInputs()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::clearInputs ( )
inline

Definition at line 538 of file fsm.h.

◆ dt()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
TimeInterval zth::Fsm< State_, Input_, FsmImpl_ >::dt ( ) const
inline

Definition at line 365 of file fsm.h.

◆ entry()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::entry ( ) const
inline

Definition at line 405 of file fsm.h.

◆ eval()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
TimeInterval zth::Fsm< State_, Input_, FsmImpl_ >::eval ( bool  alwaysDoCallback = false)
inline

Definition at line 563 of file fsm.h.

◆ exit()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::exit ( ) const
inline

Definition at line 410 of file fsm.h.

◆ guardLock()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::guardLock ( )
inline

Check if a lock was requested while running in lockstep mode.

Returns
true if the Fsm is to be paused
See also
setLockstep()

Definition at line 477 of file fsm.h.

◆ guardStep()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::guardStep ( )
inline

Check if a step was requested while paused in lockstep mode.

Returns
true if the Fsm may continue
See also
setLockstep()

Definition at line 493 of file fsm.h.

◆ hasInput()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::hasInput ( Input  i) const
inline

Definition at line 555 of file fsm.h.

◆ input()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::input ( Input  i)
inline

Definition at line 521 of file fsm.h.

◆ lockstep()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
bool zth::Fsm< State_, Input_, FsmImpl_ >::lockstep ( ) const
inline

Check if lockstep mode is enabled.

Definition at line 418 of file fsm.h.

◆ next()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
State const& zth::Fsm< State_, Input_, FsmImpl_ >::next ( ) const
inline

Definition at line 370 of file fsm.h.

◆ reset()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::reset ( )
inline

Definition at line 385 of file fsm.h.

◆ run()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::run ( )
inline

Definition at line 608 of file fsm.h.

◆ setInputsCapacity()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::setInputsCapacity ( size_t  capacity)
inline

Definition at line 543 of file fsm.h.

◆ setLockstep()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::setLockstep ( bool  enable)
inline

Enable/disable lockstep mode.

Assume the following FSM:

* +--> A ---+
* |         |
* +--- B <--+
* 

Normally, the transitions have guards that determine if the Fsm is allowed to continue. In lockstep mode, one add special states where the Fsm can wait for an explicit input. For example, if such explicit input is required before continuing back to A:

* +--> A ---+
* |         |
* +--- B <--+
*     ^ \
*    /   \
*   +- C <+
* 

In this Fsm, once B is reached, the Fsm may go to the stalled state C, wait for an explicit input, go back to B and proceed to A. To implement this, add a guard that checks guardLock() at entry of B. If it returns true, proceed to C. In C, add a guard that checks guardStep(). If it returns true, proceed back to B. Any guardLock() guards are ignored until state B is left. If the expected explicit input was received, call step().

Definition at line 456 of file fsm.h.

◆ state()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
State const& zth::Fsm< State_, Input_, FsmImpl_ >::state ( ) const
inline

Definition at line 346 of file fsm.h.

◆ step()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::step ( )
inline

Allow the Fsm to proceed one step, if in lockstep mode.

See also
setLockstep()

Definition at line 510 of file fsm.h.

◆ t()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
Timestamp const& zth::Fsm< State_, Input_, FsmImpl_ >::t ( ) const
inline

Definition at line 360 of file fsm.h.

◆ trigger()

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
void zth::Fsm< State_, Input_, FsmImpl_ >::trigger ( )
inline

Definition at line 632 of file fsm.h.

Member Data Documentation

◆ m_compiledDescription

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
FsmDescription<FsmImpl> const* zth::Fsm< State_, Input_, FsmImpl_ >::m_compiledDescription

Definition at line 711 of file fsm.h.

◆ m_compiler

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
Compiler const* zth::Fsm< State_, Input_, FsmImpl_ >::m_compiler

Definition at line 710 of file fsm.h.

◆ m_description

template<typename State_ , typename Input_ = int, typename FsmImpl_ = void>
FsmDescription<FsmImpl>* zth::Fsm< State_, Input_, FsmImpl_ >::m_description

Definition at line 709 of file fsm.h.


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