Giter Site home page Giter Site logo

sml's Introduction

Boost Licence Version Build Status Build Status Coveralls Github Issues


[Boost].SML (formerly called [Boost].MSM-lite)

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


Let's release a TCP connection!

tcp release

// $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 {}

Benchmark

<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

process_event:
	movb	$1, (%r8) // current state = 1
	movl	$1, %eax  // handled
	ret

main: leaq 14(%rsp), %r8 leaq 15(%rsp), %rdi movb $0, 14(%rsp) movq %r8, %rcx movq %r8, %rdx movq %r8, %rsi call *process_event // jump table ...


Documentation


Disclaimer Boost.SML is not an official Boost library.

sml's People

Contributors

denizthatmenace avatar feltech avatar justwillim avatar kris-jusiak avatar krzysztof-jusiak avatar redboltz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.