Maki
Loading...
Searching...
No Matches
State Sets

Definition

A state set expresses a constraint that defines whether it contains any given state.

Note
Here, the word "set" is used in its mathematical sense, not in its std::set sense.

A state set can be "any state", "no state at all", or everything in between.

Maki exposes the maki::state_set class template to represent state sets. Internally, it's nothing more than a predicate, i.e. a function taking a maki::state and returning a bool.

To instantiate a maki::state_set, you have to compose maki::state_builder objects with boolean operators, such as:

constexpr auto initializing = maki::state_builder{};
constexpr auto running = maki::state_builder{};
constexpr auto sleeping = maki::state_builder{};
constexpr auto powering_off = maki::state_builder{}
{
std::cout << "Powering off...\n";
})
;
//A state set
constexpr auto not_powering_off = !powering_off;

When to Use State Sets

You can use state sets as source states in transition tables to factorize transitions. In the example below, all the states, except powering_off itself, transition to powering_off whenever the state machine processes a power_button_press event:

constexpr auto transition_table = maki::transition_table{}
//source, target, event
(maki::init, initializing)
(initializing, running, maki::event<end_of_init>)
(running, sleeping, maki::event<sleep_button_press>)
(sleeping, running, maki::event<wake_up_button_press>)
(!powering_off, powering_off, maki::event<power_button_press>) // <-- Here
;