//Note: spaces added for < ... > for this html file to display correctly,
//please delete the spaces after downloading this file, otherwise it
//will not compile
//
#include "iostream.h"
#include < list> //space added for html file, please remove the space
#include < map>
using namespace std ;
class Transition
{
public:
Transition ( int cs, int ie, int in)
{
m_iCurrentState = cs ;
m_iEvent = ie;
m_iNextState = in ;
}
int GetCurrentState ( ) const { return m_iCurrentState ; }
int GetEvent ( ) const { return m_iEvent ; }
int GetNextState ( ) const { return m_iNextState ; }
private :
int m_iCurrentState ; //this is subscript of map
int m_iEvent ;
int m_iNextState ;
} ;
void processingEvent ( int event )
{
cout << "processingEvent: " << event << endl ;
}
int findNextState ( map < int, list < Transition > > &stateMap,
int currentState, int eventReceived )
{
int nextState = -1 ;
list< Transition> tl = stateMap[currentState];
list< Transition>::iterator it ;
for ( it = tl.begin( ) ; it != tl.end( ) ; it++ )
{
if ( it->GetEvent( ) == eventReceived )
{
nextState = it->GetNextState( ) ;
break ;
}
}
return nextState ;
}
int main ( void )
{
list< Transition> l ;
map< int, list< Transition> > m ;
Transition t0 (0, 0, 1 ) ;
Transition t1 (1, 1, 2 ) ;
Transition t2 (2, 2, 0 ) ;
Transition t3 (2, 3, 0 ) ;
m[0].push_back ( t0 ) ; //calling list::push_back
m[1].push_back ( t1 ) ;
m[2].push_back ( t2 ) ;
m[2].push_back ( t3 ) ;
int event = 0 ;
int goodState = 1 ;
int currentState = 0 ;
while ( goodState == 1 )
{
int nextState = findNextState ( m, currentState, event) ;
if ( nextState == -1 )
{
goodState = 0 ;
}
cout << "currentState = " << currentState << endl ;
processingEvent ( event ) ;
cout << "nextState = " << nextState << endl ;
cout << endl ;
currentState = nextState ;
event++ ;
}
/*
Output:
currentState = 0
processingEvent: 0
nextState = 1
currentState = 1
processingEvent: 1
nextState = 2
currentState = 2
processingEvent: 2
nextState = 0
currentState = 0
processingEvent: 3
nextState = -1
*/
}