diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-05-25 17:47:34 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-05-25 17:47:34 +0200 |
commit | 078038da762d7f64ae07cf416a2a08dddfc0c651 (patch) | |
tree | 474a8f9a82a7848f851f5f14ba4f65de1022c87e /shared/FSM.h | |
parent | 0350186840aa15ff2c5547d48fe831d0729b3ef0 (diff) | |
parent | 23017163757ea5e674bec4fb5529c24fe54002d7 (diff) |
Merge branch 'master' into prot/vault-puzzle (merge #5)
Diffstat (limited to 'shared/FSM.h')
-rw-r--r-- | shared/FSM.h | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/shared/FSM.h b/shared/FSM.h new file mode 100644 index 0000000..792a44f --- /dev/null +++ b/shared/FSM.h @@ -0,0 +1,50 @@ +#ifndef _FSM_H_ +#define _FSM_H_ + +#include <map> +#include <memory> + +#include "IBehaviouralState.hpp" + +/// <summary> +/// +/// </summary> +/// <typeparam name="TState">IBehaviouralState is the only accepted +/// class.</typeparam> +template <class TState> class FSM { +public: + template <class... TPState> FSM(TPState &...args) { + int i = 0; + + ((void)_states.emplace(i++, args), ...); + } + + /// <summary> + /// Implement with FSM::act() + /// </summary> + void act(); + + /// <summary> + /// Used to check current state. + /// </summary> + /// <returns>Current state.</returns> + std::shared_ptr<TState> &get_state() { return _currentState; } + + /// <summary> + /// Used to get all states. + /// </summary> + /// <returns>Current states.</returns> + std::map<int, std::shared_ptr<TState>> get_states(); + + /// <summary> + /// Sets current state, calls appropiate functions. + /// </summary> + /// <param name="">State to transition into.</param> + void set_state(std::shared_ptr<TState>); + +private: + std::map<int, std::shared_ptr<TState>> _states; + std::shared_ptr<TState> _currentState; +}; + +#endif // _FSM_H_
\ No newline at end of file |