-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStateMachine.cpp
More file actions
116 lines (94 loc) · 2.75 KB
/
StateMachine.cpp
File metadata and controls
116 lines (94 loc) · 2.75 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
/*
* @author: Andrew Horsman
* @description: The StateMachine class allows a program to register and
* manipulate states in an easy, clean fashion.
*/
// Includes:
#include "Headers/WPILibrary.h"
// Namespaces:
using namespace std;
// StateMachine Map type definition.
// stateMap<stateKey, stateHistory[]>
typedef map<int, vector<int> > holder;
typedef map<int, vector<int> >::iterator holderIterator;
/*
* StateMachine Class
* Stores states which can be read, compared, and changed.
*/
class StateMachine {
public:
StateMachine() {} // Empty constructor.
~StateMachine() {} // Empty destructor.
/*
* Register a state key with it's initial state.
* stateKey must be unique.
*/
bool RegisterState(int stateKey, int currentState) {
if (CheckIfMapKeyExists(this->stateMap, stateKey))
return false;
this->stateMap[stateKey] = this->DefaultState(currentState);
return true;
}
/*
* Changes the current state via a state key.
* Returns false is the state has not been registered via a key.
*/
bool ChangeState(int stateKey, int newState) {
if (!CheckIfMapKeyExists(this->stateMap, stateKey))
return false;
if (this->GetCurrentState(stateKey) != newState)
this->stateMap[stateKey].push_back(newState);
return true;
}
/*
* Gets the current state via a state key.
*/
int GetCurrentState(int stateKey) {
if (!CheckIfMapKeyExists(this->stateMap, stateKey))
return -1;
return this->stateMap[stateKey].back();
}
/*
* Checks if the current state via a key is the same as the compared
* key.
*/
bool CompareState(int stateKey, int compareState) {
if (!CheckIfMapKeyExists(this->stateMap, stateKey))
return false;
return (GetCurrentState(stateKey) == compareState);
}
/*
* Get the original value of the state (from registration).
*/
int GetOrigin(int stateKey) {
if (!CheckIfMapKeyExists(this->stateMap, stateKey))
return false;
return this->stateMap[stateKey].front();
}
/*
* Resets a state via it's key to it's original state upon registration.
*/
bool ResetState(int stateKey) {
if (!CheckIfMapKeyExists(this->stateMap, stateKey))
return false;
return this->ChangeState(stateKey, this->GetOrigin(stateKey));
}
private:
holder stateMap;
/*
* Check if an int key exists in a map<int, int> Map.
*/
bool CheckIfMapKeyExists(holder& checkMap, int key) {
holderIterator isFound = checkMap.find(key);
return (isFound != checkMap.end());
}
/*
* Creates a new stack for holding state values (current value and value
* history).
*/
vector<int> DefaultState(int initialState) {
vector<int> stateHistory;
stateHistory.push_back(initialState);
return stateHistory;
}
};