- 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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user