Giter Site home page Giter Site logo

stephanlachnit / serializiblefsm Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 15 KB

C++ Final State Machine library for easy network transport

License: MIT License

C++ 83.72% Meson 16.28%
cpp cpp17 final-state-machine fsm header-only network serialization

serializiblefsm's Introduction

SerializableFSM

SerializableFSM is a templated header-only final state machine C++20 library designed to be easily serializable for network transport. It achieves this by being templated by two enums: one for the possible states of the FSM, and one for the possible events the FSM may encounter.

Simple example

#include <cassert>
#include <SerializableFSM.hpp>

// Possible switch states
enum class State {
    On,
    Off,
};

// Possible switch events
enum class Event {
    SwitchOn,
    SwitchOff,
};

// Typedefs for prettier code
class Switch;
using BaseFSM = SerializableFSM::FSM<Switch, State, Event>;

class Switch : public BaseFSM {
private:
    friend BaseFSM;

    // Transition to turn switch on
    auto switchOn(std::any /* user_data */) -> State {
        return State::On;
    }

    // Transition to turn switch off
    auto switchOff(std::any /* user_data */) -> State {
        return State::Off;
    }

public:
    explicit Switch(State initial_state) : BaseFSM(this, initial_state, BaseFSM::StateTransitionMap({
        // All transitions from the On state go here
        {State::On, {{Event::SwitchOff, &Switch::switchOff}}},
        // All transitions from the Off state go here
        {State::Off, {{Event::SwitchOn, &Switch::switchOn}}},
    })) {}
};

int main() {
    // Define FSM with starting in Off state
    Switch fsm {State::Off};

    // Turn switch on
    fsm.react(Event::SwitchOn);

    // Switch is now in On state
    assert(fsm.getState() == State::On);

    return 0;
}

For more examples, take a look at the examples directory.

Build examples

You can build the examples with Meson:

meson setup build -Dexamples=true
meson compile -C build

The examples are then located in build/example.

License

Licensed under MIT.

serializiblefsm's People

Contributors

stephanlachnit 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.