Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Hierarchical State Machine Design in C++


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

Listing 4

#include "stdafx.h"
#include "resource.h"          
#include "pelican_static.h"

HWND hWnd_=0;

void Pelican::Off::on_enter(Pelican*,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in Off");
        return;
    }
    printf("->Off::on_enter");
    EndDialog(hWnd_, 0);
}

void Pelican::vehiclesEnabled::on_enter(Pelican* p,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in vehiclesEnabled");
        return;
    }
    operational::on_enter(p,old_state);

    printf("->vehiclesEnabled::on_enter");
    signalPedestrians(DONT_WALK);
}

void Pelican::vehiclesGreen::on_enter(Pelican* p,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in vehiclesGreen");
        return;
    }
    vehiclesEnabled::on_enter(p,old_state);

    printf("->vehiclesGreen::on_enter");

    m_isPedestrianWaiting=FALSE;
    SetDlgItemText(hWnd_, IDC_STATE, "vehiclesGreen");
    SetTimer(hWnd_, 1, VEHICLES_GREEN_MIN_TOUT, 0);
    signalVehicles(GREEN); 
}

void Pelican::vehiclesGreenInt::on_enter(Pelican* p,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in vehiclesGreenInt");
        return;
    }
    vehiclesEnabled::on_enter(p,old_state);

    printf("->vehiclesGreenInt::on_enter");

    SetDlgItemText(hWnd_, IDC_STATE, "vehiclesGreenInt"); 
    signalVehicles(GREEN); 
}

void Pelican::vehiclesYellow::on_enter(Pelican* p,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in vehiclesYellow");
        return;
    }
    vehiclesEnabled::on_enter(p,old_state);
    printf("->vehiclesYellow::on_enter");

    SetDlgItemText(hWnd_, IDC_STATE, "vehiclesYellow");
    SetTimer(hWnd_, 1, VEHICLES_YELLOW_TOUT, 0);
    signalVehicles(YELLOW);
}

void Pelican::pedestriansEnabled::on_enter(Pelican* p,State<Pelican>* old_state){ 
    if (old_state && old_state->derives_from(this)) {
        printf("-already in pedestriansEnabled");
        return;
    }
    operational::on_enter(p,old_state);

    printf("->pedestriansEnabled::on_enter");

    signalVehicles(RED); 
}

void Pelican::pedestriansWalk::on_enter(Pelican* p,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in pedestriansWalk");
        return;
    }
    pedestriansEnabled::on_enter(p,old_state);

    printf("->pedestriansWalk::on_enter");

    SetDlgItemText(hWnd_, IDC_STATE, "pedestriansWalk");
    signalPedestrians(WALK);
    UINT_PTR t=SetTimer(hWnd_, 1, PEDESTRIANS_WALK_TOUT, 0);
}

void Pelican::pedestriansFlash::on_enter(Pelican* p,State<Pelican>* old_state){
    if (old_state && old_state->derives_from(this)) {
        printf("-already in pedestriansFlash");
        return;
    }
    pedestriansEnabled::on_enter(p,old_state);

    printf("->pedestriansFlash::on_enter");

    SetDlgItemText(hWnd_, IDC_STATE, "pedestriansFlash");
    SetTimer(hWnd_, 1, PEDESTRIANS_FLASH_TOUT, 0);
    pedestrianFlashCtr_ = PEDESTRIANS_FLASH_NUM*2 + 1;
}

void Pelican::vehiclesEnabled::on_exit(Pelican* p,State<Pelican>* new_state){ 
    if (new_state && new_state->derives_from(this)) {
        printf("-stay in vehiclesEnabled");
        return;
    }

    printf("->vehiclesEnabled::on_exit");

    operational::on_exit(p,new_state);
}


void Pelican::pedestriansWalk::on_exit(Pelican* p,State<Pelican>* new_state){ 
    if (new_state && new_state->derives_from(this)) {
        printf("-stay in pedestriansWalk");
        return;
    }
    printf("->pedestriansWalk::on_exit");

    KillTimer(hWnd_, 1); 
    pedestriansEnabled::on_exit(p,new_state);
}

void Pelican::vehiclesYellow::on_exit(Pelican* p,State<Pelican>* new_state){
    if (new_state && new_state->derives_from(this)) {
        printf("-stay in vehiclesYellow");
        return;
    }
    printf("->vehiclesYellow::on_exit");

    KillTimer(hWnd_, 1);
    vehiclesEnabled::on_exit(p,new_state);
}
void Pelican::vehiclesGreen::on_exit(Pelican* p,State<Pelican>* new_state){
    if (new_state && new_state->derives_from(this)) {
        printf("-stay in vehiclesGreen");
        return;
    }

    printf("->vehiclesGreen::on_exit");
    
    KillTimer(hWnd_, 1); 
    
    vehiclesEnabled::on_exit(p,new_state);
}

void Pelican::pedestriansFlash::on_exit(Pelican* p,State<Pelican>* new_state){
    if (new_state && new_state->derives_from(this)) {
        printf("-stay in pedestriansFlash");
        return;
    }

    printf("->pedestriansFlash::on_exit");

    KillTimer(hWnd_, 1);

    pedestriansEnabled::on_exit(p,new_state);
}

State<Pelican>* Pelican::operational::off(Pelican* p,const boost::any&){
    return &p->m_Off;    //can simply return 0;
}

State<Pelican>* Pelican::vehiclesGreen::pedestrianWaiting(Pelican*,const 
  boost::any&){
    m_isPedestrianWaiting = TRUE;
    return this;
}
State<Pelican>* Pelican::vehiclesGreen::timeout(Pelican* p,const boost::any&){
    if (m_isPedestrianWaiting)return &p->m_vehiclesYellow;
    else                      return &p->m_vehiclesGreenInt;
}

State<Pelican>* Pelican::vehiclesGreenInt::pedestrianWaiting(Pelican* p,const 
  boost::any&){
    return &p->m_vehiclesYellow;
}

State<Pelican>* Pelican::vehiclesYellow::timeout(Pelican* p,const boost::any&){
    return  &p->m_pedestriansWalk;
}

State<Pelican>* Pelican::pedestriansWalk::timeout(Pelican* p,const 
  boost::any&){
    return &p->m_pedestriansFlash;
}

State<Pelican>* Pelican::pedestriansFlash::timeout(Pelican* p,const boost::any&){
    if (--pedestrianFlashCtr_ == 0) //done flashing?
        return &p->m_vehiclesGreen; //note the change from Miro Samek's--
                                  //we enter derived state directly
    
    //even counter?DONT_WALK:BLANK
    signalPedestrians((pedestrianFlashCtr_ & 1) == 0? DONT_WALK : BLANK);

    return this;
}

Pelican::Pelican(){}

void Pelican::init(){
    operational::signalVehicles(operational::RED);
    operational::signalPedestrians(operational::DONT_WALK);

    //make the initial transition. note the change from Miro Samek's--
    //we enter derived state directly
    transition(&m_vehiclesGreen);
}

void Pelican::operational::signalVehicles(enum VehiclesSignal sig) {
   ShowWindow(GetDlgItem(hWnd_, IDC_RED), sig == RED);
   ShowWindow(GetDlgItem(hWnd_, IDC_YELLOW), sig == YELLOW);
   ShowWindow(GetDlgItem(hWnd_, IDC_GREEN), sig == GREEN);
}

void Pelican::operational::signalPedestrians(enum PedestriansSignal sig) {
   ShowWindow(GetDlgItem(hWnd_, IDC_DONT_WALK), sig == DONT_WALK);
   ShowWindow(GetDlgItem(hWnd_, IDC_BLANK), sig == BLANK);
   ShowWindow(GetDlgItem(hWnd_, IDC_WALK), sig == WALK);
}

BOOL CALLBACK Pelican::dlgproc(HWND hwnd,UINT iMsg,WPARAM wParam, LPARAM lParam){
   static Pelican app;
   Pelican::Event e(0);

   switch (iMsg) {
    case WM_INITDIALOG:
        hWnd_=hwnd;
        app.init();
        SendMessage(hwnd, WM_SETICON, (WPARAM)TRUE, (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_ICON1)));
        break;
    case WM_TIMER:
        e =State<Pelican>::timeout;
        break;
    case WM_COMMAND:                     
        switch (LOWORD(wParam)) {
        case IDCANCEL:
            e =State<Pelican>::off;
            break;
        case IDC_PEDESTRIAN_WAITING:
            e = State<Pelican>::pedestrianWaiting;
            break;
        }      
   }

   if(e){
    boost::any arg;
    if(0==app.dispatch(e,arg))
        EndDialog(hWnd_, 0);
   }
   return FALSE;            
}

int main(int argc, char* argv[]){
    return DialogBox(GetModuleHandle(0), MAKEINTRESOURCE(IDD_DIALOG), 0, 
      Pelican::dlgproc);                  
}


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.