git-svn-id: http://moon:8086/svn/software/trunk/projects/FsmEval@135 b431acfa-c32f-4a4a-93f1-934dc6c82436
118 lines
1.6 KiB
C++
118 lines
1.6 KiB
C++
/*
|
|
* File: AStateMachine.hpp
|
|
* Author: jens
|
|
*
|
|
* Created on 21. Januar 2015, 05:20
|
|
*/
|
|
|
|
#ifndef ASTATEMACHINE_HPP
|
|
#define ASTATEMACHINE_HPP
|
|
|
|
#include "AState.hpp"
|
|
|
|
template <class IdType>
|
|
class AStateMachine
|
|
{
|
|
public:
|
|
|
|
AStateMachine()
|
|
: m_firstState(0)
|
|
, m_currState(0)
|
|
{
|
|
}
|
|
|
|
virtual ~AStateMachine()
|
|
{
|
|
}
|
|
|
|
AState<IdType>* currState()
|
|
{
|
|
if (!m_currState)
|
|
{
|
|
reset();
|
|
}
|
|
return m_currState;
|
|
}
|
|
|
|
virtual void reset()
|
|
{
|
|
setState(m_firstState->id());
|
|
}
|
|
|
|
void add(AState<IdType> *pState)
|
|
{
|
|
if (!pState)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (findById(pState->id()))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!m_firstState)
|
|
{
|
|
m_firstState = pState;
|
|
}
|
|
else
|
|
{
|
|
AState<IdType> *pStateCurr = m_firstState;
|
|
while(pStateCurr->m_pNext)
|
|
{
|
|
pStateCurr = pStateCurr->m_pNext;
|
|
}
|
|
pStateCurr->m_pNext = pState;
|
|
}
|
|
pState->registerStateMachine(this);
|
|
}
|
|
|
|
void setState(IdType stateId)
|
|
{
|
|
AState<IdType> *pStateNext = findById(stateId);
|
|
|
|
if (!pStateNext)
|
|
{
|
|
return;
|
|
}
|
|
|
|
bool isTransition = (m_currState != pStateNext);
|
|
|
|
m_currState = pStateNext;
|
|
|
|
if (isTransition)
|
|
{
|
|
m_currState->onTransition();
|
|
}
|
|
}
|
|
|
|
private:
|
|
AState<IdType> *m_firstState;
|
|
AState<IdType> *m_currState;
|
|
|
|
AState<IdType>* findById(IdType stateId)
|
|
{
|
|
AState<IdType> *pStateNext = m_firstState;
|
|
|
|
if (pStateNext)
|
|
{
|
|
while(pStateNext->id() != stateId)
|
|
{
|
|
pStateNext = pStateNext->m_pNext;
|
|
if (!pStateNext)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
return pStateNext;
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
#endif /* ASTATEMACHINE_HPP */
|
|
|