Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

sml

> 设计协作
Open source

C++14 State Machine library

1.4K stars0 likes1 views
WebsiteGitHub

About

C++14 State Machine library


SML (State Machine Language)

Your scalable C++14 one header only State Machine Library with no dependencies

https://www.youtube.com/watch?v=Zb6xcd2as6o



Let's release a TCP connection!

Quick start

Download

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

Include

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

Dependencies

cpp
struct sender {
  template<class TMsg>
  constexpr void send(const TMsg& msg) { std::printf("send: %d\n", msg.id); }
};

Events

cpp
struct ack { bool valid{}; };
struct fin { int id{}; bool valid{}; };
struct release {};
struct timeout {};

Guards

cpp
constexpr auto is_valid = [](const auto& event) { return event.valid; };

Actions

cpp
constexpr auto send_fin = [](sender& s) { s.send(fin{0}); };
constexpr auto send_ack = [](const auto& event, sender& s) { s.send(event); };

State Machine

cpp
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>                      = X
    );
  }
};

Usage

cpp
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)
  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
    bash
    $CXX -std=c++14 -O2 -fno-exceptions -Wall -Wextra -Werror -pedantic tcp_release.cpp
  • MSVC
    bash
    cl /std:c++14 /Ox /W3 tcp_release.cpp

tcp_release.cpp Clang-3.8 GCC-6.3 MSVC-2015
Compilation Time 0.102s 0.118s 0.296s
Binary size (stripped) 6.2kb 6.2kb 105kb
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)

bash
send: 0
send: 42

Benchmark

Complex Test

Enum/Switch Variant [Boost::ext].SML - 1.1.0 Boost-1.65.MSM-eUML Boost-1.65.Statechart
Compilation time 0.132s 15.321s 0.582s 1m15.935s 5.671s
Execution time 679ms 827ms 622ms 664ms 2282ms
Memory usage 1b 2b/8b 1b 120b 224b
Executable size 15K 187K 34K 611K 211K

Examples

  • Arduino

https://godbolt.org/z/Y983h4

https://www.tinkercad.com/things/9epUrFrzKP3


  • AVR

https://godbolt.org/z/qhx8Md


  • Match3

https://github.com/modern-cpp-examples/match3


Documentation

  • Introduction
    • UML State Machine
    • Do I need a State Machine?
    • Real Life examples?
    • Why [Boost].SML?
    • Problems with Boost.MSM - eUML
    • [Boost].SML design goals
    • What 'lite' implies?
    • Supported UML features
    • Additional features
    • Related materials
    • Acknowledgements
  • Overview
    • Quick Start
    • Dependencies
    • Supported/Tested compilers
    • Configuration
    • Exception Safety
    • Thread Safety
    • Design
    • Error messages
  • Features/Benchmarks
    • Features
    • Benchmarks
  • Tutorial/Workshop
    • 0. Read Boost.MSM - eUML documentation
    • 1. Create events and states
    • 2. Create guards and actions
    • 3. Create a transition table
    • 4. Set initial states
    • 5. Create a state machine
    • 6. Process events
    • 8. Handle errors
    • 9. Test it
    • 10. Debug it
  • UML vs SML
    • Unified Modeling Language™ (UML®) Version 2.5
    • Initial Pseudostate
    • Terminate Pseudostate
    • External transition
    • Anonymous transition
  • User Guide
    • transitional [concept]
    • configurable [concept]
    • callable [concept]
    • dispatchable [concept]
    • state [core]
    • event [core]
    • make_transition_table [state machine]
    • sm [state machine]
    • policies [state machine]
    • testing::sm [testing]
    • make_action [utility]
    • make_dispatch_table [utility]
  • Examples
    • Hello World
    • Events
    • States
    • Actions Guards
    • Transitions
    • Defer/Process
    • Orthogonal Regions
    • [Composite](https://boost-ext.github.io/sml/example

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++design-patternsmetaprogrammingstate-machine

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category设计协作
PricingOpen source

> Related tools

F
Figma
协作式界面设计工具
S
sketch-wakatime
Sketch plugin for automatic time tracking and metrics generated from your Sketch usage.
Q
QuickColor
Quickly apply fills from the global or document color palettes to selected elements - 🎨