| View previous topic :: View next topic |
| Author |
Message |
WittyGuy Guest
|
Posted: Tue Jul 12, 2005 4:17 pm Post subject: FSM Implementation C++ |
|
|
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
|
Posted: Tue Jul 12, 2005 10:38 pm Post subject: Re: FSM Implementation C++ |
|
|
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
|
Posted: Wed Jul 13, 2005 4:19 pm Post subject: Re: FSM Implementation C++ |
|
|
"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
|
Posted: Thu Jul 14, 2005 1:14 am Post subject: Re: FSM Implementation C++ |
|
|
"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
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Fri Jul 15, 2005 5:21 pm Post subject: Re: FSM Implementation C++ |
|
|
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
|
Posted: Sat Jul 16, 2005 11:58 pm Post subject: Re: FSM Implementation C++ |
|
|
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 |
|
 |
|