All Projects → boost-ext → Sml

boost-ext / Sml

Licence: bsl-1.0
SML: C++14 State Machine Library

Programming Languages

metaprogramming
66 projects

Projects that are alternatives of or similar to Sml

Di
DI: C++14 Dependency Injection Library
Stars: ✭ 756 (+40%)
Mutual labels:  boost, design-patterns
When Ts
When: recombinant design pattern for state machines based on gene expression with a temporal model
Stars: ✭ 112 (-79.26%)
Mutual labels:  state-machine, design-patterns
concurrent-resource
A header-only C++ library that allows easily creating thread-safe, concurrency friendly resources.
Stars: ✭ 17 (-96.85%)
Mutual labels:  boost, design-patterns
Sheets
⭐ ‎‎‎‏‏‎ ‎Offers a range of beautiful sheets (dialogs & bottom sheets) for quick use in your project. Includes many ways to customize sheets.
Stars: ✭ 441 (-18.33%)
Mutual labels:  design-patterns
Tinyfsm
A simple C++ finite state machine library
Stars: ✭ 452 (-16.3%)
Mutual labels:  state-machine
Ut
UT: C++20 μ(micro)/Unit Testing Framework
Stars: ✭ 507 (-6.11%)
Mutual labels:  boost
Javacodeacc
Java代码积累 | 大话设计模式(Java实现版本)、线程协作
Stars: ✭ 537 (-0.56%)
Mutual labels:  design-patterns
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (-17.41%)
Mutual labels:  state-machine
Automatonymous
A state machine library for .Net - 100% code - No doodleware
Stars: ✭ 516 (-4.44%)
Mutual labels:  state-machine
Math
The Stan Math Library is a C++ template library for automatic differentiation of any order using forward, reverse, and mixed modes. It includes a range of built-in functions for probabilistic modeling, linear algebra, and equation solving.
Stars: ✭ 494 (-8.52%)
Mutual labels:  boost
Design Patterns In Kotlin
Design Patterns implemented in Kotlin
Stars: ✭ 5,009 (+827.59%)
Mutual labels:  design-patterns
Aasm
AASM - State machines for Ruby classes (plain Ruby, ActiveRecord, Mongoid, NoBrainer, Dynamoid)
Stars: ✭ 4,474 (+728.52%)
Mutual labels:  state-machine
State Machine Cat
write beautiful state charts 🙀
Stars: ✭ 509 (-5.74%)
Mutual labels:  state-machine
Service Pattern Go
Simple clean Go REST API architecture with dependency injection and mocking example, following SOLID principles.
Stars: ✭ 449 (-16.85%)
Mutual labels:  design-patterns
Rust Design Pattern
rust design patterns
Stars: ✭ 529 (-2.04%)
Mutual labels:  design-patterns
Tobetopjavaer
To Be Top Javaer - Java工程师成神之路
Stars: ✭ 22,056 (+3984.44%)
Mutual labels:  design-patterns
Circlebar
A fun, easy-to-use tab bar navigation controller for iOS.
Stars: ✭ 513 (-5%)
Mutual labels:  design-patterns
Designpatternslibrary
A comprehensive design patterns library implemented in C#, which covers various design patterns from the most commonly used ones to the lesser-known ones. Get familiar with and learn design patterns through moderately realistic examples.
Stars: ✭ 485 (-10.19%)
Mutual labels:  design-patterns
Cloud Design Patterns
Prescriptive Architecture Guidance for Cloud Applications
Stars: ✭ 484 (-10.37%)
Mutual labels:  design-patterns
Cmake Examples
Useful CMake Examples
Stars: ✭ 7,220 (+1237.04%)
Mutual labels:  boost

Boost Licence Version Build Status Build Status Codecov Github Issues Try it online


[Boost::ext].SML (State Machine Language)

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

Rise of the State Machines

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



Let's release a TCP connection!

tcp release

Quick start

Download

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

Include

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

Dependencies

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

Events

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

Guards

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

Actions

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

State Machine

struct tcp_release final {
  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

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

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 UML

Arduino Code

https://godbolt.org/z/Y983h4

Arduino Board

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


AVR performance

https://godbolt.org/z/qhx8Md


match3

match3

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


Documentation


Disclaimer [Boost::ext].SML is not an official Boost library.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].