Zth (libzth)
|
A Finite-state machine. More...
#include <fsm.h>
Public Types | |
typedef choose_type< FsmImpl_, Fsm >::type | FsmImpl |
typedef State_ | State |
typedef Input_ | Input |
typedef FsmCompiler< FsmImpl > | Compiler |
typedef FsmDescription< FsmImpl > | Description[] |
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) | |
UniqueID & | operator= (UniqueID const &)=delete |
UniqueID & | operator= (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 |
A Finite-state machine.
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. |
typedef FsmCompiler<FsmImpl> zth::Fsm< State_, Input_, FsmImpl_ >::Compiler |
typedef FsmDescription<FsmImpl> zth::Fsm< State_, Input_, FsmImpl_ >::Description[] |
typedef choose_type<FsmImpl_, Fsm>::type zth::Fsm< State_, Input_, FsmImpl_ >::FsmImpl |
|
protected |
|
protected |
|
protected |
|
inlineexplicit |
|
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_ >.
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
|
inline |
Check if a lock was requested while running in lockstep mode.
true
if the Fsm is to be paused
|
inline |
Check if a step was requested while paused in lockstep mode.
true
if the Fsm may continue
|
inline |
|
inline |
|
inline |
|
inline |
|
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().
|
inline |
Allow the Fsm to proceed one step, if in lockstep mode.
|
inline |
FsmDescription<FsmImpl> const* zth::Fsm< State_, Input_, FsmImpl_ >::m_compiledDescription |
FsmDescription<FsmImpl>* zth::Fsm< State_, Input_, FsmImpl_ >::m_description |