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