Debian Codegraph @

StateMachine.hpp

raw
1 /*
2 * StateMachine.hpp
3 *
4 * Copyright 2020 Kevin Krüger <[email protected]>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 *
21 *
22 */
23
24 #pragma once
25
26 #include <cstdint>
27 #include <map>
28 #include <unordered_set>
29 #include <tuple>
30 #include <limits>
31 #include <mutex>
32 #include "config.h"
33
34 namespace TelescopeMountControl
35 {
36 //interface template for statemachine transition notification.
37 template<typename StateType, typename SignalType>
38 class IStateNotification
39 {
40 public:
41 //every time a state change occurs this function is called.
42 virtual void OnTransitionChanged(StateType fromState, SignalType signal, StateType toState) = 0;
43
44 //If the error state is triped this callback is called.
45 virtual void OnErrorStateReached(StateType fromState, SignalType signal) = 0;
46 };
47
48 //state machine with thread safe state transition.
49 template<typename StateType, typename SignalType, class NotificationInterfaceImplementation>
50 class StateMachine
51 {
52 //tuple used for state signal relationship.
53 typedef std::tuple<StateType, SignalType> StateSignal;
54
55 private:
56 //implementation object of the notification inferface
57 NotificationInterfaceImplementation &mStateMachineNotification;
58
59 //transition table of the state changes
60 std::map<StateSignal, StateType> mTransitionTable;
61
62 //set of states which are concidered final.
63 std::unordered_set<StateType> mFinalStates;
64
65 //start state of the state machine.
66 StateType mStartState;
67
68 //any undefined transition causes this state to be active
69 StateType mErrorState;
70
71 //the pointer of the current state.
72 StateType mCurrentState;
73
74 //make this whole thing thread safe.
75 std::mutex mMutex;
76
77 public:
78 StateMachine(NotificationInterfaceImplementation &interfaceImplementation, StateType startState, StateType errorState) :
79 mStateMachineNotification(interfaceImplementation),
80 mStartState(startState),
81 mErrorState(errorState),
82 mCurrentState(mStartState)
83 {
84
85 }
86
87 virtual ~StateMachine()
88 {
89
90 }
91
92 //Reset the machine so it can simply restart.
93 bool Reset()
94 {
95 std::lock_guard<std::mutex> guard(mMutex); //lock this function call to avoid concurrent modification.
96
97 mCurrentState = mStartState;
98
99 return true;
100 }
101
102 //mark a state as final.
103 bool AddFinalState(StateType state)
104 {
105 std::lock_guard<std::mutex> guard(mMutex); //lock this function call to avoid concurrent modification.
106
107 mFinalStates.insert(state);
108
109 return false;
110 }
111
112 //Add a transition from a state to a state tripped by a signal. All types should be in range of the state and signal types.
113 bool AddTransition(StateType fromState, SignalType signal, StateType toState)
114 {
115 std::lock_guard<std::mutex> guard(mMutex); //lock this function call to avoid concurrent modification.
116
117 StateSignal stateSignalTupel = std::make_tuple(fromState, signal);
118
119 if(mTransitionTable.count(stateSignalTupel) == 0) //only deterministic state machines alowed.
120 {
121 mTransitionTable[stateSignalTupel] = toState;
122 return true;
123
124 }
125
126 return false;
127 }
128
129 //submit a signal to the state machine and do a transistion.
130 //the notify interface gets called when a transition or indefined transition occurred.
131 bool DoTransition(SignalType signal)
132 {
133 std::lock_guard<std::mutex> guard(mMutex); //lock this function call to avoid concurrent modification.
134
135 StateSignal stateSignalTupel = std::make_tuple(mCurrentState, signal);
136
137 StateType fromState = mCurrentState;
138
139 if(mTransitionTable.count(stateSignalTupel) > 0) //transition defined.
140 {
141 StateType toState = mTransitionTable[stateSignalTupel];
142
143 mCurrentState = toState;
144
145 mStateMachineNotification.OnTransitionChanged(fromState, signal, toState);
146
147 return true;
148 }
149 else //undefined transition.
150 {
151 mCurrentState = mErrorState;
152
153 mStateMachineNotification.OnErrorStateReached(fromState, signal);
154 }
155
156 return false;
157 }
158
159 //returns true if the current state is a final state.
160 bool IsFinalized()
161 {
162 std::lock_guard<std::mutex> guard(mMutex);
163
164 if(mFinalStates.count(mCurrentState) > 0)
165 {
166 return true;
167 }
168
169 return false;
170 }
171
172 // Returns true if the state machine is in the error state.
173 bool IsInErrorState()
174 {
175 std::lock_guard<std::mutex> guard(mMutex);
176
177 return mCurrentState == mErrorState;
178 }
179
180 //returns the current state of the machine.
181 StateType CurrentState()
182 {
183 std::lock_guard<std::mutex> guard(mMutex);
184
185 return mCurrentState;
186 }
187 };
188 }