https://github.com/stephanlachnit/serializiblefsm
C++ Final State Machine library for easy network transport
https://github.com/stephanlachnit/serializiblefsm
cpp cpp17 final-state-machine fsm header-only network serialization
Last synced: about 1 year ago
JSON representation
C++ Final State Machine library for easy network transport
- Host: GitHub
- URL: https://github.com/stephanlachnit/serializiblefsm
- Owner: stephanlachnit
- License: mit
- Created: 2022-11-13T10:31:03.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-19T21:15:30.000Z (over 2 years ago)
- Last Synced: 2025-03-25T11:23:09.739Z (over 1 year ago)
- Topics: cpp, cpp17, final-state-machine, fsm, header-only, network, serialization
- Language: C++
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```c++
#include
#include
// 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;
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](examples/) directory.
## Build examples
You can build the examples with [Meson](https://mesonbuild.com/):
```shell
meson setup build -Dexamples=true
meson compile -C build
```
The examples are then located in `build/example`.
## License
Licensed under MIT.