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

FSM base class. More...

#include <fsm14.h>

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

Public Types

using base = BasicFsm
 
- Public Types inherited from zth::fsm::BasicFsm
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

 Fsm (cow_string const &name="FSM")
 Ctor. More...
 
 Fsm (Fsm &&f)
 Move ctor. More...
 
Fsmoperator= (Fsm &&f) noexcept
 Move assignment. More...
 
virtual ~Fsm () override=default
 Dtor. More...
 
virtual void reset () noexcept override
 Reset the state machine. More...
 
GuardPollInterval run (Timestamp const &until)
 Run the state machine until the given time stamp. More...
 
GuardPollInterval run (bool returnWhenBlocked=false)
 Run the state machine. More...
 
void trigger () noexcept
 Trigger a currently running FSM to reevaluate the guards immediately. More...
 
void stop () noexcept
 Interrupt a running FSM. More...
 
virtual void input (Symbol i) override
 Register the given input symbol. More...
 
Timestamp const & t () const noexcept
 Returns the time stamp when the current state was entered. More...
 
TimeInterval dt () const
 Returns the time since the current state was entered. More...
 
- Public Member Functions inherited from zth::fsm::BasicFsm
 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...
 
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...
 
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
 

Static Public Member Functions

template<time_t s>
static GuardPollInterval timeoutGuard_s (Fsm &fsm)
 Callback function for the zth::fsm::timeout_s guard. More...
 
template<uint64_t ms>
static GuardPollInterval timeoutGuard_ms (Fsm &fsm)
 Callback function for the zth::fsm::timeout_ms guard. More...
 
template<uint64_t us>
static GuardPollInterval timeoutGuard_us (Fsm &fsm)
 Callback function for the zth::fsm::timeout_us guard. More...
 
- Static Public Member Functions inherited from zth::UniqueID< BasicFsm >
static uint64_t getID () noexcept
 

Protected Member Functions

virtual void enter () override
 Called when the current state was just entered. More...
 
- Protected Member Functions inherited from zth::fsm::BasicFsm
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...
 
- Protected Member Functions inherited from zth::UniqueIDBase
virtual ~UniqueIDBase ()=default
 

Detailed Description

FSM base class.

If you want to hold some state in the FSM, inherit this class. A reference to your inherited class can be passed to the guard and action functions.

Definition at line 1788 of file fsm14.h.

Member Typedef Documentation

◆ base

Definition at line 1791 of file fsm14.h.

Constructor & Destructor Documentation

◆ Fsm() [1/2]

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

Ctor.

Definition at line 1796 of file fsm14.h.

◆ Fsm() [2/2]

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

Move ctor.

Definition at line 1803 of file fsm14.h.

◆ ~Fsm()

virtual zth::fsm::Fsm::~Fsm ( )
overridevirtualdefault

Dtor.

Member Function Documentation

◆ dt()

TimeInterval zth::fsm::Fsm::dt ( ) const
inline

Returns the time since the current state was entered.

See also
t()

Definition at line 1944 of file fsm14.h.

◆ enter()

virtual void zth::fsm::Fsm::enter ( )
inlineoverrideprotectedvirtual

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 from zth::fsm::BasicFsm.

Definition at line 1988 of file fsm14.h.

◆ input()

virtual void zth::fsm::Fsm::input ( Symbol  i)
inlineoverridevirtual

Register the given input symbol.

trigger() is called to trigger immediate reevaluation of the guards.

Reimplemented from zth::fsm::BasicFsm.

Definition at line 1920 of file fsm14.h.

◆ operator=()

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

Move assignment.

Definition at line 1812 of file fsm14.h.

◆ reset()

virtual void zth::fsm::Fsm::reset ( )
inlineoverridevirtualnoexcept

Reset the state machine.

Reimplemented from zth::fsm::BasicFsm.

Definition at line 1823 of file fsm14.h.

◆ run() [1/2]

GuardPollInterval zth::fsm::Fsm::run ( bool  returnWhenBlocked = false)
inline

Run the state machine.

When returnWhenBlocked is true, and there are no enabled guards, the function returns with the time until the next enabled guard. Otherwise, the FSM keeps running until stop() is called.

Definition at line 1868 of file fsm14.h.

◆ run() [2/2]

GuardPollInterval zth::fsm::Fsm::run ( Timestamp const &  until)
inline

Run the state machine until the given time stamp.

When the time stamp is hit, or when stop() is called, the FSM stops execution. The amount of time waiting for the next enabled guard is returned.

Definition at line 1837 of file fsm14.h.

◆ stop()

void zth::fsm::Fsm::stop ( )
inlinenoexcept

Interrupt a running FSM.

This is the callback function for the zth::fsm::stop action. When the FSM is not running (by run()), calling stop() does nothing.

Definition at line 1908 of file fsm14.h.

◆ t()

Timestamp const& zth::fsm::Fsm::t ( ) const
inlinenoexcept

Returns the time stamp when the current state was entered.

This is the time of the last transition. This includes self-loops, but not transitions without a target state.

Definition at line 1935 of file fsm14.h.

◆ timeoutGuard_ms()

template<uint64_t ms>
static GuardPollInterval zth::fsm::Fsm::timeoutGuard_ms ( Fsm fsm)
inlinestatic

Callback function for the zth::fsm::timeout_ms guard.

Definition at line 1962 of file fsm14.h.

◆ timeoutGuard_s()

template<time_t s>
static GuardPollInterval zth::fsm::Fsm::timeoutGuard_s ( Fsm fsm)
inlinestatic

Callback function for the zth::fsm::timeout_s guard.

Definition at line 1953 of file fsm14.h.

◆ timeoutGuard_us()

template<uint64_t us>
static GuardPollInterval zth::fsm::Fsm::timeoutGuard_us ( Fsm fsm)
inlinestatic

Callback function for the zth::fsm::timeout_us guard.

Definition at line 1973 of file fsm14.h.

◆ trigger()

void zth::fsm::Fsm::trigger ( )
inlinenoexcept

Trigger a currently running FSM to reevaluate the guards immediately.

This may be used when any of the guard function may have become enabled, by some event outside of the state machine.

Definition at line 1897 of file fsm14.h.


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