FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
December 01, 2005

Hierarchical State Machine Design in C++

(Page 4 of 7)

December, 2005: Hierarchical State Machine Design in C++

Listing 1

class Aggregator;

class NO_VTABLE State:public IState_base<Aggregator,State>
{
public:
  virtual State* event1(Aggregator*,const boost::any&){return this;}
  virtual State* event2(Aggregator*,const boost::any&){return this;}
};
/*****************************************************************************/
     IState_base<Aggregator>
          |
        State     <<-app-specific, prototypes every event handled by the system
        / | \
       /  |  \
      /   |   \
state3  state4 state1 <<each state implements events that it cares about
                |
               state2
******************************************************************************/
class Aggregator:public Fsm<Aggregator,State>{
    class state1:public State{
        virtual State* event1 (Aggregator* p,const boost::any&)
        {return &p->m_state2;}
        virtual State* event2 (Aggregator* p,const boost::any&){return this;}
    } m_state1;

    class state2:public state1{
        virtual State* event1(Aggregator* p,const boost::any&)
        {return &p->m_state1;}
    } m_state2;
};

Aggregator::Event get_event(boost::any& arg){
   return (random)? State::event1 : State::event2;      
}

main(){
 for(;;){
   boost::any arg;
   Aggregator::Event e=get_event(arg);
   State* next_state=a.dispatch(e,arg);
   if(0==next_state) break;
 }
}

Previous Page | 1 | 2 | 3 | 4 | 5 | 6 | 7 Next Page
TOP 5 ARTICLES
No Top Articles.



MICROSITES
FEATURED TOPIC

ADDITIONAL TOPICS

INFO-LINK