AI Toolkit
Loading...
Searching...
No Matches
fsm.hpp
1#pragma once
2
156#include <memory>
157#include <vector>
158
159#include <type_traits>
160#include <concepts>
161
162namespace aitoolkit::fsm {
168 template <typename T>
169 class state {
170 public:
171 virtual ~state() = default;
172
173 virtual void enter(T& blackboard) {};
174 virtual void exit(T& blackboard) {};
175
176 virtual void pause(T& blackboard) {};
177 virtual void resume(T& blackboard) {};
178
179 virtual void update(T& blackboard) {};
180 };
181
186 template <typename T>
187 using state_ptr = std::unique_ptr<state<T>>;
188
189 template <typename S, typename T>
190 concept state_trait = std::derived_from<S, state<T>>;
191
197 template <typename T>
199 public:
203 template <state_trait<T> S>
204 void set_state(S state, T& blackboard) {
205 if (m_current_state) {
206 m_current_state->exit(blackboard);
207 }
208
209 m_current_state = std::make_unique<S>(state);
210 m_current_state->enter(blackboard);
211
212 if (m_paused) {
213 m_current_state->pause(blackboard);
214 }
215 }
216
220 void clear_state(T& blackboard) {
221 if (m_current_state) {
222 m_current_state->exit(blackboard);
223 m_current_state = nullptr;
224 }
225 }
226
230 void pause(T& blackboard) {
231 m_paused = true;
232
233 if (m_current_state) {
234 m_current_state->pause(blackboard);
235 }
236 }
237
241 void resume(T& blackboard) {
242 m_paused = false;
243
244 if (m_current_state) {
245 m_current_state->resume(blackboard);
246 }
247 }
248
252 void update(T& blackboard) {
253 if (m_paused) {
254 return;
255 }
256
257 if (m_current_state) {
258 m_current_state->update(blackboard);
259 }
260 }
261
262 private:
263 state_ptr<T> m_current_state{nullptr};
264 bool m_paused{false};
265 };
266
272 template <typename T>
274 public:
278 template <state_trait<T> S>
279 void push_state(S state, T& blackboard) {
280 if (!m_state_stack.empty()) {
281 auto& current_state = m_state_stack.back();
282 current_state->pause(blackboard);
283 }
284
285 state.enter(blackboard);
286 m_state_stack.push_back(std::make_unique<S>(state));
287 }
288
292 void pop_state(T& blackboard) {
293 if (!m_state_stack.empty()) {
294 auto& current_state = m_state_stack.back();
295 current_state->exit(blackboard);
296 m_state_stack.pop_back();
297 }
298
299 if (!m_state_stack.empty()) {
300 auto& current_state = m_state_stack.back();
301 current_state->resume(blackboard);
302 }
303 }
304
308 void update(T& blackboard) {
309 if (!m_state_stack.empty()) {
310 auto& current_state = m_state_stack.back();
311 current_state->update(blackboard);
312 }
313 }
314
315 private:
316 std::vector<state_ptr<T>> m_state_stack;
317 };
318}
A simple FSM.
Definition fsm.hpp:198
void update(T &blackboard)
Update the machine.
Definition fsm.hpp:252
void resume(T &blackboard)
Resume the machine.
Definition fsm.hpp:241
void clear_state(T &blackboard)
Clear the current state.
Definition fsm.hpp:220
void pause(T &blackboard)
Pause the machine.
Definition fsm.hpp:230
void set_state(S state, T &blackboard)
Enters in a new state, exiting the previous one (if any).
Definition fsm.hpp:204
A stack FSM.
Definition fsm.hpp:273
void push_state(S state, T &blackboard)
Enters in a new state, pausing the previous one (if any).
Definition fsm.hpp:279
void pop_state(T &blackboard)
Exits the current state, resuming the previous one (if any).
Definition fsm.hpp:292
void update(T &blackboard)
Update the machine.
Definition fsm.hpp:308
A state of the FSM.
Definition fsm.hpp:169
Definition fsm.hpp:190
std::unique_ptr< state< T > > state_ptr
Heap-allocated pointer to a state.
Definition fsm.hpp:187