Skip to content

Commit dd853d7

Browse files
committed
Snapshot for the 1.0.3 release with updated docs
1 parent ec5fd24 commit dd853d7

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

README.md

Lines changed: 57 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,34 @@
33
This package allows to convert TypeScript language State Machine developed
44
using [Xstate](https://github.com/davidkpiano/xstate) into C++ generated SM, no coding required.
55

6-
Project location: https://github.com/shuvalov-mdb/xstate-cpp-generator
7-
Copyright Andrew Shuvalov, MIT [License](https://github.com/shuvalov-mdb/xstate-cpp-generator/blob/master/LICENSE)
6+
* Project location: https://github.com/shuvalov-mdb/xstate-cpp-generator
7+
* NPM TypeScript package location: https://www.npmjs.com/package/xstate-cpp-generator
8+
* Copyright Andrew Shuvalov, MIT [License](https://github.com/shuvalov-mdb/xstate-cpp-generator/blob/master/LICENSE)
89

910
## Features
1011

1112
* Design and test the State Machine in [Xstate](https://github.com/davidkpiano/xstate) and then convert to C++ without any changes
1213
* Use the [online vizualizer](https://xstate.js.org/viz/) to debug the State Machine
13-
* SM basics: [States](https://xstate.js.org/docs/guides/states.html), [Events](https://xstate.js.org/docs/guides/events.html), [Transitions](https://xstate.js.org/docs/guides/transitions.html)
14+
* SM basic features supported: [States](https://xstate.js.org/docs/guides/states.html), [Events](https://xstate.js.org/docs/guides/events.html), [Transitions](https://xstate.js.org/docs/guides/transitions.html)
15+
* SM extra features supported: [Actions](https://xstate.js.org/docs/guides/actions.html#declarative-actions)
1416
* Generated C++ is fully synchronized, safe to use in multi-threaded environemnt without any changes
15-
* No exteral dependencies except STL. No boost dependency.
16-
* Callback model based on subclassing and virtual methods
17-
* Callbacks are invoked when: leaving a state, entering a state, after entering a state
17+
* No external dependencies except STL. No boost dependency.
18+
* Callback model:
19+
* Entry, Exit and Trasition [Actions](https://xstate.js.org/docs/guides/actions.html#declarative-actions) are code
20+
generated as static methods in the template object used to declare the State Machine and can be implemented by the user
21+
* Every state and transtion callbacks are generated as virtual methods that can be overloaded by subclassing
1822
* Arbitrary user-defined data structure (called Context) can be stored in the SM
1923
* Any event can have an arbitrary user-defined payload attached. The event payload is propagated to related callbacks
2024

2125
## Install and Quick Start Tutorial
2226

23-
Install the xstate-cpp-generator TypeScript package, locally (or globally with `-g` option):
27+
### 1. Install the xstate-cpp-generator TypeScript package, locally (or globally with `-g` option):
2428

2529
```bash
2630
npm install xstate-cpp-generator
2731
```
28-
Create a simple Xstate model file `ping.ts` with few lines to trigger C++ generation at the end:
32+
### 2. Create a simple Xstate model file `engineer.ts` with few lines to trigger C++ generation at the end:
33+
(this example is located at https://github.com/shuvalov-mdb/xstate-cpp-generator/tree/master/demo-project)
2934

3035
```TypeScript
3136
const CppGen = require('xstate-cpp-generator');
@@ -42,43 +47,78 @@ const engineerMachine = Machine({
4247
exit: 'morningRoutine',
4348
on: {
4449
'TIMER': { target: 'working', actions: ['startHungryTimer', 'startTiredTimer'] },
45-
'TIRED': { target: 'sleeping' }
4650
}
4751
},
4852
working: {
49-
entry: ['checkEmail', 'startHungryTimer' ],
53+
entry: ['checkEmail', 'startHungryTimer', 'checkIfItsWeekend' ],
5054
on: {
5155
'HUNGRY': { target: 'eating', actions: ['checkEmail']},
52-
'TIRED': { target: 'sleeping' }
56+
'TIRED': { target: 'sleeping' },
57+
'ENOUGH': { target: 'weekend' }
5358
},
5459
},
5560
eating: {
5661
entry: 'startShortTimer',
57-
exit: [ 'checkEmail', 'startHungryTimer', 'startTiredTimer' ],
62+
exit: [ 'checkEmail', 'startHungryTimer' ],
5863
on: {
5964
'TIMER': { target: 'working', actions: ['startHungryTimer'] },
6065
'TIRED': { target: 'sleeping' }
6166
}
67+
},
68+
weekend: {
69+
type: 'final',
6270
}
6371
}
6472
});
6573

66-
6774
CppGen.generateCpp({
6875
xstateMachine: engineerMachine,
6976
destinationPath: "",
70-
namespace: "mongo",
77+
namespace: "engineer_demo",
7178
pathForIncludes: "",
7279
tsScriptName: path.basename(__filename)
7380
});
81+
7482
```
83+
To visualize this State Machine copy-paste the 'Machine' method call to the [online vizualizer](https://xstate.js.org/viz/).
7584

76-
And generate C++ with:
85+
### 3. And generate C++ with
7786

7887
```bash
79-
ts-node ping.ts
88+
ts-node engineer.ts
8089
```
8190
You should see new generated files:
8291
```
83-
ping_sm.cpp ping_sm.h ping_test.cpp
92+
engineer_sm.h engineer_sm.cpp engineer_test.cpp
8493
```
94+
95+
The `engineer_test.cpp` is an automatically generated Unit Test for the model. Create a simple `SConscript` file to compile it:
96+
97+
```
98+
env = Environment()
99+
100+
LIBS =''
101+
102+
common_libs = ['gtest_main', 'gtest', 'pthread']
103+
env.Append( LIBS = common_libs )
104+
105+
env.Append(CCFLAGS=['-fsanitize=address,undefined',
106+
'-fno-omit-frame-pointer'],
107+
LINKFLAGS='-fsanitize=address,undefined')
108+
109+
env.Program('engineer_test', ['engineer_sm.cpp', 'engineer_test.cpp'],
110+
LIBS, LIBPATH='/opt/gtest/lib:/usr/local/lib', CXXFLAGS="-std=c++17")
111+
112+
```
113+
and run it with:
114+
```
115+
scons
116+
./engineer_test
117+
```
118+
119+
120+
## Release Notes
121+
122+
### V 1.0.3
123+
* Full support of entry, exit and transition Actions
124+
* Multi-threading bugfixes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "xstate-cpp-generator",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "C++ code generator for Xstate State Machine",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

0 commit comments

Comments
 (0)