Your scalable C++14 header State Machine Library with no dependencies (Try it online!)
Let's release a TCP connection!
// $CXX -std=c++14 -O2 -fno-exceptions -Wall -Wextra -Werror -pedantic -pedantic-errors tcp_release.cpp
// cl /std:c++14 /Ox /W3 tcp_release.cpp (***)
#include <cassert>
#include <boost/sml.hpp>
namespace sml = boost::sml;
//dependencies
struct sender {
template<class T>
void send(const T&) {}
};
// events
struct ack { bool valid = true; };
struct fin { bool valid = true;};
struct release {};
struct timeout {};
// guards
const auto is_valid = [](const auto& event) { return event.valid; };
// actions
const auto send_fin = [](sender& s) { s.send(fin{}); };
const auto send_ack = [](const auto& event, sender& s) { s.send(event); };
struct tcp_release {
auto operator()() const {
using namespace sml;
/**
* Initial state: *initial_state
* Transition DSL: src_state + event [ guard ] / action = dst_state
*/
return make_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> / send_ack = X
);
}
};
int main() {
using namespace sml;
sender s;
sm<tcp_release> sm{s}; // pass dependencies via ctor...
assert(sm.is("established"_s));
sm.process_event(release{}); // complexity O(1) -> jump table
assert(sm.is("fin wait 1"_s));
sm.process_event(ack{});
assert(sm.is("fin wait 2"_s));
sm.process_event(fin{});
assert(sm.is("timed wait"_s));
sm.process_event(timeout{});
assert(sm.is(X)); // released
}
(***) MSVC-2015 (Example)
- use
state<class state_name>
instead of"state_name"_s
- expliclty state a lambda's result type
auto action = [] -> void {}
<td rowspan="4">
<a href="http://boost-experimental.github.io/sml/benchmarks/index.html#benchmarks">More Benchmarks</a>
</td>
Clang-3.8 | GCC-6 | MSVC-2015 | |
---|---|---|---|
Compilation Time | 0.102s | 0.118s | 0.296s |
Binary size (stripped) | 6.2kb | 6.2kb | 105kb |
ASM x86-64 |
|
- Introduction
- Overview
- Features/Benchmarks
- Tutorial
- UML vs SML
- User Guide
- Examples
- FAQ
- CHANGELOG
- TODO
Disclaimer Boost.SML
is not an official Boost library.