From 251ac211bd564cfaaf08e591df0c4f58aa4d40ec Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Sat, 24 Jan 2015 11:49:40 +0000 Subject: [PATCH] - 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 --- AState.hpp | 18 ++++++++-- AStateMachine.hpp | 87 +++++++++++++++++++++++++++++++++++++++++------ main.cpp | 57 +++++++++++++++++-------------- 3 files changed, 123 insertions(+), 39 deletions(-) diff --git a/AState.hpp b/AState.hpp index 10ffea3..fa449f3 100644 --- a/AState.hpp +++ b/AState.hpp @@ -10,15 +10,25 @@ #include "IStateMachine.hpp" +template class AState { public: - AState() - : m_pStateMachine(0) + AState *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 */ diff --git a/AStateMachine.hpp b/AStateMachine.hpp index 7a924b7..595825c 100644 --- a/AStateMachine.hpp +++ b/AStateMachine.hpp @@ -11,12 +11,14 @@ #include "IStateMachine.hpp" #include "AState.hpp" +template 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* currState() { + if (!m_currState) + { + reset(); + } + return m_currState; + } + + virtual void reset() + { + setState(m_firstState->id()); + } + + void add(AState *pState) + { + if (!pState) + { + return; + } + + if (findById(pState->id())) + { + return; + } + + if (!m_firstState) + { + m_firstState = pState; +// m_currState = pState; + } + else + { + AState *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 *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 *m_firstState; + AState *m_currState; + + AState* findById(StateId stateId) + { + AState *pStateNext = m_firstState; + + if (pStateNext) + { + while(pStateNext->id() != stateId) + { + pStateNext = pStateNext->m_pNext; + if (!pStateNext) + { + break; + } + } + } + return pStateNext; + } + + }; diff --git a/main.cpp b/main.cpp index ffe37ad..83e8082 100644 --- a/main.cpp +++ b/main.cpp @@ -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 { public: - StateMachine(StateId stateInit) - : AStateMachine(stateInit) + StateMachine() + : AStateMachine() { } @@ -66,16 +66,17 @@ public: void process(int x) { - dynamic_cast(m_states[m_stateCurr])->process(x); + dynamic_cast(currState())->process(x); } }; -class StateInit : public IState +class StateInit : public AState, 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, 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, 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, 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);