-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.cpp
More file actions
85 lines (72 loc) · 1.36 KB
/
example.cpp
File metadata and controls
85 lines (72 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// example.cpp
/**
* Originally by Kipras Melnikovas (https://kipras.org) <kipras@kipras.org>
* MIT Licensed
*
*
* compile with:
*
* ```sh
g++ -std=c++11 ./example.cpp ./src/*.cpp -o example.out
* ```
*
*/
#include <iostream> // std::cout
#include "./src/xstate.cpp"
int main() {
xs::StateMachine machine = {
.id = "light",
.initial = "green",
.states = {
{
"green" ,
{ .on = { { "TIMER", "yellow" } } }
},
{
"yellow",
{ .on = { { "TIMER", "red" } } }
},
{
"red" ,
{
.on = { { "TIMER", "red.red-100" } },
.nested = {
.id = "red-brightness",
.initial = "red-100",
.states = {
{
"red-100",
{ .on = { { "TIMER", "red.red-0" } } }
},
{
"red-0",
{ .on = { { "TIMER", "green" } } }
}
}
}
}
}
}
};
xs::Interpreter *toggleMachine = xs::interpret(machine)
->logInfo()
->onStart([]() {
std::cout << "let's go!\n";
})
->onTransition([](xs::Interpreter *self) {
self->logInfo();
})
->onStop([]() {
std::cout << "oh no we stopped c:\n";
})
->start();
toggleMachine->send("TIMER");
toggleMachine->send("TIMER");
toggleMachine->send("TIMER");
toggleMachine->send("TIMER");
toggleMachine->send("TIMER");
toggleMachine->send("TIMER");
toggleMachine->stop();
delete toggleMachine;
return 0;
}