Zth (libzth)
zth::fsm::BasicFsm Class Reference

Basic FSM base class. More...

#include <fsm14.h>

Inheritance diagram for zth::fsm::BasicFsm:
zth::UniqueID< BasicFsm > zth::UniqueIDBase zth::fsm::Fsm

Public Types

enum class  Flag : size_t {
  entry , selfloop , blocked , transition ,
  pushed , popped , stop , input ,
  flags
}
 Flags that indicate properties of the current state. More...
 
using index_type = TransitionsBase::index_type
 

Public Member Functions

 BasicFsm (cow_string const &name="FSM")
 Ctor. More...
 
 BasicFsm (BasicFsm &&f)
 Move ctor. More...
 
BasicFsmoperator= (BasicFsm &&f) noexcept
 Move assignment. More...
 
virtual ~BasicFsm ()=default
 Dtor. More...
 
State const & state () const noexcept
 Return the current state. More...
 
bool valid () const noexcept
 Checks if the FSM was initialized. More...
 
void reserveStack (size_t capacity)
 Reserve memory to push() a amount of states. More...
 
virtual void reset () noexcept
 Reset the state machine. More...
 
State const & prev () const noexcept
 Return the previous state. More...
 
GuardPollInterval step ()
 Try to take one step in the state machine. More...
 
void push ()
 Push the current state onto the state stack. More...
 
virtual void pop ()
 Pop the previous state from the state stack. More...
 
bool popped () const noexcept
 Check if the current state was reached via pop(). More...
 
bool flag (Flag f) const noexcept
 Check if the given flag is set. More...
 
bool entry () noexcept
 Check if the state was just entered. More...
 
Symbol input () const noexcept
 Return the input symbol that triggered the transition to the current state. More...
 
virtual void input (Symbol i)
 Register the given input symbol. More...
 
bool clearInput (Symbol i) noexcept
 Remove the given input symbol. More...
 
void clearInput () noexcept
 Clear the input symbol that triggered the current state. More...
 
void clearInputs () noexcept
 Clear all inputs. More...
 
void reserveInputs (size_t capacity)
 Reserve memory for the given amount of input symbols. More...
 
bool hasInput (Symbol i) const noexcept
 Checks if the given input symbol was registered before. More...
 
- Public Member Functions inherited from zth::UniqueID< BasicFsm >
 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 Member Functions

bool setFlag (Flag f, bool value=true) noexcept
 Set or clear the given flag. More...
 
void clearFlag (Flag f) noexcept
 Set the given flag. More...
 
virtual void leave ()
 Called when the current state is about to be left. More...
 
virtual void enter ()
 Called when the current state was just entered. More...
 
- Protected Member Functions inherited from zth::UniqueIDBase
virtual ~UniqueIDBase ()=default
 

Additional Inherited Members

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

Detailed Description

Basic FSM base class.

This basic class only executes the FSM, without references to Timestamp and fibers. It is suitable to be executed from an interrupt context, where the current time is not available. Just call step() regularly to run the FSM.

Definition at line 1285 of file fsm14.h.

Member Typedef Documentation

◆ index_type

Member Enumeration Documentation

◆ Flag

enum zth::fsm::BasicFsm::Flag : size_t
strong

Flags that indicate properties of the current state.

See also
flag()
Enumerator
entry 

Just entered the current state.

selfloop 

Took transition to the same state.

blocked 

The FSM is blocked, as no guards are enabled.

transition 

A transition is taken.

pushed 

The current state was entered using push().

popped 

The current state was entered using pop().

stop 

stop() was requested. run() will terminate.

input 

The current state was entered via an input symbol.

flags 

Not a flag, just a count of the other flags.

Definition at line 1294 of file fsm14.h.

Constructor & Destructor Documentation

◆ BasicFsm() [1/2]

zth::fsm::BasicFsm::BasicFsm ( cow_string const &  name = "FSM")
inline

Ctor.

Definition at line 1309 of file fsm14.h.

◆ BasicFsm() [2/2]

zth::fsm::BasicFsm::BasicFsm ( BasicFsm &&  f)
inline

Move ctor.

Definition at line 1318 of file fsm14.h.

◆ ~BasicFsm()

virtual zth::fsm::BasicFsm::~BasicFsm ( )
virtualdefault

Dtor.

Member Function Documentation

◆ clearFlag()

void zth::fsm::BasicFsm::clearFlag ( Flag  f)
inlineprotectednoexcept

Set the given flag.

Equivalent to setFlag(f, false).

Definition at line 1690 of file fsm14.h.

◆ clearInput() [1/2]

void zth::fsm::BasicFsm::clearInput ( )
inlinenoexcept

Clear the input symbol that triggered the current state.

This is the callback function for the zth::fsm::consume action.

See also
input()

Definition at line 1633 of file fsm14.h.

◆ clearInput() [2/2]

bool zth::fsm::BasicFsm::clearInput ( Symbol  i)
inlinenoexcept

Remove the given input symbol.

Returns
false if the given symbol was not registered
See also
input(Symbol)

Definition at line 1611 of file fsm14.h.

◆ clearInputs()

void zth::fsm::BasicFsm::clearInputs ( )
inlinenoexcept

Clear all inputs.

Definition at line 1641 of file fsm14.h.

◆ enter()

virtual void zth::fsm::BasicFsm::enter ( )
inlineprotectedvirtual

Called when the current state was just entered.

This function calls the action. When overriding this function in a subclass, usually call this function first before adding custom logic.

Reimplemented in zth::fsm::Fsm.

Definition at line 1710 of file fsm14.h.

◆ entry()

bool zth::fsm::BasicFsm::entry ( )
inlinenoexcept

Check if the state was just entered.

This is the callback function for the zth::fsm::entry() guard.

Definition at line 1569 of file fsm14.h.

◆ flag()

bool zth::fsm::BasicFsm::flag ( Flag  f) const
inlinenoexcept

Check if the given flag is set.

Definition at line 1559 of file fsm14.h.

◆ hasInput()

bool zth::fsm::BasicFsm::hasInput ( Symbol  i) const
inlinenoexcept

Checks if the given input symbol was registered before.

See also
input(Symbol)

Definition at line 1663 of file fsm14.h.

◆ input() [1/2]

Symbol zth::fsm::BasicFsm::input ( ) const
inlinenoexcept

Return the input symbol that triggered the transition to the current state.

Returns the invalid Symbol when that was not the case.

Definition at line 1584 of file fsm14.h.

◆ input() [2/2]

virtual void zth::fsm::BasicFsm::input ( Symbol  i)
inlinevirtual

Register the given input symbol.

Reimplemented in zth::fsm::Fsm.

Definition at line 1598 of file fsm14.h.

◆ leave()

virtual void zth::fsm::BasicFsm::leave ( )
inlineprotectedvirtual

Called when the current state is about to be left.

Definition at line 1698 of file fsm14.h.

◆ operator=()

BasicFsm& zth::fsm::BasicFsm::operator= ( BasicFsm &&  f)
inlinenoexcept

Move assignment.

Definition at line 1327 of file fsm14.h.

◆ pop()

virtual void zth::fsm::BasicFsm::pop ( )
inlinevirtual

Pop the previous state from the state stack.

leave() and enter() are called when appropriate.

This is the callback function for the zth::fsm::pop() action. push() and pop() must come in pairs.

The FSM must be valid().

See also
push()

Definition at line 1513 of file fsm14.h.

◆ popped()

bool zth::fsm::BasicFsm::popped ( ) const
inlinenoexcept

Check if the current state was reached via pop().

This is the callback function for the zth::fsm::popped() guard.

Definition at line 1551 of file fsm14.h.

◆ prev()

State const& zth::fsm::BasicFsm::prev ( ) const
inlinenoexcept

Return the previous state.

Definition at line 1395 of file fsm14.h.

◆ push()

void zth::fsm::BasicFsm::push ( )
inline

Push the current state onto the state stack.

This is the callback function for the zth::fsm::push() action.

The FSM must be valid().

See also
pop()

Definition at line 1492 of file fsm14.h.

◆ reserveInputs()

void zth::fsm::BasicFsm::reserveInputs ( size_t  capacity)
inline

Reserve memory for the given amount of input symbols.

See also
input(Symbol)
Exceptions
std::bad_allocwhen allocation fails

Definition at line 1651 of file fsm14.h.

◆ reserveStack()

void zth::fsm::BasicFsm::reserveStack ( size_t  capacity)
inline

Reserve memory to push() a amount of states.

Exceptions
std::bad_allocwhen allocation fails

Definition at line 1372 of file fsm14.h.

◆ reset()

virtual void zth::fsm::BasicFsm::reset ( )
inlinevirtualnoexcept

Reset the state machine.

Reimplemented in zth::fsm::Fsm.

Definition at line 1383 of file fsm14.h.

◆ setFlag()

bool zth::fsm::BasicFsm::setFlag ( Flag  f,
bool  value = true 
)
inlineprotectednoexcept

Set or clear the given flag.

Definition at line 1679 of file fsm14.h.

◆ state()

State const& zth::fsm::BasicFsm::state ( ) const
inlinenoexcept

Return the current state.

Calling this function is always thread-safe.

Definition at line 1354 of file fsm14.h.

◆ step()

GuardPollInterval zth::fsm::BasicFsm::step ( )
inline

Try to take one step in the state machine.

This basically does:

  • Find the first enabled guard of the current state.
  • If there is none, return the shortest returned time interval until the guard must be checked again.
  • Otherwise:
    • If a target state is specified, call leave(), set the new state, and call enter().
    • If no target state is specified, call enter() for the same state.

Definition at line 1414 of file fsm14.h.

◆ valid()

bool zth::fsm::BasicFsm::valid ( ) const
inlinenoexcept

Checks if the FSM was initialized.

Definition at line 1363 of file fsm14.h.


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