aboutsummaryrefslogtreecommitdiff
path: root/shared/FSM.h
blob: db1de07f02613667ebd8d196430a189a3fc5b95f (plain)
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
#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_