-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachineManager.h
60 lines (42 loc) · 1.32 KB
/
StateMachineManager.h
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
//
// Created by michael_uman on 5/6/22.
//
#ifndef STATEMACHINETOOL_STATEMACHINEMANAGER_H
#define STATEMACHINETOOL_STATEMACHINEMANAGER_H
#include <string>
#include <map>
#include <functional>
#include <initializer_list>
#include <memory>
#include <vector>
using stateCb = std::function<bool()>;
using stateMap = std::map<std::string, stateCb>;
struct nodeInitializer {
std::string state;
stateCb cb;
};
class StateMachineManager;
class StateMachineNode {
public:
explicit StateMachineNode(const std::string &nodeName, std::initializer_list<nodeInitializer> initlist);
virtual ~StateMachineNode();
bool canTransition(const std::string &state);
private:
friend class StateMachineManager;
std::string nodeName;
stateMap states;
};
class StateMachineManager {
public:
explicit StateMachineManager(const std::string &machineName);
virtual ~StateMachineManager();
void addState(const std::string &nodeName, std::initializer_list<nodeInitializer> initlist);
bool setState(const std::string &state);
std::string getState() const;
private:
StateMachineNode * findNode(const std::string &nodeName);
std::string machinename;
std::vector<std::unique_ptr<StateMachineNode>> states;
StateMachineNode * current_state = nullptr;
};
#endif //STATEMACHINETOOL_STATEMACHINEMANAGER_H