State Library
 All Files Functions Typedefs Pages
state.h File Reference

state library api More...

Go to the source code of this file.

Typedefs

typedef int(* StateFunc )(void *data)
 Default type for a state function.
 

Functions

struct StateMachine * state_newmachine ()
 state_newmachine instanciate a new empty machine More...
 
void state_free (struct StateMachine *p)
 state_free release resource of a machine allocated with state_newmachine More...
 
int state_add_transition (struct StateMachine *machine, StateFunc from, int value, StateFunc to)
 state_add_transition Add a new transition into the machine More...
 
int state_start (struct StateMachine *machine, StateFunc start, void *data)
 state_start run a state machine More...
 

Detailed Description

state library api

The state library is really simple. First you must create a machine using the state_newmachine function. This will return an handler to an empty machine. Then you can register any state and transition using state_add_transition function. A state is a pointer to a function, transition are associated to int. State return value are used to select the next state function that will be run. Finally, you can start your machine by giving the initial state function and a pointer that will be passed to all state function. At last, release the resources associated to the machine using state_free.

#include <state.h>
#include <stdio.h>
int a(void *p){
static int counter=0;
counter+=1;
printf("a: %d\n",counter);
return counter;
}
int b(void *p){
printf("b\n");
return 0;
}
int main(){
struct StateMachine *machine;
machine=state_newmachine();
state_add_transition(machine,a,1,b);
state_add_transition(machine,b,0,a);
state_add_transition(machine,a,2,NULL);
state_start(machine,a,NULL);
state_free(machine);
}

Function Documentation

int state_add_transition ( struct StateMachine *  machine,
StateFunc  from,
int  value,
StateFunc  to 
)

state_add_transition Add a new transition into the machine

Parameters
machinea pointer obtained with state_newmachine
fromthe current machine state
valuethe value return by from
tothe state run after from, NULL if the machine must exit
Returns
0 if no error, 1 if memory error, 2 if transition already in machine
Examples:
basic.c, and demo_menu.c.
void state_free ( struct StateMachine *  p)

state_free release resource of a machine allocated with state_newmachine

Parameters
pa pointer returned by state_newmachine
Examples:
basic.c, and demo_menu.c.
struct StateMachine* state_newmachine ( )

state_newmachine instanciate a new empty machine

Returns
NULL if error, a pointer that must be release with state_free
Examples:
basic.c, and demo_menu.c.
int state_start ( struct StateMachine *  machine,
StateFunc  start,
void *  data 
)

state_start run a state machine

Parameters
machinea pointer to the state machine
startthe first state that will be run
dataa pointer that will be passed to all states
Returns
the return value of the last state run
Examples:
basic.c, and demo_menu.c.