erikzenker / Hsm
Licence: mit
Finite state machine library based on the boost hana meta programming library. It follows the principles of the boost msm and boost sml libraries, but tries to reduce own complex meta programming code to a minimum.
Stars: ✭ 106
Programming Languages
cpp17
186 projects
metaprogramming
66 projects
Projects that are alternatives of or similar to Hsm
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+19981.13%)
Mutual labels: state-machine, finite-state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+129.25%)
Mutual labels: state-machine, finite-state-machine
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-73.58%)
Mutual labels: state-machine, finite-state-machine
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (-57.55%)
Mutual labels: state-machine, finite-state-machine
use-tiny-state-machine
A tiny (~700 bytes) react hook to help you write finite state machines
Stars: ✭ 37 (-65.09%)
Mutual labels: state-machine, finite-state-machine
smacha
SMACHA is a meta-scripting, templating, and code generation engine for rapid prototyping of ROS SMACH state machines.
Stars: ✭ 15 (-85.85%)
Mutual labels: state-machine, finite-state-machine
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-76.42%)
Mutual labels: state-machine, finite-state-machine
go-sm
A finite-state machine library for the Go programming language
Stars: ✭ 14 (-86.79%)
Mutual labels: state-machine, finite-state-machine
Jstate
Advanced state machines in Java.
Stars: ✭ 84 (-20.75%)
Mutual labels: state-machine, finite-state-machine
SimpleStateMachineLibrary
📚 A simple library for realization state machines in C# code
Stars: ✭ 30 (-71.7%)
Mutual labels: state-machine, finite-state-machine
Xstateful
A wrapper for xstate that stores state, handles transitions, emits events for state changes and actions/activities, and includes an optional reducer framework for updating state and invoking side-effects
Stars: ✭ 81 (-23.58%)
Mutual labels: state-machine, finite-state-machine
Fsm As Promised
A finite state machine library using ES6 promises
Stars: ✭ 446 (+320.75%)
Mutual labels: state-machine, finite-state-machine
tsm
A Hierarchical State Machine Framework in C++
Stars: ✭ 30 (-71.7%)
Mutual labels: state-machine, finite-state-machine
Hal
🔴 A non-deterministic finite-state machine for Android & JVM that won't let you down
Stars: ✭ 63 (-40.57%)
Mutual labels: state-machine, finite-state-machine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (-76.42%)
Mutual labels: state-machine, finite-state-machine
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-81.13%)
Mutual labels: state-machine, finite-state-machine
StateBuilder
State machine code generator for C++ and Java.
Stars: ✭ 30 (-71.7%)
Mutual labels: state-machine, finite-state-machine
flviz
FLVIz - Finite Automata Simulator written in QT/Graphviz
Stars: ✭ 36 (-66.04%)
Mutual labels: state-machine, finite-state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (-55.66%)
Mutual labels: state-machine, finite-state-machine
Statecharts.github.io
There is no state but what we make. Feel free to pitch in.
Stars: ✭ 265 (+150%)
Mutual labels: state-machine, finite-state-machine
Hana State Machine (HSM)
The hana state machine (hsm) is a finite state machine library based on the boost hana meta programming library. It follows the principles of the boost msm and boost sml libraries, but tries to reduce own complex meta programming code to a minimum.
The following table compares features among popular c++ state machine libraries. A click on a particular feature check mark will forward to the feature documentation.
Feature | Hsm | Sml | Msm | Statechart |
---|---|---|---|---|
External transition | ✓ | ✓ | ✓ | ✓ |
Anonymous transition (Completion) | ✓ | ✓ | ✓ | ✗ |
Internal transition | ✓ | ✓ | ✓ | ✓ |
Direct transition | ✓ | ✗ | ✓ | ✗ |
Guards / actions | ✓ | ✓ | ✓ | ✓ |
Entry / exit actions | ✓ | ✓ | ✓ | ✓ |
Orthogonal regions | ✓ | ✓ | ✓ | ✓ |
Hierachies / sub state machines | ✓ | ✓ | ✓ | ✓ |
Event defering | ✓ | ✓ | ✓ | ✓ |
Transition logging | ✓ | ✓ | ✓ | ? |
Initial pseudo state | ✓ | ✓ | ✓ | ✓ |
History pseudo state | ✓ | ✓ | ✓ | ✓ |
eUml postfix frontend | ✓ | ✓ | ✓ | ✗ |
eUml prefix frontend | ✓ | ✓ | ✓ | ✗ |
Entry / exit pseudo state | ✓ | ✗ | ✓ | ✗ |
State data members | ✓ | ✓ | ✓ | ✓ |
Unexpected event / no transition handler | ✓ | ✗ | ✓ | ✗ |
Dependency injection | ✓ | ✓ | ✗ | ✗ |
Single amalgamation header | ✓ | ✓ | ✗ | ✗ |
Custom target state construction | ✓ | ✗ | ✗ | ✗ |
Chain actions | ✓ | ✓ | ✓ | ? |
Run)
Example (#include "hsm/hsm.h"
#include <iostream>
#include <cassert>
// States
struct Locked {
};
struct Unlocked {
};
// Events
struct Push {
};
struct Coin {
};
// Guards
const auto noError = [](auto /*event*/, auto /*source*/, auto /*target*/) { return true; };
// Actions
constexpr auto beep
= [](auto /*event*/, auto /*source*/, auto /*target*/) { std::cout << "beep!" << std::endl; };
constexpr auto blink = [](auto /*event*/, auto /*source*/, auto /*target*/) {
std::cout << "blink, blink, blink!" << std::endl;
};
struct Turnstile {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
// Source + Event [Guard] / Action = Target
// +-------------------+-----------------+---------+--------+----------------------+
* hsm::state<Locked> + hsm::event<Push> / beep = hsm::state<Locked> ,
hsm::state<Locked> + hsm::event<Coin> [noError] / blink = hsm::state<Unlocked>,
// +--------------------+---------------------+---------+--------+------------------------+
hsm::state<Unlocked> + hsm::event<Push> [noError] = hsm::state<Locked> ,
hsm::state<Unlocked> + hsm::event<Coin> / blink = hsm::state<Unlocked>
// +--------------------+---------------------+---------+--------+------------------------+
);
// clang-format on
}
};
auto main() -> int
{
hsm::sm<Turnstile> turnstileSm;
// The turnstile is initially locked
assert(turnstileSm.is(hsm::state<Locked>));
// Inserting a coin unlocks it
turnstileSm.process_event(Coin {});
assert(turnstileSm.is(hsm::state<Unlocked>));
// Entering the turnstile will lock it again
turnstileSm.process_event(Push {});
assert(turnstileSm.is(hsm::state<Locked>));
return 0;
}
Play with it Online
- Follow the link to the compiler explorer: https://godbolt.org/z/jqPbcj
Runtime Benchmark Results
The benchmark result are taken from the state machine benchmark repository.
Benchmark | Hsm | Sml | Msm | Statechart |
---|---|---|---|---|
Simple state machine | 99 ms | 17 ms | 18 ms | 443 ms |
Complex state machine | 818 ms | 978 ms | 881 ms | 1374 ms |
Compiletime Benchmark Results
Benchmark | Hsm | Sml | Msm | Statechart |
---|---|---|---|---|
Simple state machine | 6.41 s | 0.62 s | 5.17 s | 1.52 s |
Complex state machine | 41.99 s | 3.01 s | 25.54 s | 4.27 s |
Dependencies
- Boost 1.72
- C++17
- >= g++-8
- >= clang-8
- Cmake 3.14
Dev Dependencies
- Gtest
Integration
Usage as Single Header
- Download amalgamation header and put it into your project src folder
- Include amalgamation header:
#include "path/to/amalgamation/header/hsm.h"
CMake
To use this library from a CMake project, you can locate it directly with find_package() and use the namespaced imported target from the generated package configuration:
# CMakeLists.txt
find_package(hsm 1.3.5 REQUIRED)
...
add_library(foo ...)
...
target_link_libraries(foo PRIVATE hsm::hsm)
Install
CMake
cmake -S . -B build
cmake --install build/ --prefix /tmp/
Conan/Cmake
mkdir -p build/dependencies/conan
conan install . -if build/dependencies/conan -s compiler.libcxx=libstdc++11 --build missing
cmake -S . -B build
cmake --install build/ --prefix /tmp/ -D "CMAKE_MODULE_PATH=${PWD}/build/dependencies/conan"
conan remote add conan-erikzenker https://api.bintray.com/conan/erikzenker/conan-erikzenker
conan install hsm/[email protected]/testing --build missing
Install from Arch Linux AUR
pacaur -S hsm-git
Compile and Run the Tests Using the Installed Library
cmake -S test -B build
cmake --build build/test
cd build/test
ctest --output-on-failure
Author
- erikzenker(at)hotmail.com
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].