Woman, Life, Freedom


Adaptive AUTOSAR
ARA public interface header documentation
finite_state_machine.h
1#ifndef FINITE_MACHINE_STATE_H
2#define FINITE_MACHINE_STATE_H
3
4#include <map>
5#include <initializer_list>
6#include "./machine_state.h"
7
8namespace ara
9{
10 namespace com
11 {
12 namespace helper
13 {
18 template <typename T>
20 {
21 private:
22 std::map<T, MachineState<T> *> mStates;
23 T mCurrentState;
24
25 public:
26 FiniteStateMachine() noexcept = default;
27 ~FiniteStateMachine() noexcept = default;
28 FiniteStateMachine(const FiniteStateMachine &) = delete;
29 FiniteStateMachine &operator=(const FiniteStateMachine &) = delete;
30
34 void Initialize(std::initializer_list<MachineState<T> *> states, T entrypoint)
35 {
36 for (auto state : states)
37 {
38 mStates.emplace(state->GetState(), state);
39 state->Register(this);
40 }
41
42 auto _initialState = mStates.at(entrypoint);
43 // At entrypoint the previous state and the next state are the same.
44 _initialState->Activate(entrypoint);
45 mCurrentState = entrypoint;
46 }
47
50 T GetState() const noexcept
51 {
52 return mCurrentState;
53 }
54
58 {
59 return mStates.at(mCurrentState);
60 }
61
62 void Transit(T previousState, T nextState) override
63 {
64 // Only current state should be able to transit to another state
65 if (previousState == mCurrentState)
66 {
67 auto _nextMachineState = mStates.at(nextState);
68 mCurrentState = nextState;
69 _nextMachineState->Activate(previousState);
70 }
71 }
72 };
73 }
74 }
75}
76#endif
Abstract Finite State Machine (FMS) that transits between states.
Definition: abstract_state_machine.h:14
Finite State Machine (FMS) controller.
Definition: finite_state_machine.h:20
void Transit(T previousState, T nextState) override
Tranit from the current state to a new state.
Definition: finite_state_machine.h:62
MachineState< T > * GetMachineState() const
Get the current machine state object.
Definition: finite_state_machine.h:57
void Initialize(std::initializer_list< MachineState< T > * > states, T entrypoint)
Initalize the FSM.
Definition: finite_state_machine.h:34
T GetState() const noexcept
Get the FSM current state.
Definition: finite_state_machine.h:50
Machine state abstract class.
Definition: machine_state.h:46