- 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"
template <class IdType>
class AState
{
public:
AState()
: m_pStateMachine(0)
AState<IdType> *m_pNext;
AState(IdType id)
: m_pNext(0)
, m_pStateMachine(0)
, m_id(id)
{
}
virtual ~AState() {}
IdType id()
{
return m_id;
}
void registerStateMachine(IStateMachine *pStateMachine)
{
m_pStateMachine = pStateMachine;
@@ -30,7 +40,7 @@ public:
}
protected:
void setState(IStateMachine::StateId state)
void setState(IdType state)
{
if(m_pStateMachine)
{
@@ -39,6 +49,8 @@ protected:
}
private:
IStateMachine *m_pStateMachine;
IdType m_id;
};
#endif /* ASTATE_HPP */
+77 -10
View File
@@ -11,12 +11,14 @@
#include "IStateMachine.hpp"
#include "AState.hpp"
template <class IdType>
class AStateMachine : public IStateMachine
{
public:
AStateMachine(StateId stateInit)
: m_stateCurr(stateInit)
AStateMachine()
: 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);
m_states[stateId] = pState;
}
void setState(StateId stateId)
{
bool isTransition = (m_stateCurr != stateId);
AState<IdType> *pStateNext = findById(stateId);
if (!pStateNext)
{
return;
}
m_stateCurr = stateId;
bool isTransition = (m_currState != pStateNext);
m_currState = pStateNext;
if (isTransition)
{
m_states[m_stateCurr]->onTransition();
m_currState->onTransition();
}
}
protected:
StateId m_stateCurr;
AState *m_states[NUM_STATES];
private:
AState<IdType> *m_firstState;
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;
};
class IState : public AState
class IState
{
public:
IState()
@@ -51,12 +51,12 @@ public:
};
class StateMachine : public AStateMachine
class StateMachine : public AStateMachine<IStateMachine::StateId>
{
public:
StateMachine(StateId stateInit)
: AStateMachine(stateInit)
StateMachine()
: AStateMachine()
{
}
@@ -66,16 +66,17 @@ public:
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:
StateInit(Context &context)
: m_context(context)
StateInit(IStateMachine::StateId id, Context &context)
: AState(id)
, m_context(context)
{
}
@@ -92,11 +93,12 @@ private:
Context &m_context;
};
class StateIdle : public IState
class StateIdle : public AState<IStateMachine::StateId>, public IState
{
public:
StateIdle(Context &context)
: m_context(context)
StateIdle(IStateMachine::StateId id, Context &context)
: AState(id)
, m_context(context)
{
}
@@ -122,11 +124,12 @@ private:
Context &m_context;
};
class StateActive0 : public IState
class StateActive0 : public AState<IStateMachine::StateId>, public IState
{
public:
StateActive0(Context &context)
: m_context(context)
StateActive0(IStateMachine::StateId id, Context &context)
: AState(id)
, m_context(context)
{
}
@@ -154,11 +157,12 @@ private:
Context &m_context;
};
class StateActive1 : public IState
class StateActive1 : public AState<IStateMachine::StateId>, public IState
{
public:
StateActive1(Context &context)
: m_context(context)
StateActive1(IStateMachine::StateId id, Context &context)
: AState(id)
, m_context(context)
{
}
@@ -188,17 +192,18 @@ private:
int main(int argc, char** argv)
{
Context ctx;
StateMachine fsm(IStateMachine::INIT);
StateInit init(ctx);
StateIdle idle(ctx);
StateActive0 active0(ctx);
StateActive1 active1(ctx);
StateMachine fsm;
StateInit init(IStateMachine::INIT, ctx);
StateIdle idle(IStateMachine::IDLE, ctx);
StateActive0 active0(IStateMachine::ACTIVE_0, ctx);
StateActive1 active1(IStateMachine::ACTIVE_1, ctx);
fsm.add(IStateMachine::INIT, &init);
fsm.add(IStateMachine::IDLE, &idle);
fsm.add(IStateMachine::ACTIVE_0, &active0);
fsm.add(IStateMachine::ACTIVE_1, &active1);
fsm.add(&init);
fsm.add(&idle);
fsm.add(&active0);
fsm.add(&active1);
fsm.reset();
for (int i=0; i < 1000; i++)
{
fsm.process(i);