C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FSM Implementation C++

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
WittyGuy
Guest





PostPosted: Tue Jul 12, 2005 4:17 pm    Post subject: FSM Implementation C++ Reply with quote



Dear all,
I am bewildering how to implement FSM in C++. I'm a newbie to FSM
too(just got some idea after searching). There are also tools like FSM
editor/ State Machine compiler. But I just want to know much in detail
about implementing the state machines. Any pointers is really
appreciable one.

thanks
Wg


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
John Fuller
Guest





PostPosted: Tue Jul 12, 2005 10:38 pm    Post subject: Re: FSM Implementation C++ Reply with quote





WittyGuy wrote:
Quote:
Dear all,
I am bewildering how to implement FSM in C++. I'm a newbie to FSM
too(just got some idea after searching). There are also tools like FSM
editor/ State Machine compiler. But I just want to know much in detail
about implementing the state machines. Any pointers is really
appreciable one.

thanks
Wg

For static state machines, see the library in boost
http://boost-sandbox.sourceforge.net/libs/statechart/doc/index.html

For a dynamic implementation see my project
http://sourceforge.net/projects/easyasap/
In this case my interest was making sure the machine and actions could
be dynamically
configurable.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Michael Jørgensen
Guest





PostPosted: Wed Jul 13, 2005 4:19 pm    Post subject: Re: FSM Implementation C++ Reply with quote




"WittyGuy" <wittyguysuku (AT) gmail (DOT) com> wrote

Quote:
Dear all,
I am bewildering how to implement FSM in C++. I'm a newbie to FSM
too(just got some idea after searching). There are also tools like FSM
editor/ State Machine compiler. But I just want to know much in detail
about implementing the state machines. Any pointers is really
appreciable one.

I usually define an FSM in terms of a number of states, and a number of
events. For each state and for each event there is some action to be taken
and a new state to jump to. So you have the following model:

(currentState, event) -> (action, newState).

Whenever an event is received you need to perform a double lookup, using
both the currentState and the event. This is most simply coded as a two
dimensional array. The action may be implemented as a function pointer.

So you have code like:

// Begin code fragment
// Disclaimer. The code below is virgin, and has not been touched by a
compiler....

typedef int state_t;
typedef int event_t;
typedef void (*action_t)(void);
typedef struct {
action_t action;
state_t newState
} entry_t;
typedef entry_t[][] fsm_t;

fsm_t fsm;

void ProcessEvent(event_t event)
{
entry_t *pEntry = fsm[state][event];
pEntry->action();
state = pEntry->newState;
}

// End code fragment

Now you just need to populate the entries in the variable fsm, and you are
flying!

Of course, all the above is very C-style'ish. You can achieve the same goal
using polymorphism and such stuff, but the above just seems very straight
forward to me. No point killing flies with a canon ball....

-Michael.



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
David Abrahams
Guest





PostPosted: Thu Jul 14, 2005 1:14 am    Post subject: Re: FSM Implementation C++ Reply with quote

"WittyGuy" <wittyguysuku (AT) gmail (DOT) com> writes:

Quote:
Dear all,
I am bewildering how to implement FSM in C++. I'm a newbie to FSM
too(just got some idea after searching). There are also tools like FSM
editor/ State Machine compiler. But I just want to know much in detail
about implementing the state machines. Any pointers is really
appreciable one.

At http://lists.boost.org/boost/2004/03/3118.php
you can find two attachments posted that show a couple of different
simple FSM generation frameworks in C++. The ideas behind these are
illustrated in http://www.boost-consulting.com/mplbook

HTH,
--
Dave Abrahams
Boost Consulting
www.boost-consulting.com

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Jez Stephens
Guest





PostPosted: Thu Jul 14, 2005 1:20 am    Post subject: Re: FSM Implementation C++ Reply with quote

This article might make an interesting read for you:
http://www.eventhelix.com/RealtimeMantra/HierarchicalStateMachine.htm

I know I found it interesting. I actually only stumbled upon it earlier
today as I too have just started pondering possible implementations of
a FSM in C++.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Jonathan Turkanis
Guest





PostPosted: Fri Jul 15, 2005 5:21 pm    Post subject: Re: FSM Implementation C++ Reply with quote

David Abrahams wrote:
Quote:
"WittyGuy" <wittyguysuku (AT) gmail (DOT) com> writes:

Dear all,
I am bewildering how to implement FSM in C++. I'm a newbie to FSM
too(just got some idea after searching). There are also tools like
FSM editor/ State Machine compiler. But I just want to know much in
detail about implementing the state machines. Any pointers is really
appreciable one.

At http://lists.boost.org/boost/2004/03/3118.php
you can find two attachments posted that show a couple of different
simple FSM generation frameworks in C++. The ideas behind these are
illustrated in http://www.boost-consulting.com/mplbook

There's an application of this technique to i/o filtering here:

http://boost-consulting.com/boost/libs/iostreams/doc/index.html?path=2.2.10

(Note, however, that the implementation has not been optimized.)

Jonathan



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Keith H Duggar
Guest





PostPosted: Sat Jul 16, 2005 11:58 pm    Post subject: Re: FSM Implementation C++ Reply with quote

There is a nice case study of implementing an FSM in

"C++ Programming Style", Chapter 8, Tom Cargill

If you can make time I think you will find it quite
informative (as is the rest of the book). And if you
do get the chance to read that section please let me
know what you think of it.


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.