-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateManager.hpp
More file actions
131 lines (107 loc) · 4.62 KB
/
StateManager.hpp
File metadata and controls
131 lines (107 loc) · 4.62 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/**
* @brief A library for managing various states (a state machine).
* @author Honzik Schenk
*
* StateManager is a library that provides easy and efficient management of
* different states for various applications (ex: robotics). It allows you
* to add, remove, and configurate various states and manage how they transition
* between each other. A more common name for this is a state machine.
*/
#ifndef STATEMANAGER_HPP
#define STATEMANAGER_HPP
#include <string>
#include <vector>
using namespace std;
class StateManager
{
private:
struct State
{
string stateName;
bool (*stateFunction)();
bool (*transitionToState)(string activeState);
bool operator==(const State &s)
{
return stateName == s.stateName;
}
bool operator!=(const State &s)
{
return stateName != s.stateName;
}
};
State *getStateByName(const string stateName);
State dummyState;
static bool dummyStateFunction();
static bool dummyTransitionToState(string activeState);
public:
vector<State> states;
State &activeState;
StateManager();
/**
* @brief Run the state manager running the active state without transitioning to the proper next state.
* @return True if the state manager executed the active state succesfully.
*
* @warning If there are no states in the state manager, this function will always return false.
* @warning If two states want to become active at the same time, the state manager will choose the first one in the list.
*/
bool run();
/**
* @brief Run the state manager including running the active state and (if flagged true) transitioning to the proper next state.
* @param transitionToo If true, the state manager will also transition to the next state.
* @return True if the state manager executed the active state succesfully.
*
* @warning If there are no states in the state manager, this function will always return false.
* @warning If two states want to become active at the same time, the state manager will choose the first one in the list.
*/
bool run(bool transitionToo);
/**
* @brief Transition to the state that wants to become active.
* @return True if a state was transitioned to successfully, false if no state needed to be transitioned to.
*
* @warning If there are no states in the state manager, this function will always return false.
* @warning If two states want to become active at the same time, the state manager will choose the first one in the list.
*
* @note This function is called automatically when using the run() function with the transitionToo flag set to true.
*/
bool transition();
/**
* @brief Transition to a specific state.
* @param stateName The name of the state to transition to.
* @return True if the state was found and transitioned to successfully, false if the state was not found.
*/
bool transition(string stateName);
/**
* @brief Add a state to the state manager.
* @param stateName The name of the new state.
* @return True if the state was added successfully, false if the state already exists.
*/
bool addState(string stateName);
/**
* @brief Remove a state from the state manager.
* @param stateName The name of the state to remove.
* @return True if the state was removed successfully, false if not found.
*
* @warning Removing the active state will cause the state manager to return false when run until you switch to another state.
*/
bool removeState(string stateName);
/**
* @brief Set the function that will be called when the state is active.
* @param stateName The name of the state to set the function for.
* @param stateFunction The function to call when the state is active.
* @return True if the function was set successfully, false if the state was not found.
*/
bool setStateFunction(string stateName, bool (*stateFunction)());
/**
* @brief Set the function that will be called when the state is transitioning to.
* @param stateName The name of the state to set the function for.
* @param transitionToState The function to call when the state is transitioning to.
* @return True if the function was set successfully, false if the state was not found.
*/
bool setTransitionToState(string stateName, bool (*transitionToState)(string activeState));
/**
* @brief Get the name of the active state.
* @return The name of the active state.
*/
string getActiveStateName();
};
#endif // STATEMANAGER_HPP