-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachineObjects.h
121 lines (101 loc) · 2.67 KB
/
StateMachineObjects.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#pragma once
#include"RxConcurrent/CommonDefines.h"
#include"RxConcurrent/StateMachineMethods.h"
namespace BaseLib { namespace Concurrent {
/**
* @brief The StateMachineObjectBase class
*/
class StateMachineObjectBase
{
virtual bool ExecuteNext() = 0;
};
/**
* @brief The StateMachineMethods class
*
* TODO: typename LOCK = NullReadWriteLock - makes this synchronized or not
* TODO: virtual bool ExecutePrevious()? Is it necessary?
* TODO: Implement a StateMachineController?
*
*/
template <typename StateType>
class StateMachineObjects
: public StateMachineMethods<StateType>
, public StateMachineObjectBase
{
private:
typedef StateMachineMethods<StateType> SM;
public:
StateMachineObjects()
: StateMachineMethods<StateType>()
{}
virtual ~StateMachineObjects()
{}
CLASS_TRAITS(StateMachineObjects)
/**
* true if finished, otherwise false
*/
virtual bool ExecuteNext()
{
if(SM::isDone())
{
// -- debug --
IDEBUG() << "State machine reached final state";
// -- debug --
return false;
}
// ------------------------------------
// Set start state
// ------------------------------------
if(!SM::currentState_)
{
SM::currentState_ = SM::getStartState();
// -- debug --
ASSERT(SM::currentState_);
// -- debug --
}
// ------------------------------------
// Execute the currentState_ by calling its run()
// ------------------------------------
bool isExecuted = SM::executeTask(SM::currentState_);
// -- debug --
ASSERT(isExecuted);
// -- debug --
if(SM::isFinalState(SM::currentState_->GetStateType()))
{
// -- debug --
IDEBUG() << "Final state reached!";
// -- debug --
SM::setDone(true);
}
else // not finished, get next state
{
SM::currentState_ = SM::getState(SM::currentState_->GetNextState());
}
return SM::isDone();
}
/**
* @brief ResetToStart
*/
void ResetToStart()
{
SM::currentState_ = SM::getStartState();
SM::setDone(false);
}
/**
* @brief SetNextState
* @param stateType
* @return
*/
bool SetNextState(StateType stateType)
{
typename State<StateType>::Ptr nextState = SM::getState(stateType);
if(nextState == NULL)
{
IDEBUG() << "State not found " << stateType;
return false;
}
SM::currentState_ = nextState;
return true;
}
};
}}