Friday, 16 December 2011

Finite State Machine in C++

In my article "Finite State Machine in C" I gave a short introduction to Finite State Machines (FSM) and demonstrated two possible implementations of the Car state machine model.

In this article I will show C++ implementation of the same model, by using State Pattern. All state classes are derived from an abstract base class CState and each of them implements public method HandleEvent(EVENT evt). CSMManager class is a State Machine Manager and represents the core of our State Machine: it receives events and dispatches them to the current state for handling. Its private member m_pCurrState is a pointer to the CState base class but it always points to actual state objects. State transition is implemented as its reassignment to a different state object (object's address). This happens when some event occurs. Behaviour of this model is event-driven and, in contrast to C implementations, state transition control is not centralized here - it is not the State Manager who takes care of which state is going to be the next one. Current state decides on that itself, depending on its current conditions and events it receives.
















Event received: EVENT_IGNITE
   Whoooa! I'm turned on!

Event received: EVENT_ACCELERATE
   Yipee! I'm accelerating!

Event received: EVENT_BRAKE
   Whoops! Was I too fast?

Event received: EVENT_TURN_OFF
   That was probably enough...


Note that State Manager's member which refers to the current state (m_pCurrState) is not of reference type (CState&) but a pointer (CState*). This is one of the cases where we MUST use pointer instead of reference because we want to have a variable which reffers to different objects throughout the execution and as re-seating the reference is not allowed, the only option is using a pointer. Please refer Parashift's FAQ on References and this SO question.

The reason for introducing m_prevStateID is that sometimes state machine (or some of its states) needs to know what was its previous state. Variable which keeps track of the previous state should not be of type reference (m_prevState : CState&) or pointer (m_pPrevState : CState*) as current state should not be able to access (members) of other states. It is therefore enough if it holds only the ID of the previous state.

Note that base abstract class CState declares OnEntry() and OnExit() methods - those which are executed when entering and leaving state. Current state executes HandleEvent(EVENT evt) each time it receives some event.


PPS Gusain said...

Nice post. Thanks

PPS Gusain said...

Nice post. Thanks

sapide said...

Among many solutions found on the web using state pattern, this is the most balanced approach so far. Did you also managed to make a unit test for this? I would think of something like log file comparison or something like that. Well done!

Chris Hemsworth said...

The article is so informative. This is more helpful for our
magento training course in chennai
magento training institute in chennai
magento 2 training in chennai
magento development training
magento 2 course
magento developer training
Thanks for sharing.

zuan said...

Thanks for sharing an informative blog keep rocking bring more details.I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
web designer course in chennai
web design development training in chennai
web designing course with placement in chennai
web design and programming courses in chennai
web design and development training in chennai
web designing courses in chennai
web designing training and placement in chennai
mobile application development course
mobile app development training
mobile application development training online
web designing course with placement in chennai
web designing training institute in chennai
web design and development training
Web Designing Course Training Institute in Chennai
mobile app development course
mobile application development course
learn mobile application development

divi said...

This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.web design company in velachery