- chained FsmMode and FsmProcess
git-svn-id: http://moon:8086/svn/software/trunk/projects/FsmEval@137 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -8,8 +8,8 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
#include "AState.hpp"
|
||||
#include "AStateMachine.hpp"
|
||||
#include "FsmProcess.hpp"
|
||||
#include "FsmMode.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -17,206 +17,20 @@ using namespace std;
|
||||
*
|
||||
*/
|
||||
|
||||
class Context
|
||||
{
|
||||
public:
|
||||
Context()
|
||||
: m_count(0)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~Context() {}
|
||||
|
||||
int m_count;
|
||||
|
||||
};
|
||||
class IState
|
||||
{
|
||||
public:
|
||||
IState()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual ~IState()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
virtual void process(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
enum StateId
|
||||
{
|
||||
INIT=0,
|
||||
IDLE,
|
||||
ACTIVE_0,
|
||||
ACTIVE_1,
|
||||
NUM_STATES
|
||||
};
|
||||
|
||||
class StateMachine : public AStateMachine<StateId>
|
||||
{
|
||||
public:
|
||||
|
||||
StateMachine()
|
||||
: AStateMachine()
|
||||
{
|
||||
}
|
||||
|
||||
~StateMachine()
|
||||
{
|
||||
}
|
||||
|
||||
void process(int x)
|
||||
{
|
||||
dynamic_cast<IState*>(currState())->process(x);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class StateInit : public AState<StateId>, public IState
|
||||
{
|
||||
public:
|
||||
StateInit(StateId id, Context &context)
|
||||
: AState(id)
|
||||
, m_context(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~StateInit() {}
|
||||
|
||||
void process(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
setState(IDLE);
|
||||
}
|
||||
|
||||
private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
class StateIdle : public AState<StateId>, public IState
|
||||
{
|
||||
public:
|
||||
StateIdle(StateId id, Context &context)
|
||||
: AState(id)
|
||||
, m_context(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~StateIdle() {}
|
||||
|
||||
void onTransition()
|
||||
{
|
||||
printf("%s\n", __PRETTY_FUNCTION__);
|
||||
m_context.m_count = 0;
|
||||
}
|
||||
|
||||
void process(int x)
|
||||
{
|
||||
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
||||
if (x >= 10)
|
||||
{
|
||||
setState(ACTIVE_0);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
class StateActive0 : public AState<StateId>, public IState
|
||||
{
|
||||
public:
|
||||
StateActive0(StateId id, Context &context)
|
||||
: AState(id)
|
||||
, m_context(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~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);
|
||||
|
||||
m_context.m_count++;
|
||||
if (m_context.m_count == 10)
|
||||
{
|
||||
setState(ACTIVE_1);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
class StateActive1 : public AState<StateId>, public IState
|
||||
{
|
||||
public:
|
||||
StateActive1(StateId id, Context &context)
|
||||
: AState(id)
|
||||
, m_context(context)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~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(ACTIVE_0);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Context &m_context;
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
Context ctx;
|
||||
StateMachine fsm;
|
||||
StateInit init(INIT, ctx);
|
||||
StateIdle idle(IDLE, ctx);
|
||||
StateActive0 active0(ACTIVE_0, ctx);
|
||||
StateActive1 active1(ACTIVE_1, ctx);
|
||||
FsmProcess fsmProcess0(ctx);
|
||||
FsmProcess fsmProcess1(ctx);
|
||||
ModeContext mctx(0, &fsmProcess0, &fsmProcess1);
|
||||
FsmMode fsmMode(mctx);
|
||||
|
||||
fsm.add(&init);
|
||||
fsm.add(&idle);
|
||||
fsm.add(&active0);
|
||||
fsm.add(&active1);
|
||||
|
||||
fsm.reset();
|
||||
fsmMode.reset();
|
||||
for (int i=0; i < 1000; i++)
|
||||
{
|
||||
fsm.process(i);
|
||||
fsmMode.process(i);
|
||||
}
|
||||
fsmMode.reset();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user