aboutsummaryrefslogtreecommitdiff
path: root/shared/FSM.hpp
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-05-25 17:47:34 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-05-25 17:47:34 +0200
commit078038da762d7f64ae07cf416a2a08dddfc0c651 (patch)
tree474a8f9a82a7848f851f5f14ba4f65de1022c87e /shared/FSM.hpp
parent0350186840aa15ff2c5547d48fe831d0729b3ef0 (diff)
parent23017163757ea5e674bec4fb5529c24fe54002d7 (diff)
Merge branch 'master' into prot/vault-puzzle (merge #5)
Diffstat (limited to 'shared/FSM.hpp')
-rw-r--r--shared/FSM.hpp68
1 files changed, 68 insertions, 0 deletions
diff --git a/shared/FSM.hpp b/shared/FSM.hpp
new file mode 100644
index 0000000..c86ed80
--- /dev/null
+++ b/shared/FSM.hpp
@@ -0,0 +1,68 @@
+#ifndef _FSM_HPP_
+#define _FSM_HPP_
+
+#include "FSM.h"
+
+#include <iostream>
+
+/// <summary>
+/// This is a generic update method, unknown classes are not supported.
+/// </summary>
+/// <typeparam name="TState">IBehaviouralState is the only accepted
+/// class.</typeparam>
+template <class TState> void FSM<TState>::act() {
+ // No implementation for unknown class.
+ std::cout << "No implementation for unknown class" << std::endl;
+}
+//
+///// <summary>
+///// Calls the IBehaviouralState act method.
+///// </summary>
+///// <typeparam name="TState">IBehaviouralState is the only accepted
+/// class.</typeparam>
+template <> inline void FSM<IBehaviouralState>::act() { _currentState->act(); }
+
+/// <summary>
+/// This is a generic return of the type.
+/// </summary>
+/// <typeparam name="TState">Any class type works with this return.</typeparam>
+/// <returns>The current IBehaviouralState map.</returns>
+template <class TState>
+std::map<int, std::shared_ptr<TState>> FSM<TState>::get_states() {
+ return _states;
+}
+
+///// <summary>
+///// State transitioning from current state to newState.
+///// Calls Exit on the current state and Enter on the new state.
+///// </summary>
+///// <typeparam name="TState">IBehaviouralState is the only accepted
+/// class.</typeparam>
+///// <param name="newState">New state to transition into.</param>
+template <>
+inline void
+FSM<IBehaviouralState>::set_state(std::shared_ptr<IBehaviouralState> newState) {
+ // We can guarantee all statemachines are based on IBehaviouralState
+ if (_currentState != nullptr)
+ _currentState->exit();
+
+ _currentState = nullptr;
+ _currentState = newState;
+ _currentState->enter();
+}
+
+/// <summary>
+/// State transitioning from current state to newState.
+/// Calls Exit on the current state and Enter on the new state.
+/// </summary>
+/// <typeparam name="TState">IBehaviouralState is the only accepted
+/// class.</typeparam> <param name="newState">New state to transition
+/// into.</param>
+template <class TState>
+void FSM<TState>::set_state(std::shared_ptr<TState> newState) {
+ // We can guarantee all statemachines are based on IBehaviouralState
+ // No implementation for an unknown state change class.
+ std::cout << "No implementation for unknown state change class" << std::endl;
+}
+
+#endif // _FSM_HPP_ \ No newline at end of file