/* Copyright (c) 2007 Gerhard Reitmayr Permission to use, copy, modify, and distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ #ifndef _FSM_H #define _FSM_H /// list of ints as recursive type template struct IntList { enum { head = HEAD }; typedef TAIL tail; }; /// end of list marker type struct IntListEnd {}; /// switch statement implementation through iterating over the type list and /// comparing it to a given state template struct SwitchTemplate { template static typename CONTEXT::ReturnType work( int state, CONTEXT & context){ return ((STATELIST::head == state) ? context.template operator()() : SwitchTemplate::work( state, context )); } }; /// end of list marker specialization template <> struct SwitchTemplate { template static typename CONTEXT::ReturnType work( int state, CONTEXT & context){ return typename CONTEXT::ReturnType(); } }; /// The actual state machine implementation /// sub types are functors passed to the Worker class to execute the /// right template specialization of the underlying member function of the /// context object. template struct StateMachine { CONTEXT & context; int state; template struct CallEvent { typedef RET ReturnType; CONTEXT & context; DATA & data; template RET operator()(){ return context.template event(data); } }; template struct CallEventConst { typedef RET ReturnType; CONTEXT & context; const DATA & data; template RET operator()(){ return context.template event(data); } }; template struct CallEventNoData { typedef RET ReturnType; CONTEXT & context; template RET operator()(){ return context.template event(); } }; struct CallEnter { typedef void ReturnType; CONTEXT & context; template ReturnType operator()(){ return context.template enter(); } }; struct CallExit { typedef void ReturnType; CONTEXT & context; template ReturnType operator()(){ return context.template exit(); } }; struct CallTick { typedef void ReturnType; CONTEXT & context; template ReturnType operator()(){ return context.template tick(); } }; StateMachine( CONTEXT & c ) : context(c), state(STATELIST::head) { CallEnter cee = {context}; SwitchTemplate::work(state, cee); } void changeState(const int newstate){ CallExit cl = {context}; SwitchTemplate::work(state, cl); state = newstate; CallEnter cee = {context}; SwitchTemplate::work(state, cee); } void tick() { CallTick ct = {context}; SwitchTemplate::work(state, ct); } void work(){ CallEventNoData ce = {context}; int newstate = SwitchTemplate::work(state, ce); if(newstate != state) changeState(newstate); } template void work( const EVENT & ev ){ CallEventConst ce = {context, ev}; int newstate = SwitchTemplate::work(state, ce); if(newstate != state) changeState(newstate); } template void work( EVENT & ev ){ CallEvent ce = {context, ev}; int newstate = SwitchTemplate::work(state, ce); if(newstate != state) changeState(newstate); } }; /// macros for simple state list definition #define LIST1(a) IntList #define LIST2(a,b) IntList #define LIST3(a,b,c) IntList #define LIST4(a,b,c,d) IntList #define LIST5(a,b,c,d,e) IntList #define LIST6(a,b,c,d,e,f) IntList #define LIST7(a,b,c,d,e,f,g) IntList #define LIST8(a,b,c,d,e,f,g,h) IntList #endif // _FSM_H