All Projects → shuvalov-mdb → xstate-cpp-generator

shuvalov-mdb / xstate-cpp-generator

Licence: MIT License
C++ State Machine generator for Xstate

Programming Languages

C++
36643 projects - #6 most used programming language
typescript
32286 projects
python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to xstate-cpp-generator

kstatemachine
KStateMachine is a Kotlin DSL library for creating finite state machines (FSM) and hierarchical state machines (HSM).
Stars: ✭ 63 (+90.91%)
Mutual labels:  state-management, fsm, state-machine, fsm-library
use-state-machine
Use Finite State Machines with React Hooks
Stars: ✭ 28 (-15.15%)
Mutual labels:  state-management, fsm, state-machine
xstate
State machines and statecharts for the modern web.
Stars: ✭ 21,286 (+64403.03%)
Mutual labels:  state-management, fsm, state-machine
xstate-viz
Visualizer for XState machines
Stars: ✭ 274 (+730.3%)
Mutual labels:  state-management, state-machine, xstate
fea state machines
A Buffet Of C++17 State Machines
Stars: ✭ 19 (-42.42%)
Mutual labels:  fsm, state-machine, fsm-library
tstate-machine
TypeScript implementation of State Manager(like StateMachine)
Stars: ✭ 20 (-39.39%)
Mutual labels:  state-management, state-machine
pastafarian
A tiny event-based finite state machine
Stars: ✭ 20 (-39.39%)
Mutual labels:  fsm, state-machine
HFSM
Hierarchical Finite State Machine Framework
Stars: ✭ 73 (+121.21%)
Mutual labels:  fsm, fsm-library
statebot-sh
Statebot for shell-scripts. Write more robust and understandable programs.
Stars: ✭ 14 (-57.58%)
Mutual labels:  state-management, state-machine
xstate.dart
xstate for dart & flutter
Stars: ✭ 31 (-6.06%)
Mutual labels:  state-management, xstate
state-machine-demo
A React state machine demo using xstate
Stars: ✭ 18 (-45.45%)
Mutual labels:  state-machine, xstate
xstate-catalogue
Professionally designed, interactive state machines
Stars: ✭ 616 (+1766.67%)
Mutual labels:  state-machine, xstate
FiniteStateMachine
This project is a finite state machine designed to be used in games.
Stars: ✭ 45 (+36.36%)
Mutual labels:  fsm, state-machine
qp-arduino
QP real-time embedded frameworks/RTOS for Arduino (AVR and SAM)
Stars: ✭ 37 (+12.12%)
Mutual labels:  fsm, state-machine
UnityHFSM
A simple yet powerful class based hierarchical finite state machine for Unity3D
Stars: ✭ 243 (+636.36%)
Mutual labels:  fsm, state-machine
statemachine-go
🚦 Declarative Finite-State Machines in Go
Stars: ✭ 47 (+42.42%)
Mutual labels:  fsm, state-machine
useStateMachine
The <1 kb state machine hook for React
Stars: ✭ 2,231 (+6660.61%)
Mutual labels:  state-management, state-machine
stateless
Finite State Machine porting from Stateless C#
Stars: ✭ 25 (-24.24%)
Mutual labels:  fsm, state-machine
remachine
[WIP] Reason pattern matching viz
Stars: ✭ 44 (+33.33%)
Mutual labels:  fsm, state-machine
simple-state-machine
A simple Java state machine for Spring Boot projects
Stars: ✭ 25 (-24.24%)
Mutual labels:  fsm, state-machine

C++ State Machine generator for Xstate

This package allows to convert TypeScript language State Machine developed using Xstate into C++ generated SM, no coding required.

Tutorials and Documentation

Features

  • Design and test the State Machine in Xstate and then convert to C++ without any changes
  • SM basic features supported: States, Events, Transitions
    • SM extra features supported: Actions
  • Generated C++ is fully synchronized, safe to use in multi-threaded environemnt without any changes
  • No external dependencies except STL. No boost dependency.
  • Callback model:
    • Entry, Exit and Trasition Actions are code generated as static methods in the template object used to declare the State Machine and can be implemented by the user
    • Every state and transtion callbacks are generated as virtual methods that can be overloaded by subclassing
  • Arbitrary user-defined data structure (called Context) can be stored in the SM
  • Any event can have an arbitrary user-defined payload attached. The event payload is propagated to related callbacks

Install and Quick Start Tutorial

1. Install the xstate-cpp-generator TypeScript package, locally (or globally with -g option):

   npm install xstate-cpp-generator

2. Create a simple Xstate model file engineer.ts with few lines to trigger C++ generation at the end:

(this example is located at https://github.com/shuvalov-mdb/xstate-cpp-generator/tree/master/demo-project)

const CppGen = require('xstate-cpp-generator');
const path = require('path');

import { Machine } from 'xstate';

const engineerMachine = Machine({
    id: 'engineer',
    initial: 'sleeping',
    states: {
        sleeping: {
            entry: 'startWakeupTimer',
            exit: 'morningRoutine',
            on: {
                'TIMER': { target: 'working', actions: ['startHungryTimer', 'startTiredTimer'] },
            }
        },
        working: {
            entry: ['checkEmail', 'startHungryTimer', 'checkIfItsWeekend' ],
            on: {
                'HUNGRY': { target: 'eating', actions: ['checkEmail']},
                'TIRED': { target: 'sleeping' },
                'ENOUGH': { target: 'weekend' }
            },
        },
        eating: {
            entry: 'startShortTimer',
            exit: [ 'checkEmail', 'startHungryTimer' ],
            on: {
                'TIMER': { target: 'working', actions: ['startHungryTimer'] },
                'TIRED': { target: 'sleeping' }
            }
        },
        weekend: {
            type: 'final',
        }
    }
});

CppGen.generateCpp({
    xstateMachine: engineerMachine,
    destinationPath: "",
    namespace: "engineer_demo",
    pathForIncludes: "",
    tsScriptName: path.basename(__filename)
  });

To visualize this State Machine copy-paste the 'Machine' method call to the online vizualizer.

3. Generate C++

Install all required dependencies:

   npm install

And run the C++ generator:

   ts-node engineer.ts

You should see new generated files:

engineer_sm.h  engineer_sm.cpp  engineer_test.cpp

The engineer_test.cpp is an automatically generated Unit Test for the model. Create a simple SConscript file to compile it:

env = Environment()

LIBS =''

common_libs = ['gtest_main', 'gtest', 'pthread']
env.Append( LIBS = common_libs )

env.Append(CCFLAGS=['-fsanitize=address,undefined',
                    '-fno-omit-frame-pointer'],
           LINKFLAGS='-fsanitize=address,undefined')

env.Program('engineer_test', ['engineer_sm.cpp', 'engineer_test.cpp'], 
            LIBS, LIBPATH='/opt/gtest/lib:/usr/local/lib', CXXFLAGS="-std=c++17")

and run it with:

   scons
   ./engineer_test

Release Notes

V 1.0.3

  • Full support of entry, exit and transition Actions
  • Multi-threading bugfixes

V 1.0.4

  • Converted onEnteredState() from move sematics && to shared_ptr
  • Started Tutorial
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].