git-svn-id: http://moon:8086/svn/software/trunk/projects/FsmEval@139 b431acfa-c32f-4a4a-93f1-934dc6c82436
175 lines
2.5 KiB
C++
175 lines
2.5 KiB
C++
/*
|
|
* File: FsmMode.hpp
|
|
* Author: jens
|
|
*
|
|
* Created on 24. Januar 2015, 15:54
|
|
*/
|
|
|
|
#ifndef FSMMODE_HPP
|
|
#define FSMMODE_HPP
|
|
|
|
#include "AStateMachine.hpp"
|
|
#include "FsmProcess.hpp"
|
|
|
|
class ModeContext
|
|
{
|
|
public:
|
|
ModeContext(FsmProcess<Mode0> *mode0, FsmProcess<Mode1> *mode1)
|
|
: fsmMode0(mode0)
|
|
, fsmMode1(mode1)
|
|
{
|
|
|
|
}
|
|
|
|
~ModeContext() {}
|
|
|
|
FsmProcess<Mode0> *fsmMode0;
|
|
FsmProcess<Mode1> *fsmMode1;
|
|
|
|
};
|
|
|
|
class IFsmMode
|
|
{
|
|
public:
|
|
|
|
enum StateId
|
|
{
|
|
INIT=0,
|
|
MODE0,
|
|
MODE1,
|
|
NUM_STATES
|
|
};
|
|
|
|
IFsmMode()
|
|
{
|
|
|
|
}
|
|
|
|
virtual ~IFsmMode()
|
|
{
|
|
|
|
}
|
|
|
|
virtual void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
}
|
|
|
|
};
|
|
|
|
class FsmMode : public AStateMachine<IFsmMode::StateId>
|
|
{
|
|
public:
|
|
|
|
FsmMode(ModeContext &ctx)
|
|
: AStateMachine()
|
|
, init(IFsmMode::INIT, ctx)
|
|
, mode0(IFsmMode::MODE0, ctx)
|
|
, mode1(IFsmMode::MODE1, ctx)
|
|
{
|
|
add(&init);
|
|
add(&mode0);
|
|
add(&mode1);
|
|
}
|
|
|
|
~FsmMode()
|
|
{
|
|
}
|
|
|
|
void process(int x)
|
|
{
|
|
dynamic_cast<IFsmMode*>(currState())->process(x);
|
|
}
|
|
|
|
private:
|
|
class AStateBase : public AState<IFsmMode::StateId>, public IFsmMode
|
|
{
|
|
public:
|
|
AStateBase(IFsmMode::StateId id, ModeContext &context)
|
|
: AState(id)
|
|
, m_context(context)
|
|
{
|
|
|
|
}
|
|
|
|
protected:
|
|
ModeContext &m_context;
|
|
|
|
};
|
|
|
|
class StateInit : public AStateBase
|
|
{
|
|
public:
|
|
StateInit(IFsmMode::StateId id, ModeContext &context)
|
|
: AStateBase(id, context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateInit() {}
|
|
|
|
// Transition to Init is reset
|
|
void onTransition()
|
|
{
|
|
m_context.fsmMode0->reset();
|
|
m_context.fsmMode1->reset();
|
|
}
|
|
|
|
void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
|
|
if (x == 10)
|
|
setState(IFsmMode::MODE0);
|
|
}
|
|
|
|
};
|
|
|
|
class StateMode0 : public AStateBase
|
|
{
|
|
public:
|
|
StateMode0(IFsmMode::StateId id, ModeContext &context)
|
|
: AStateBase(id, context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateMode0() {}
|
|
|
|
void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
m_context.fsmMode0->process(x);
|
|
if (x == 20)
|
|
setState(IFsmMode::MODE1);
|
|
}
|
|
|
|
};
|
|
|
|
class StateMode1 : public AStateBase
|
|
{
|
|
public:
|
|
StateMode1(IFsmMode::StateId id, ModeContext &context)
|
|
: AStateBase(id, context)
|
|
{
|
|
|
|
}
|
|
|
|
~StateMode1() {}
|
|
|
|
void process(int x)
|
|
{
|
|
printf("%s() x=%d\n", __PRETTY_FUNCTION__, x);
|
|
m_context.fsmMode1->process(x);
|
|
}
|
|
|
|
};
|
|
private:
|
|
StateInit init;
|
|
StateMode0 mode0;
|
|
StateMode1 mode1;
|
|
};
|
|
|
|
#endif /* FSMMODE_HPP */
|
|
|