- improved
git-svn-id: http://moon:8086/svn/software/trunk/projects/FsmEval@132 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -31,7 +31,7 @@ public:
|
||||
int m_count;
|
||||
|
||||
};
|
||||
class IState
|
||||
class IState : public AState
|
||||
{
|
||||
public:
|
||||
IState()
|
||||
@@ -44,11 +44,6 @@ public:
|
||||
|
||||
}
|
||||
|
||||
virtual void onTransition(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
}
|
||||
|
||||
virtual void process(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
@@ -60,8 +55,8 @@ class StateMachine : public AStateMachine
|
||||
{
|
||||
public:
|
||||
|
||||
StateMachine()
|
||||
: AStateMachine()
|
||||
StateMachine(StateId stateInit)
|
||||
: AStateMachine(stateInit)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -71,16 +66,12 @@ public:
|
||||
|
||||
void process(int x)
|
||||
{
|
||||
if(transition())
|
||||
{
|
||||
dynamic_cast<IState*>(m_states[m_stateCurr])->onTransition(x);
|
||||
}
|
||||
dynamic_cast<IState*>(m_states[m_stateCurr])->process(x);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class StateInit : public AState, public IState
|
||||
class StateInit : public IState
|
||||
{
|
||||
public:
|
||||
StateInit(Context &context)
|
||||
@@ -101,7 +92,7 @@ private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
class StateIdle : public AState, public IState
|
||||
class StateIdle : public IState
|
||||
{
|
||||
public:
|
||||
StateIdle(Context &context)
|
||||
@@ -112,9 +103,9 @@ public:
|
||||
|
||||
~StateIdle() {}
|
||||
|
||||
void onTransition(int x)
|
||||
void onTransition()
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
printf("%s\n", __PRETTY_FUNCTION__);
|
||||
m_context.m_count = 0;
|
||||
}
|
||||
|
||||
@@ -131,7 +122,7 @@ private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
class StateActive0 : public AState, public IState
|
||||
class StateActive0 : public IState
|
||||
{
|
||||
public:
|
||||
StateActive0(Context &context)
|
||||
@@ -142,6 +133,12 @@ public:
|
||||
|
||||
~StateActive0() {}
|
||||
|
||||
void onTransition()
|
||||
{
|
||||
m_context.m_count = 0;
|
||||
printf("%s, Count = %d\n", __PRETTY_FUNCTION__, m_context.m_count);
|
||||
}
|
||||
|
||||
void process(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
@@ -157,7 +154,7 @@ private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
class StateActive1 : public AState, public IState
|
||||
class StateActive1 : public IState
|
||||
{
|
||||
public:
|
||||
StateActive1(Context &context)
|
||||
@@ -168,13 +165,19 @@ public:
|
||||
|
||||
~StateActive1() {}
|
||||
|
||||
void onTransition()
|
||||
{
|
||||
m_context.m_count = 5;
|
||||
printf("%s, Count = %d\n", __PRETTY_FUNCTION__, m_context.m_count);
|
||||
}
|
||||
|
||||
void process(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
m_context.m_count--;
|
||||
if (m_context.m_count == 0)
|
||||
{
|
||||
setState(IStateMachine::IDLE);
|
||||
setState(IStateMachine::ACTIVE_0);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -185,7 +188,7 @@ private:
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Context ctx;
|
||||
StateMachine fsm;
|
||||
StateMachine fsm(IStateMachine::INIT);
|
||||
StateInit init(ctx);
|
||||
StateIdle idle(ctx);
|
||||
StateActive0 active0(ctx);
|
||||
|
||||
Reference in New Issue
Block a user