Zth (libzth)
fsm (C++14)

A fiber-aware FSM implementation, with concise eDSL and constexpr compatible. More...

Classes

struct  zth::fsm::invalid_fsm
 Exception thrown when the FSM description is incorrect. More...
 
class  zth::fsm::Symbol
 A input/state symbol. More...
 
class  zth::fsm::Fsm
 FSM base class. More...
 

Typedefs

using zth::fsm::State = Symbol
 A state within the FSM. More...
 

Functions

constexpr Symbol zth::fsm::operator""_S (char const *s, size_t len) noexcept
 Literal suffix to convert a string literal to zth::fsm::Symbol. More...
 
template<typename T >
constexpr auto zth::fsm::guard (T &&g, char const *name=nullptr)
 Create a guard from a function. More...
 
constexpr auto zth::fsm::guard (Symbol &&input) noexcept
 Create a guard from a input symbol. More...
 
template<typename T >
constexpr auto zth::fsm::action (T &&a, char const *name=nullptr)
 Create an action from a function. More...
 
constexpr auto zth::fsm::input (Symbol &&symbol) noexcept
 Create a guard from an input symbol. More...
 
template<typename... T>
constexpr auto zth::fsm::compile (T &&... t)
 Compile a transition description. More...
 

Variables

constexpr auto zth::fsm::always
 Trivial guard that is always enabled. More...
 
constexpr auto zth::fsm::never
 Trivial guard that is never enabled. More...
 
constexpr auto zth::fsm::nothing
 Trivial action that does nothing. More...
 
constexpr auto zth::fsm::entry
 Guard that is only enabled upon entry of a state. More...
 
constexpr auto zth::fsm::push
 Action to push the new state onto the stack. More...
 
constexpr auto zth::fsm::pop
 Action to pop the current state from the stack. More...
 
constexpr auto zth::fsm::popped
 Guard to indicate that the current state was reached via pop. More...
 
constexpr auto zth::fsm::stop
 Action to return from Fsm::run(). More...
 
constexpr auto zth::fsm::consume
 Action consume the current input symbol. More...
 
template<time_t s>
constexpr auto zth::fsm::timeout_s
 A guard that is enabled after a s seconds after entering the current state. More...
 
template<uint64_t ms>
constexpr auto zth::fsm::timeout_ms
 A guard that is enabled after a ms milliseconds after entering the current state. More...
 
template<uint64_t us>
constexpr auto zth::fsm::timeout_us
 A guard that is enabled after a us microseconds after entering the current state. More...
 

Detailed Description

A fiber-aware FSM implementation, with concise eDSL and constexpr compatible.

See also
fsm14.cpp

Typedef Documentation

◆ State

using zth::fsm::State = typedef Symbol

A state within the FSM.

Definition at line 276 of file fsm14.h.

Function Documentation

◆ action()

template<typename T >
constexpr auto zth::fsm::action ( T &&  a,
char const *  name = nullptr 
)
constexpr

Create an action from a function.

The supported types are:

  • function pointer: void (*)(A)
  • member function pointer: void (C::*)()
  • const member function pointer void (C::*)() const
  • lambda (C++17 when used in a constexpr): [...](A) {...}

The argument A may be omitted. If provided, it must be zth::fsm::BasicFsm&, or a reference to the actual subclass type of zth::fsm::BasicFsm.

The class type C must be zth::fsm::BasicFsm, or the type of the subclass of zth::fsm::BasicFsm. The member function does not have to be static.

For C++17 and higher, these function can also be noexcept.

Returns
an Action object, to be used in the FSM transitions description.
Examples
fsm14.cpp, and measure.cpp.

Definition at line 649 of file fsm14.h.

◆ compile()

template<typename... T>
constexpr auto zth::fsm::compile ( T &&...  t)
constexpr

Compile a transition description.

Examples
fsm14.cpp, and measure.cpp.

Definition at line 1273 of file fsm14.h.

◆ guard() [1/2]

constexpr auto zth::fsm::guard ( Symbol &&  input)
inlineconstexprnoexcept

Create a guard from a input symbol.

Using input("symbol") is usually better than guard("symbol"). However, you can create a constexpr guard using this function, while input() only returns a temporary object.

Returns
a Guard object, to be used in the FSM transitions description.
See also
input(Symbol&&)

Definition at line 546 of file fsm14.h.

◆ guard() [2/2]

template<typename T >
constexpr auto zth::fsm::guard ( T &&  g,
char const *  name = nullptr 
)
constexpr

Create a guard from a function.

The supported types are:

  • function pointer: R (*)(A)
  • member function pointer: R (C::*)()
  • const member function pointer R (C::*)() const
  • lambda (C++17 when used in a constexpr): [...](A) -> R {...}

The return type R must be bool or zth::TimeInterval. The TimeInterval indicates the time until the guard may become enabled and should be checked again. When the interval is 0 or negative, the guard is supposed to be enabled.

In case the return type is bool, true is equivalent to a time interval of 0. false is equivalent to an infinite time interval.

The argument A may be omitted. If provided, it must be zth::fsm::BasicFsm&, or a reference to the actual subclass type of zth::fsm::BasicFsm.

The class type C must be zth::fsm::BasicFsm, or the type of the subclass of zth::fsm::BasicFsm. The member function does not have to be static.

For C++17 and higher, these function can also be noexcept.

Returns
a Guard object, to be used in the FSM transitions description.
Examples
fsm14.cpp, and measure.cpp.

Definition at line 530 of file fsm14.h.

◆ input()

constexpr auto zth::fsm::input ( Symbol &&  symbol)
inlineconstexprnoexcept

Create a guard from an input symbol.

This function returns a temporary, while guard("symbol") returns an object that can be constexpr.

See also
guard(Symbol&&)

Definition at line 781 of file fsm14.h.

◆ operator""_S()

constexpr Symbol zth::fsm::operator""_S ( char const *  s,
size_t  len 
)
constexprnoexcept

Literal suffix to convert a string literal to zth::fsm::Symbol.

Definition at line 267 of file fsm14.h.

Variable Documentation

◆ always

constexpr auto zth::fsm::always
inlineconstexpr

Trivial guard that is always enabled.

Examples
fsm14.cpp.

Definition at line 564 of file fsm14.h.

◆ consume

constexpr auto zth::fsm::consume
inlineconstexpr

Action consume the current input symbol.

Usually, combined with the input guard. When a transition is taken because of an input symbol, remove that symbol from the list of inputs symbol. If consume is not used, the symbol guard will be enabled again the next evaluation of the guards.

Examples
fsm14.cpp.

Definition at line 2050 of file fsm14.h.

◆ entry

constexpr auto zth::fsm::entry
inlineconstexpr

Guard that is only enabled upon entry of a state.

Examples
fsm14.cpp.

Definition at line 2008 of file fsm14.h.

◆ never

constexpr auto zth::fsm::never
inlineconstexpr

Trivial guard that is never enabled.

Definition at line 576 of file fsm14.h.

◆ nothing

constexpr auto zth::fsm::nothing
inlineconstexpr

Trivial action that does nothing.

Definition at line 661 of file fsm14.h.

◆ pop

constexpr auto zth::fsm::pop
inlineconstexpr

Action to pop the current state from the stack.

Definition at line 2023 of file fsm14.h.

◆ popped

constexpr auto zth::fsm::popped
inlineconstexpr

Guard to indicate that the current state was reached via pop.

Definition at line 2030 of file fsm14.h.

◆ push

constexpr auto zth::fsm::push
inlineconstexpr

Action to push the new state onto the stack.

See also
pop

Definition at line 2016 of file fsm14.h.

◆ stop

constexpr auto zth::fsm::stop
inlineconstexpr

Action to return from Fsm::run().

Examples
measure.cpp, and zmq.cpp.

Definition at line 2037 of file fsm14.h.

◆ timeout_ms

template<uint64_t ms>
constexpr auto zth::fsm::timeout_ms
inlineconstexpr

A guard that is enabled after a ms milliseconds after entering the current state.

Definition at line 2069 of file fsm14.h.

◆ timeout_s

template<time_t s>
constexpr auto zth::fsm::timeout_s
inlineconstexpr

A guard that is enabled after a s seconds after entering the current state.

Definition at line 2060 of file fsm14.h.

◆ timeout_us

template<uint64_t us>
constexpr auto zth::fsm::timeout_us
inlineconstexpr

A guard that is enabled after a us microseconds after entering the current state.

Definition at line 2078 of file fsm14.h.