- AState and AStateMachine have template stateId

- AStateMachine has linked list for states
- AStateMachine::reset() resets to first added state


git-svn-id: http://moon:8086/svn/software/trunk/projects/FsmEval@134 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2015-01-24 11:49:40 +00:00
parent 6c2aa8157f
commit 251ac211bd
3 changed files with 123 additions and 39 deletions
+15 -3
View File
@@ -10,15 +10,25 @@
#include "IStateMachine.hpp" #include "IStateMachine.hpp"
template <class IdType>
class AState class AState
{ {
public: public:
AState() AState<IdType> *m_pNext;
: m_pStateMachine(0)
AState(IdType id)
: m_pNext(0)
, m_pStateMachine(0)
, m_id(id)
{ {
} }
virtual ~AState() {} virtual ~AState() {}
IdType id()
{
return m_id;
}
void registerStateMachine(IStateMachine *pStateMachine) void registerStateMachine(IStateMachine *pStateMachine)
{ {
m_pStateMachine = pStateMachine; m_pStateMachine = pStateMachine;
@@ -30,7 +40,7 @@ public:
} }
protected: protected:
void setState(IStateMachine::StateId state) void setState(IdType state)
{ {
if(m_pStateMachine) if(m_pStateMachine)
{ {
@@ -39,6 +49,8 @@ protected:
} }
private: private:
IStateMachine *m_pStateMachine; IStateMachine *m_pStateMachine;
IdType m_id;
}; };
#endif /* ASTATE_HPP */ #endif /* ASTATE_HPP */
+77 -10
View File
@@ -11,12 +11,14 @@
#include "IStateMachine.hpp" #include "IStateMachine.hpp"
#include "AState.hpp" #include "AState.hpp"
template <class IdType>
class AStateMachine : public IStateMachine class AStateMachine : public IStateMachine
{ {
public: public:
AStateMachine(StateId stateInit) AStateMachine()
: m_stateCurr(stateInit) : m_firstState(0)
, m_currState(0)
{ {
} }
@@ -24,26 +26,91 @@ public:
{ {
} }
void add(StateId stateId, AState *pState) AState<IdType>* currState()
{ {
if (!m_currState)
{
reset();
}
return m_currState;
}
virtual void reset()
{
setState(m_firstState->id());
}
void add(AState<IdType> *pState)
{
if (!pState)
{
return;
}
if (findById(pState->id()))
{
return;
}
if (!m_firstState)
{
m_firstState = pState;
// m_currState = pState;
}
else
{
AState<IdType> *pStateCurr = m_firstState;
while(pStateCurr->m_pNext)
{
pStateCurr = pStateCurr->m_pNext;
}
pStateCurr->m_pNext = pState;
}
pState->registerStateMachine(this); pState->registerStateMachine(this);
m_states[stateId] = pState;
} }
void setState(StateId stateId) void setState(StateId stateId)
{ {
bool isTransition = (m_stateCurr != stateId); AState<IdType> *pStateNext = findById(stateId);
if (!pStateNext)
{
return;
}
bool isTransition = (m_currState != pStateNext);
m_currState = pStateNext;
m_stateCurr = stateId;
if (isTransition) if (isTransition)
{ {
m_states[m_stateCurr]->onTransition(); m_currState->onTransition();
} }
} }
protected: private:
StateId m_stateCurr; AState<IdType> *m_firstState;
AState *m_states[NUM_STATES]; AState<IdType> *m_currState;
AState<IdType>* findById(StateId stateId)
{
AState<IdType> *pStateNext = m_firstState;
if (pStateNext)
{
while(pStateNext->id() != stateId)
{
pStateNext = pStateNext->m_pNext;
if (!pStateNext)
{
break;
}
}
}
return pStateNext;
}
}; };
+31 -26
View File
@@ -31,7 +31,7 @@ public:
int m_count; int m_count;
}; };
class IState : public AState class IState
{ {
public: public:
IState() IState()
@@ -51,12 +51,12 @@ public:
}; };
class StateMachine : public AStateMachine class StateMachine : public AStateMachine<IStateMachine::StateId>
{ {
public: public:
StateMachine(StateId stateInit) StateMachine()
: AStateMachine(stateInit) : AStateMachine()
{ {
} }
@@ -66,16 +66,17 @@ public:
void process(int x) void process(int x)
{ {
dynamic_cast<IState*>(m_states[m_stateCurr])->process(x); dynamic_cast<IState*>(currState())->process(x);
} }
}; };
class StateInit : public IState class StateInit : public AState<IStateMachine::StateId>, public IState
{ {
public: public:
StateInit(Context &context) StateInit(IStateMachine::StateId id, Context &context)
: m_context(context) : AState(id)
, m_context(context)
{ {
} }
@@ -92,11 +93,12 @@ private:
Context &m_context; Context &m_context;
}; };
class StateIdle : public IState class StateIdle : public AState<IStateMachine::StateId>, public IState
{ {
public: public:
StateIdle(Context &context) StateIdle(IStateMachine::StateId id, Context &context)
: m_context(context) : AState(id)
, m_context(context)
{ {
} }
@@ -122,11 +124,12 @@ private:
Context &m_context; Context &m_context;
}; };
class StateActive0 : public IState class StateActive0 : public AState<IStateMachine::StateId>, public IState
{ {
public: public:
StateActive0(Context &context) StateActive0(IStateMachine::StateId id, Context &context)
: m_context(context) : AState(id)
, m_context(context)
{ {
} }
@@ -154,11 +157,12 @@ private:
Context &m_context; Context &m_context;
}; };
class StateActive1 : public IState class StateActive1 : public AState<IStateMachine::StateId>, public IState
{ {
public: public:
StateActive1(Context &context) StateActive1(IStateMachine::StateId id, Context &context)
: m_context(context) : AState(id)
, m_context(context)
{ {
} }
@@ -188,17 +192,18 @@ private:
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
Context ctx; Context ctx;
StateMachine fsm(IStateMachine::INIT); StateMachine fsm;
StateInit init(ctx); StateInit init(IStateMachine::INIT, ctx);
StateIdle idle(ctx); StateIdle idle(IStateMachine::IDLE, ctx);
StateActive0 active0(ctx); StateActive0 active0(IStateMachine::ACTIVE_0, ctx);
StateActive1 active1(ctx); StateActive1 active1(IStateMachine::ACTIVE_1, ctx);
fsm.add(IStateMachine::INIT, &init); fsm.add(&init);
fsm.add(IStateMachine::IDLE, &idle); fsm.add(&idle);
fsm.add(IStateMachine::ACTIVE_0, &active0); fsm.add(&active0);
fsm.add(IStateMachine::ACTIVE_1, &active1); fsm.add(&active1);
fsm.reset();
for (int i=0; i < 1000; i++) for (int i=0; i < 1000; i++)
{ {
fsm.process(i); fsm.process(i);