Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

Examples :: Boost Libraries Documentation

Examples

back adapter

This example contains a state machine implementation with an API adapter that adheres to the back::state_machine interface. It is helpful for utilizing backmp11 in a legacy code base.

Heapless state machine

This example demonstrates a state machine configured for heapless execution.

It uses boost::container::static_vector as the event pool container — a fixed-capacity replacement for the default std::deque container. Note that this container type does not support completion events; use a heapless deque (e.g. etl::deque) if those are required.

Logger

This example contains an observer implementation for logging state machine activities. The implementation logs the start of event processing and every processed transition.

Running the example produces the following output:

Starting state machine...
MyStateMachine processed transition "none + starting -> MyState" (handled)
MyStateMachine processing event TransitionEvent
MyStateMachine processed transition "MyState + TransitionEvent [ NotMyGuard ] / MyAction -> MyOtherState" (rejected)
MyStateMachine processed transition "MyState + TransitionEvent [ MyGuard ] / MyAction -> MyOtherState" (handled)
MyStateMachine processing event TransitionEvent
MyStateMachine processed transition "MyOtherState + TransitionEvent -> MyState" (handled)
MyStateMachine processing event InternalTransitionEvent
MyStateMachine processed transition "MyState + InternalTransitionEvent / MyAction" (handled)
MyStateMachine processing event SmInternalTransitionEvent
MyStateMachine processed transition "MyStateMachine + SmInternalTransitionEvent / MyAction" (handled)
Stopping state machine...

Serialization

These examples demonstrate how to implement reflect(…​) methods to serialize a state machine.

A serialization to JSON is particularly useful for inspecting a state machine’s overall state in a human-readable format:

{
    "active_state_ids": [
        1 // On
    ],
    "front_end": {
        "brightness": 75
    },
    "states": {
        "1": {
            "times_pressed": 1
        }
    },
    "stopped": false
}

DimSwitch state machine (used by all examples)

State machine interface

This example demonstrates how to establish a generic state machine interface with the favor_compile_time policy.

Hiding a state machine behind an interface enables full encapsulation of its implementation — useful for improving compilation times, unit testing, and concealing proprietary logic when distributing pre-compiled libraries.