Reputation: 595
I am learning to use finite-state machines for some tasks but I am having problems navigating my state table and executing the functions to make it a useful system.
Consider my state machine:
(source: wikimedia.org)
Explanation:
* = Print char to stdout
N = '\n'
S = ' '
A = aA-zZ
The code I started with from Automata-based programming on Wikipedia works for such a simple machine, but I want to modify it so that I can have a more robust state transition table and call functions based off those states.
I've posted working basic code at Pastebin, along with the transtion table style I want to use.
I have not used pointers to functions before so I am not sure how to write the transition functions based off the data received by process_event
. Eventually I would like to have a template that allows me to have state in/out & transition in/out functions so I can write complex user menus and even programming algorithms much more efficiently.
Upvotes: 2
Views: 3409
Reputation: 3344
Your compilation issue saying that it could not convert from int
to void (*)(int)
was from the branch struct:
struct branch
{
int event_type:3;
enum states state_new:2;
int do_func:1;
};
The do_func
is defined as an integer and not void (*do_func)(int)
;
Upvotes: 1
Reputation: 30165
Using functions as states is very powerful, but using transition tables is very error prone and painful compared to using recursive functions (function states that return functions). A fantastic implementation for you to consider is the quantum hierarchical statem machine. Although it is only about 1000 lines of code as a base, it has an accompanying book to explain any question you might have about how it works. Very powerful, very fast.
Upvotes: 2
Reputation: 10447
Did you check
Boost.msm - A very high-performance library for expressive UML2 finite state machines.
Read documentation, because it is all about managing complexity about statemachines.
There is also other state machine implementation in boost that you might prefer because it compiles faster since it is not designed for super speed (which does not means that it is not fast enough) Boost.Statechart - Arbitrarily complex finite state machines can be implemented in easily readable and maintainable C++ code.
As Brent Arias mentioned you should read book from http://www.state-machine.com/psicc2/index.php It is state machines bible.
Upvotes: 2