Skip to content

Repository files navigation

Boost LicenceVersionBuild StatusBuild StatusCodecovGithub Issues


[Boost].SML (State Machine Language)

Your scalable C++14 one header only State Machine Library with no dependencies (Try it online!)


Let's release a TCP connection!

tcp release

Quick start

Download

[Boost].SML requires only one file. Get the latest header here!

Include

#include<boost/sml.hpp>namespacesml= boost::sml;

Dependencies

structsender {
template<classTMsg>
constexprvoidsend(const TMsg& msg) { std::printf("send: %d\n", msg.id); }
};

Events

structack { bool valid{}; };
structfin { int id{}; bool valid{}; };
structrelease {};
structtimeout {};

Guards

constexprauto is_valid = [](constauto& event) { return event.valid; };

Actions

constexprauto send_fin = [](sender& s) { s.send(fin{0}); };
constexprauto send_ack = [](constauto& event, sender& s) { s.send(event); };

State Machine

structtcp_releasefinal {
autooperator()() const {
usingnamespacesml;/** * Initial state: *initial_state * Transition DSL: src_state + event [ guard ] / action = dst_state*/returnmake_transition_table(
*"established"_s + event<release> / send_fin = "fin wait 1"_s,
"fin wait 1"_s + event<ack> [ is_valid ] = "fin wait 2"_s,
"fin wait 2"_s + event<fin> [ is_valid ] / send_ack = "timed wait"_s,
"timed wait"_s + event<timeout> = X
);
}
};

Usage

intmain() {
usingnamespacesml;
sender s{};
sm<tcp_release> sm{s}; // pass dependencies via ctorassert(sm.is("established"_s));
sm.process_event(release{}); // complexity O(1)assert(sm.is("fin wait 1"_s));
sm.process_event(ack{true}); // prints 'send: 0'assert(sm.is("fin wait 2"_s));
sm.process_event(fin{42, true}); // prints 'send: 42'assert(sm.is("timed wait"_s));
sm.process_event(timeout{});
assert(sm.is(X)); // terminated
}

MSVC-2015 (Example)

  • use state<class state_name> instead of "state_name"_s
  • expliclty state a lambda's result type auto action = [] -> void {}

Compile

  • GCC/Clang
    $CXX -std=c++14 -O2 -fno-exceptions -Wall -Wextra -Werror -pedantic -pedantic-errors tcp_release.cpp
  • MSVC
    cl /std:c++14 /Ox /W3 tcp_release.cpp

tcp_release.cppClang-3.8GCC-6.3MSVC-2015
Compilation Time0.102s0.118s0.296s
Binary size (stripped)6.2kb6.2kb105kb
ASM x86-64 -
https://godbolt.org/z/y99L50

main: # @main
pushq %rax
movl $.L.str, %edi
xorl %esi, %esi
xorl %eax, %eax
callq printf
movl $.L.str, %edi
movl $42, %esi
xorl %eax, %eax
callq printf
xorl %eax, %eax
popq %rcx
retq
.L.str:
.asciz "send: %d\n"

Run

Output (https://wandbox.org/permlink/WbvV9HsIyiPkCFw7)

send: 0
send: 42

Benchmark

Complex Test

Enum/SwitchVariant[Boost].SML - 1.1.0Boost-1.65.MSM-eUMLBoost-1.65.Statechart
Compilation time0.132s15.321s0.582s1m15.935s5.671s
Execution time679ms827ms622ms664ms2282ms
Memory usage1b2b/8b1b120b224b
Executable size15K187K34K611K211K

Documentation


DisclaimerBoost.SML is not an official Boost library.

About

[Boost].SML: C++14 State Machine Library

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages