Back to smallsm

The transition table

A machine is declared as one C++ type, and the compiler rejects invalid tables.

States and events are your own structs, and the table is a template argument.

using network_machine_t = smallsm::machine_def_t<
    smallsm::initial_t<idle_t>,
    smallsm::state_t<connecting_t, smallsm::entry_hook_t, smallsm::exit_hook_t>,
    smallsm::state_t<connected_t>,
    smallsm::state_t<failed_t>,

    smallsm::transition_t<idle_t,       connect_t,    connecting_t,
                          smallsm::guarded_t, smallsm::built_t>,
    smallsm::transition_t<connecting_t, success_t,    connected_t>,
    smallsm::transition_t<connecting_t, fail_t,       failed_t>,
    smallsm::transition_t<connected_t,  disconnect_t, idle_t>,
    smallsm::transition_t<failed_t,     connect_t,    connecting_t>
>;

Validation

Validation runs when machine_t is instantiated. These are compile errors:

  • No initial state, or more than one
  • A state declared twice
  • A transition from or to an unregistered state
  • Two transitions with the same source and event
  • An unreachable state, when require_reachable_t is in the table

C++17 static_assert messages must be string literals, which means a message can't include a type name. Each rule is a class template named after the rule, and that name appears in the compiler output next to the types involved.

dispatch is templated on the event type, which makes dispatching an event that no transition uses a compile error too.

Fire events at the network machine from the transition table. An event with no transition from the current state returns no_transition, and clearing the host makes the guard reject connect_t. This is a JavaScript model of how dispatch behaves in the C++ library.

Current state

Log