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