-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathnested.cpp
More file actions
49 lines (42 loc) · 2.73 KB
/
nested.cpp
File metadata and controls
49 lines (42 loc) · 2.73 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
// File: examples/basic.cpp
#include <iostream>
#include "CXXStateTree/StateTree.h"
using namespace CXXStateTree;
int main()
{
auto machine = StateTree::Builder()
.initial("Main")
.state("Main", [](State &s)
{ s.initial_substate("Idle")
.substate("Idle", [](State &s)
{ s.on("Start", "Running", nullptr, [](const std::any &)
{ std::cout << "Transition: Idle -> Running" << std::endl; }); })
.substate("Running", [](State &s)
{ s.on("Stop", "Idle", nullptr, [](const std::any &)
{ std::cout << "Transition: Running -> idle" << std::endl; }); })
.on("Switch", "Alternate", nullptr, [](const std::any &)
{ std::cout << "Transition: Main -> Alternate" << std::endl; }); })
.state("Alternate", [](State &s)
{ s.initial_substate("Idle")
.substate("Idle", [](State &s)
{ s.on("Start", "Running", nullptr, [](const std::any &)
{ std::cout << "Transition: Idle -> Running" << std::endl; }); })
.substate("Running", [](State &s)
{ s.on("Stop", "Idle", nullptr, [](const std::any &)
{ std::cout << "Transition: Running -> idle" << std::endl; }); })
.on("Switch", "Main", nullptr, [](const std::any &)
{ std::cout << "Transition: Alternate -> Main" << std::endl; }); })
.build();
std::cout << "Initial state: " << machine.current_state().fullName() << std::endl;
machine.send("Start");
std::cout << "Current state: " << machine.current_state().fullName() << std::endl;
machine.send("Stop");
std::cout << "Current state: " << machine.current_state().fullName() << std::endl;
machine.send("Switch");
std::cout << "Current state: " << machine.current_state().fullName() << std::endl;
machine.send("Start");
std::cout << "Current state: " << machine.current_state().fullName() << std::endl;
machine.send("Stop");
std::cout << "Current state: " << machine.current_state().fullName() << std::endl;
return 0;
}