Finite State Machine implementation using std::variant

Overview

mp::fsm

Implementation of Finite State Machine presented by me on CppCon 2018 in a talk Effective replacement of dynamic polymorphism with std::variant.

It uses std::variant and std::optional under the hood.

How to?

FSM works with any types defining events. State is represented by any type too. All states have to be put into the variant and the initial state should appear the first on the list. Both states and events can be stateful.

#include <mp/fsm.h>

struct event_connect {};
struct event_disconnect {};

struct state_idle {};
struct state_connected {};

using state = std::variant<state_idle, state_connected>;

class sample_fsm : public mp::fsm<sample_fsm, state> {
public:
  template<typename State, typename Event>
  auto on_event(State&, const Event&) { return std::nullopt; }

  auto on_event(state_idle&, const event_connect&) { return state_connected{}; }
  auto on_event(state_connected&, const event_disconnect&) { return state_idle{}; }
};

int main()
{
  sample_fsm fsm;
  fsm.dispatch(event_connect());
  fsm.dispatch(event_disconnect());
}

Building, testing and installation

For detailed information on project compilation, testing and reuse please refer to INSTALL.md.

Acknowledgements

The FSM design was inspired by:

Thank you!

You might also like...
A Simple Stack Machine VM
A Simple Stack Machine VM

Arkam - Stack Machine VM Purposes Build small cli tools Sound/Graphic tinkering environment Status 2021-05-06 start Influenced by uxn Mako Retro Neste

C++ implementation of a fast hash map and hash set using hopscotch hashing

A C++ implementation of a fast hash map and hash set using hopscotch hashing The hopscotch-map library is a C++ implementation of a fast hash map and

C++ implementation of a fast hash map and hash set using robin hood hashing

A C++ implementation of a fast hash map and hash set using robin hood hashing The robin-map library is a C++ implementation of a fast hash map and has

This repository provides implementation of an incremental k-d tree for robotic applications.

ikd-Tree ikd-Tree is an incremental k-d tree designed for robotic applications. The ikd-Tree incrementally updates a k-d tree with new coming points o

A C++20 implementation of safe (wrap around) integers following MISRA C++ rules

PSsimplesafeint A C++20 implementation of safe (wrap around) integers following MISRA C++ rules. I might also work on a C++17 implementation, but this

C++14 header only result monad implementation

constexpr Either S, E C++14 header only result monad implementation. Features constexpr support 0 dependencies single header Status in development T

Typesafe, Generic & Fastest Set Data structure implementation in C
Typesafe, Generic & Fastest Set Data structure implementation in C

Typesafe & Fast as fuck Set in C Key Features Extremely fast non-cryptographic hash algorithm XXHash Complete Typesafe APIs Double Hashing to avoid bo

A fast Python Common substrings of multiple strings library with C++ implementation

A fast Python Common substrings of multiple strings library with C++ implementation Having a bunch of strings, can I print some substrings which appea

Implementation of various data structures and algorithms.
Implementation of various data structures and algorithms.

Data Structures and Algorithms A place where you can find and learn the copious number of algorithms in an efficient manner. This repository covers va

Comments
  • fsm.h is missing include header <utility> for std::move

    fsm.h is missing include header for std::move

    Wow - I absolutely loved your example and the CPPcon18 Powerpoint on FSM's Really - fantastic work. Thanks for opening my eyes on using this to implement a FSM.

    Do you have any ideas about adding a timer event if a state transition does not occur within a period of time ?

    According to www.cplusplus.com/reference/utility/move, the include file: #include

    is missing from the file: fsm-variant/src/include/mp/fsm.h

    opened by bobwilmes 4
  • Request updating code

    Request updating code

    Thank you very much for open sourcing your FSM. Would you mind updating code in github according to your talk in ACCU 2019? fsm.h in ACCU 2019 is more updated than fsm.h in github https://youtu.be/JGYxOieiZnY

    Thank you

    opened by vincent-hui 2
  • FSM was the main pillar for Windows TAPI integration

    FSM was the main pillar for Windows TAPI integration

    Hi, I want to thank you and the original authors for providing this FSM. It eased the work of pairing events and states for the application. It was a pleasure to write with this technique of variants. Thank you! A

    opened by aegonzalez 2
Releases(v1.0.0)
Owner
Mateusz Pusz
Software architect, chief engineer, security champion, and C++ trainer with more than 14 years of experience in designing, writing and maintaining C++ code.
Mateusz Pusz
This project Orchid-Fst implements a fast text string dictionary search data structure: Finite state transducer (short for FST) in c++ language.This FST C++ open source project has much significant advantages.

Orchid-Fst 1. Project Overview This project Orchid-Fst implements a fast text string dictionary search data structure: Finite state transducer , which

Bin Ding 10 Oct 18, 2022
Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union.

Eggs.Variant Introduction Eggs.Variant is a C++11/14/17 generic, type-safe, discriminated union. See the documentation at http://eggs-cpp.github.io/va

null 138 Dec 3, 2022
A realtime/embedded-friendly C++11 variant type which is never empty and prevents undesirable implicit conversions

strict variant Do you use boost::variant or one of the many open-source C++11 implementations of a "tagged union" or variant type in your C++ projects

Chris Beck 90 Oct 10, 2022
🏅State-of-the-art learned data structure that enables fast lookup, predecessor, range searches and updates in arrays of billions of items using orders of magnitude less space than traditional indexes

The Piecewise Geometric Model index (PGM-index) is a data structure that enables fast lookup, predecessor, range searches and updates in arrays of bil

Giorgio Vinciguerra 650 Dec 31, 2022
Dining philosophers problem is a problem created by Edsger Wybe Dijkstra in 1965 to explain the deadlock state of an operating system, which is traditionally commonly introduced in lectures on operating systems

42-philosophers Dining philosophers problem is a problem created by Edsger Wybe Dijkstra in 1965 to explain the deadlock state of an operating system,

Lavrenova Maria 31 Dec 26, 2022
A C++ data container replicating std::queue functionality but with better performance.

A data container replicating std::queue functionality but with better performance than standard library containers in a queue context. C++98/03/11/14/etc-compatible.

Matt Bentley 20 Dec 4, 2022
Benchmarking a trivial replacement for std::vector

std::vector replacement benchmark Dependencies You'll need gnuplot and bash to run ./bench.sh. In addition to that, you'll need to have gcc and clang

Dale Weiler 9 Aug 27, 2022
A drop-in replacement for std::list with 293% faster insertion, 57% faster erasure, 17% faster iteration and 77% faster sorting on average. 20-24% speed increase in use-case testing.

plf::list A drop-in replacement for std::list with (on average): 293% faster insertion 57% faster erasure 17% faster iteration 77% faster sorting 70%

Matt Bentley 117 Nov 8, 2022
A C++ data container replicating std::stack functionality but with better performance than standard library containers in a stack context.

plf::stack A data container replicating std::stack functionality but with better performance than standard library containers in a stack context. C++9

Matt Bentley 47 Sep 11, 2022