PIP
0.4.0_beta2
Platform-Independent Primitives
|
Base class for custom state machine. More...
Classes | |
struct | Rule |
Rule of transition between states of machine More... | |
struct | State |
State of machine More... | |
Public Types | |
typedef PIPair< PIString, int > | Condition |
Condition is a pair (string, number) | |
Public Member Functions | |
PIStateMachine (void *_parent=0) | |
Constructs an empty state machine. | |
void | addState (Type value, const PIString &name="", Handler handler=0) |
Add state of machine. | |
int | statesCount () const |
States count. | |
void | clearStates () |
Remove all states. | |
void | addRule (Type from, Type to, const PIString &condition, Handler handler=0, bool autoTransition=false, bool resetAllConditions=false) |
Add rule of transition. | |
void | addRule (Type from, Type to, Handler handler, bool autoTransition=false, bool resetAllConditions=false) |
Add rule of transition. | |
void | addRule (Type from, Type to, const PIStringList &conditions=PIStringList(), Handler handler=0, bool autoTransition=false, bool resetAllConditions=false) |
Add rule of transition. | |
void | addRule (const Rule &rule) |
Add rule of transition. | |
int | rulesCount () const |
Rules count. | |
void | clearRules () |
Remove all rules. | |
void | setInitialState (Type value) |
Setup initial state. reset() will set machine state to "value". | |
bool | switchToState (Type to) |
Try to switch machine state to state "to". More... | |
void | reset () |
Reset machine state to initial and clear all conditions. | |
const State & | currentState () const |
Returns current state of machine. | |
void | resetConditions () |
Reset all performed conditions. | |
void | resetCondition (const PIString &name) |
Reset performed condition with name "name". | |
void | performCondition (const PIString &name, int times=1) |
Perform condition with name "name" "times" times. | |
void | performConditions (const PIStringList &names) |
Perform every condition with name from "names" one time. | |
const PIVector< Condition > & | currentConditions () const |
Returns all current performed conditions. | |
![]() | |
PIObject (const PIString &name=PIString()) | |
Contructs PIObject with name "name". | |
PIString | name () const |
Returns object name. | |
virtual const char * | className () const |
Returns object class name. | |
bool | debug () const |
Return if debug of this object is active. | |
void | setName (const PIString &name) |
Set object name. | |
void | setDebug (bool debug) |
Set object debug active. | |
const PIMap< PIString, PIVariant > & | properties () const |
Returns properties of the object. | |
int | propertiesCount () const |
Returns properties count of the object. | |
PIVariant | property (const PIString &name) const |
Returns property with name "name". | |
void | setProperty (const PIString &name, const PIVariant &value) |
Set property with name "name" to "value". If there is no such property in object it will be added. | |
bool | isPropertyExists (const PIString &name) const |
Returns if property with name "name" exists. | |
Protected Member Functions | |
virtual void | execution (const State &state) |
Reimplement this function to process current state of machine. | |
virtual void | transition (const State &from, const State &to) |
Reimplement this function to process switching current state of machine. | |
![]() | |
PIObject * | emitter () const |
Returns PIObject* which has raised an event. This value is correct only in definition of some event handler. | |
virtual void | propertyChanged (const PIString &name) |
Virtual function executes after property with name "name" has been changed. | |
Handlers | |
void | tick () |
Main function of machine. Execute execution() and check if need to switch state. | |
void | tick (void *data, int delim) |
Main function of machine. Execute execution() and check if need to switch state. | |
Additional Inherited Members | |
![]() | |
static void | piDisconnect (PIObject *src, const PIString &sig) |
Disconnect object "src" from all connections with event name "sig". | |
static void | piDisconnect (PIObject *src) |
Disconnect object "src" from all connections, i.e. all connections where object "src" is emitter. | |
static PIObject * | findByName (const PIString &name) |
Returns PIObject* with name "name" or 0, if there is no object found. | |
![]() | |
#define | piCoutObj |
Macro used for conditional (piDebug and PIObject::debug()) output to PICout for subclasses of PIObject. | |
#define | PIOBJECT(name) |
you should use this macro after class declaration to use EVENT and EVENT_HANDLER and correct piCoutObj output | |
#define | EVENT_HANDLER0(ret, name) ret name() |
declare event handler "event" with name "name" and return type "ret", ret name() | |
#define | EVENT_HANDLER1(ret, name, type0, var0) ret name(type0 var0) |
declare event handler "event" with name "name" and return type "ret", ret name(type0 var0) | |
#define | EVENT_HANDLER2(ret, name, type0, var0, type1, var1) ret name(type0 var0, type1 var1) |
declare event handler "event" with name "name" and return type "ret", ret name(type0 var0, type1 var1) | |
#define | EVENT_HANDLER3(ret, name, type0, var0, type1, var1, type2, var2) ret name(type0 var0, type1 var1, type2 var2) |
declare event handler "event" with name "name" and return type "ret", ret name(type0 var0, type1 var1, type2 var2) | |
#define | EVENT_HANDLER4(ret, name, type0, var0, type1, var1, type2, var2, type3, var3) ret name(type0 var0, type1 var1, type2 var2, type3 var3) |
declare event handler "event" with name "name" and return type "ret", ret name(type0 var0, type1 var1, type2 var2, type3 var3) | |
#define | EVENT_HANDLER EVENT_HANDLER0 |
EVENT_HANDLER is synonym of EVENT_HANDLER0. | |
#define | EVENT_VHANDLER0(ret, name) virtual ret name() |
declare virtual event handler "event" with name "name" and return type "ret", virtual ret name() | |
#define | EVENT_VHANDLER1(ret, name, type0, var0) virtual ret name(type0 var0) |
declare virtual event handler "event" with name "name" and return type "ret", virtual ret name(type0 var0) | |
#define | EVENT_VHANDLER2(ret, name, type0, var0, type1, var1) virtual ret name(type0 var0, type1 var1) |
declare virtual event handler "event" with name "name" and return type "ret", virtual ret name(type0 var0, type1 var1) | |
#define | EVENT_VHANDLER3(ret, name, type0, var0, type1, var1, type2, var2) virtual ret name(type0 var0, type1 var1, type2 var2) |
declare virtual event handler "event" with name "name" and return type "ret", virtual ret name(type0 var0, type1 var1, type2 var2) | |
#define | EVENT_VHANDLER4(ret, name, type0, var0, type1, var1, type2, var2, type3, var3) virtual ret name(type0 var0, type1 var1, type2 var2, type3 var3) |
declare virtual event handler "event" with name "name" and return type "ret", virtual ret name(type0 var0, type1 var1, type2 var2, type3 var3) | |
#define | EVENT_VHANDLER EVENT_VHANDLER0 |
EVENT_VHANDLER is synonym of EVENT_VHANDLER0. | |
#define | EVENT0(name) void name(); |
declare event "event" with name "name", void name(); | |
#define | EVENT1(name, type0, var0) void name(type0 var0); |
declare event "event" with name "name", void name(type0 var0); | |
#define | EVENT2(name, type0, var0, type1, var1) void name(type0 var0, type1 var1); |
declare event "event" with name "name", void name(type0 var0, type1 var1); | |
#define | EVENT3(name, type0, var0, type1, var1, type2, var2) void name(type0 var0, type1 var1, type2 var2); |
declare event "event" with name "name", void name(type0 var0, type1 var1, type2 var2); | |
#define | EVENT4(name, type0, var0, type1, var1, type2, var2, type3, var3) void name(type0 var0, type1 var1, type2 var2, type3 var3); |
declare event "event" with name "name", void name(type0 var0, type1 var1, type2 var2, type3 var3); | |
#define | EVENT EVENT0 |
EVENT is synonym of EVENT0. | |
#define | CONNECT0(ret, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" with check of event and handler exists | |
#define | CONNECT1(ret, type0, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" with check of event and handler exists | |
#define | CONNECT2(ret, type0, type1, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" with check of event and handler exists | |
#define | CONNECT3(ret, type0, type1, type2, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" with check of event and handler exists | |
#define | CONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" with check of event and handler exists | |
#define | CONNECT CONNECT0 |
CONNECT is synonym of CONNECT0. | |
#define | WEAK_CONNECT0(ret, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" without check of event exists | |
#define | WEAK_CONNECT1(ret, type0, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" without check of event exists | |
#define | WEAK_CONNECT2(ret, type0, type1, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" without check of event exists | |
#define | WEAK_CONNECT3(ret, type0, type1, type2, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" without check of event exists | |
#define | WEAK_CONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler) |
connect event "event" from object "src" to event handler "handler" with return type "ret" from object "dest" without check of event exists | |
#define | WEAK_CONNECT WEAK_CONNECT0 |
WEAK_CONNECT is synonym of WEAK_CONNECT0. | |
#define | DISCONNECT0(ret, src, event, dest, handler) |
piDisconnect event "event" from object "src" from event handler "handler" with return type "ret" from object "dest" | |
#define | DISCONNECT1(ret, type0, src, event, dest, handler) |
piDisconnect event "event" from object "src" from event handler "handler" with return type "ret" from object "dest" | |
#define | DISCONNECT2(ret, type0, type1, src, event, dest, handler) |
piDisconnect event "event" from object "src" from event handler "handler" with return type "ret" from object "dest" | |
#define | DISCONNECT3(ret, type0, type1, type2, src, event, dest, handler) |
piDisconnect event "event" from object "src" from event handler "handler" with return type "ret" from object "dest" | |
#define | DISCONNECT4(ret, type0, type1, type2, type3, src, event, dest, handler) |
piDisconnect event "event" from object "src" from event handler "handler" with return type "ret" from object "dest" | |
#define | DISCONNECT DISCONNECT0 |
DISCONNECT is synonym of DISCONNECT0. | |
#define | HANDLER(handler) |
Returns pointer to events handler "handler". | |
Base class for custom state machine.
This class provide functionality of state machine. You should inherit from this class, implement execution() and transition() functions, set rules and periodically call tick() function to proper work of machine.
State machine operates with "state", "rule" and "condition".
int
), associated name and optional "handler" - pointer to function executed on every tick();First of all you should define states of your machine by function addState(). Then you should define transition rules for machine by function addRule(). Finally you can set initial state by function setInitialState() and provide periodically execution of function tick().
At any time the state machine is in some state. You can ask machine to enter in new state by function switchToState(). If all conditions done machine switch it state immediately, else machine remember request and will be try switch to the new state every tick. Successfull state switching execute function transition(), every tick execute function execution() with current state. On successfull transition if rule "handler" is not null it execute. Every tick() if current state "handler" is not null it execute.
Each rule has transition condition. Condition is array of pairs (string, number). It means that every condition by name "string" should be performed as least "number" times. Empty condition always permits transition.
State machine have current performed conditions. You can read this conditions by function currentConditions() and perform new conditions by functions performCondition() and performConditions(). Currend conditions can de erased by function resetConditions().
This is simple example demonstrates all features:
|
inline |
Try to switch machine state to state "to".
If there is rule of transition exists and this rule conditions is performed then machine switched to new state immediately. Otherwise machine will be try to enter to new state every tick().
true
if state switched immediately, otherwise false