-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnixartsFSM.cpp
107 lines (92 loc) · 1.84 KB
/
OnixartsFSM.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "OnixartsFSM.h"
using namespace Onixarts::Tools::FSM;
Machine::Machine()
: currentState (NULL)
{
}
Machine::Machine(State& state)
: currentState(&state)
{
}
void Machine::Notify(byte event )
{
if( currentState == NULL )
return;
State* nextState = NULL;
if( currentState->Notify( event, nextState ) )
{
currentState->Exit(event);
this->currentState = nextState;
if( currentState != NULL )
currentState->Enter(event);
}
}
void Machine::SetCurrentState(State& state)
{
this->currentState = &state;
if(this->currentState != NULL )
this->currentState->Enter();
}
//----------------------------------------------------------------------------------------
State::State()
: transitions( NULL )
, size(0)
#ifndef OA_NO_CALLBACKS
, m_enterDelegate(NULL)
, m_exitDelegate(NULL)
#endif
{
}
#ifndef OA_NO_CALLBACKS
State::State(FSMEventDelegate enterDelegate)
: transitions(NULL)
, size(0)
, m_enterDelegate(enterDelegate)
, m_exitDelegate(NULL)
{
}
State::State(FSMEventDelegate enterDelegate, FSMEventDelegate exitDelegate)
: transitions(NULL)
, size(0)
, m_enterDelegate(enterDelegate)
, m_exitDelegate(exitDelegate)
{
}
#endif
void State::SetTransitions( Transition* transitions, byte size )
{
this->transitions = transitions;
this->size = size;
}
bool State::Notify(byte eventID, State*& nextState )
{
for(uint8_t i = 0; i < size; i++)
{
// NULL state breaks the loop
if (transitions[i].state == NULL)
break;
if(transitions[i].eventID == eventID )
{
nextState = transitions[i].state;
return true;
}
}
nextState = this;
return false;
}
void State::Enter(byte eventID)
{
OnEnter(eventID);
#ifndef OA_NO_CALLBACKS
if (m_enterDelegate != NULL)
m_enterDelegate(eventID);
#endif
}
void State::Exit(byte eventID)
{
OnExit(eventID);
#ifndef OA_NO_CALLBACKS
if (m_exitDelegate != NULL)
m_exitDelegate(eventID);
#endif
}