git-svn-id: http://moon:8086/svn/software/trunk/projects/FsmEval@130 b431acfa-c32f-4a4a-93f1-934dc6c82436
207 lines
2.8 KiB
C++
207 lines
2.8 KiB
C++
/*
|
|
* File: main.cpp
|
|
* Author: jens
|
|
*
|
|
* Created on 21. Januar 2015, 05:05
|
|
*/
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
#include "AState.hpp"
|
|
#include "AStateMachine.hpp"
|
|
|
|
using namespace std;
|
|
|
|
/*
|
|
*
|
|
*/
|
|
|
|
class Context
|
|
{
|
|
public:
|
|
Context()
|
|
: m_count(0)
|
|
{
|
|
|
|
}
|
|
|
|
~Context() {}
|
|
|
|
int m_count;
|
|
|
|
};
|
|
class IState
|
|
{
|
|
public:
|
|
IState()
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~IState()
|
|
{
|
|
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
};
|
|
|
|
class StateMachine : public AStateMachine
|
|
{
|
|
public:
|
|
|
|
StateMachine()
|
|
: AStateMachine()
|
|
{
|
|
}
|
|
|
|
~StateMachine()
|
|
{
|
|
}
|
|
|
|
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
|
|
{
|
|
public:
|
|
StateInit(Context &context)
|
|
: m_context(context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateInit() {}
|
|
|
|
void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
setState(IStateMachine::IDLE);
|
|
}
|
|
|
|
private:
|
|
Context &m_context;
|
|
};
|
|
|
|
class StateIdle : public AState, public IState
|
|
{
|
|
public:
|
|
StateIdle(Context &context)
|
|
: m_context(context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateIdle() {}
|
|
|
|
void onTransition(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
m_context.m_count = 0;
|
|
}
|
|
|
|
void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
if (x >= 10)
|
|
{
|
|
setState(IStateMachine::ACTIVE_0);
|
|
}
|
|
}
|
|
|
|
private:
|
|
Context &m_context;
|
|
};
|
|
|
|
class StateActive0 : public AState, public IState
|
|
{
|
|
public:
|
|
StateActive0(Context &context)
|
|
: m_context(context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateActive0() {}
|
|
|
|
void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
|
|
m_context.m_count++;
|
|
if (m_context.m_count == 10)
|
|
{
|
|
setState(IStateMachine::ACTIVE_1);
|
|
}
|
|
}
|
|
|
|
private:
|
|
Context &m_context;
|
|
};
|
|
|
|
class StateActive1 : public AState, public IState
|
|
{
|
|
public:
|
|
StateActive1(Context &context)
|
|
: m_context(context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateActive1() {}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
private:
|
|
Context &m_context;
|
|
};
|
|
|
|
int main(int argc, char** argv)
|
|
{
|
|
Context ctx;
|
|
StateMachine fsm;
|
|
StateInit init(ctx);
|
|
StateIdle idle(ctx);
|
|
StateActive0 active0(ctx);
|
|
StateActive1 active1(ctx);
|
|
|
|
fsm.add(IStateMachine::INIT, &init);
|
|
fsm.add(IStateMachine::IDLE, &idle);
|
|
fsm.add(IStateMachine::ACTIVE_0, &active0);
|
|
fsm.add(IStateMachine::ACTIVE_1, &active1);
|
|
|
|
for (int i=0; i < 1000; i++)
|
|
{
|
|
fsm.process(i);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|