🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.lang.learn.c-c++
3 messages
3 total messages Started by Max Coppin Wed, 23 Jan 2002 03:06
conversion to state pattern
#458
Author: Max Coppin
Date: Wed, 23 Jan 2002 03:06
167 lines
5325 bytes
Hi,
Sorry for previous binary posting.
Below is the code I need to convert to the state pattern
This is also availiable for download from www.stmikes.co.uk/max - I hope!
If you can't get the files and would like them please e-mail
maxcoppin@btinternet.com

I really appreciate it!

Thanks people!

Max

main.cpp:-

#include "cdcontroller.h"
#include <iostream.h>
#include <conio.h>
void main()
{

  cdController cd;
 cout <<endl << "Event Play when in Idle state" <<endl;
 cd.processEvent(cdController::PLAY);
 cout << cd.getStatus() <<endl;
 cout <<endl << "Event Stop when in Playing state"<<endl;
 cd.processEvent(cdController::STOP);
 cout << cd.getStatus() <<endl;
 cout <<endl << "Event Stop when in Idle state"<<endl;
 cd.processEvent(cdController::STOP);
 cout << cd.getStatus() <<endl;
 cout <<endl << "Event Play when in Idle state"<<endl;
 cd.processEvent(cdController::PLAY);
 cout << cd.getStatus() <<endl;
 cout <<endl << "Event End_Of_Last_Track when in Play state"<<endl;
 cd.processEvent(cdController::END_OF_LAST_TRACK);
 cout << cd.getStatus() <<endl;
  getch();
}


cdcontroller.h:-


// cdController.h: interface for the cdController class.
//
//////////////////////////////////////////////////////////////////////

#if
!defined(AFX_CDCONTROLLER_H__B8972782_F2D4_11D5_84B5_EEF8B4A7605C__INCLUDED_
)
#define AFX_CDCONTROLLER_H__B8972782_F2D4_11D5_84B5_EEF8B4A7605C__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <afx.h>

class cdController
{
public:

//Enumerated types
 enum EState {IDLE ,PLAYING};
 enum EEvent {PLAY, STOP,END_OF_LAST_TRACK};

 void processEvent(EEvent e);
 cdController();
 virtual ~cdController();
 CString getStatus();

private:
 CString stateToCString(EState s);
 CString eventToCString(EEvent e);
 void changeState(EState s);

 EState _state, _prevState;
 EEvent _event;
};

#endif //
!defined(AFX_CDCONTROLLER_H__B8972782_F2D4_11D5_84B5_EEF8B4A7605C__INCLUDED_
)


cdcontroller.cpp:-

// cdController.cpp: implementation of the cdController class.
//
//////////////////////////////////////////////////////////////////////

#include "cdController.h"
//#include <iostream.h>
#include <afxtempl.h>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

cdController::cdController()
{
//set initial state
 _state = IDLE;
}

cdController::~cdController()
{

}

void cdController::processEvent(EEvent e)
{
  _event = e;
 _prevState = _state;
 switch (_state)
 {
  case IDLE   : if (e == PLAY)
            changeState(PLAYING);
          break;

  case PLAYING : switch (e)
          {
           case STOP : changeState(IDLE);break;
           case END_OF_LAST_TRACK : changeState(IDLE);break;
          }
 }
}

void cdController::changeState(EState s)
{
 _state = s;
}

CString cdController::getStatus()
{ CString s;
 s="Previous state : " + stateToCString(_prevState) + "\t"
  + "Event : " + eventToCString(_event) + "\t"
  + "Current state : " +  stateToCString(_state);
 return s;
}

CString cdController::stateToCString(EState s)
{
//Have to convert enumerated type to CString
//so that it can be output to the screen as text
 switch (s)
 {
 case IDLE   : return "IDLE"; break;
 case PLAYING : return "PLAYING"; break;
 default    : return "UNKOWN STATE";
 }
}

CString cdController::eventToCString(EEvent e)
{
//Have to convert enumerated type to CString
//so that it can be output to the screen as text

 switch (e)
 {
 case PLAY      : return "PLAY"; break;
 case STOP      : return "STOP"; break;
 case END_OF_LAST_TRACK : return "END_OF_LAST_TRACK"; break;
 default       : return "UNKOWN EVENT";
 }
}
Re: conversion to state pattern
#460
Author: Robert C. Martin
Date: Wed, 23 Jan 2002 19:08
176 lines
5932 bytes
The following is the translation of your CDController to the state
pattern.  


==================cdcontroller.h===============================
#ifndef FSM_CDControllerFSM
#define FSM_CDControllerFSM

#include <stddef.h>

class CDControllerFSM;

//----------------------------------------------
// CDControllerFSMState: The base state class
//----------------------------------------------
class CDControllerFSMState
{
  public: 
    virtual const char* StateName() const = 0;
    virtual void Stop( CDControllerFSM& );
    virtual void Play( CDControllerFSM& );
    virtual void End( CDControllerFSM& );
};

//----------------------------------------------
// State: Idle
//----------------------------------------------
class CDControllerFSMIdleState : public CDControllerFSMState
{
  public: 
    virtual const char* StateName() const
        { return "Idle"; }
    virtual void Play( CDControllerFSM& );
};

//----------------------------------------------
// State: Playing
//----------------------------------------------
class CDControllerFSMPlayingState : public CDControllerFSMState
{
  public: 
    virtual const char* StateName() const
        { return "Playing"; }
    virtual void Stop( CDControllerFSM& );
    virtual void End( CDControllerFSM& );
};

//----------------------------------------------
// CDControllerFSM: The Finite State Machine class
//----------------------------------------------
class CDControllerFSM
{
  public: 
    // Static State variables
    static CDControllerFSMIdleState Idle;
    static CDControllerFSMPlayingState Playing;

    CDControllerFSM(); // default Constructor

    // Event functions
    void Stop() { itsState->Stop( *this ); }
    void Play() { itsState->Play( *this ); }
    void End() { itsState->End( *this ); }

    // State Accessor functions
    void SetState( CDControllerFSMState& theState ) 
    { itsState = &theState; }
    CDControllerFSMState& GetState() const 
    { return *itsState; }

    const char* GetCurrentStateName() const 
    { return itsState->StateName(); }

  private:
    CDControllerFSMState* itsState;
};

#endif



=========================cdcontroller.cpp===================


#include "cdcontroller.h"

CDControllerFSMIdleState CDControllerFSM::Idle;
CDControllerFSMPlayingState CDControllerFSM::Playing;

//----------------------------------------------
// Default Event Functions
//----------------------------------------------

void CDControllerFSMState::Stop(CDControllerFSM& s)
  { s.FSMError("Stop", s.GetState().StateName()); }

void CDControllerFSMState::Play(CDControllerFSM& s)
  { s.FSMError("Play", s.GetState().StateName()); }

void CDControllerFSMState::End(CDControllerFSM& s)
  { s.FSMError("End", s.GetState().StateName()); }


//----------------------------------------------
// Idle Actions and Transitions
//----------------------------------------------

void CDControllerFSMIdleState::Play( CDControllerFSM& s )
{
    s.SetState(CDControllerFSM::Playing);
}

//----------------------------------------------
// Playing Actions and Transitions
//----------------------------------------------


void CDControllerFSMPlayingState::Stop( CDControllerFSM& s )
{
    s.SetState(CDControllerFSM::Idle);
}


void CDControllerFSMPlayingState::End( CDControllerFSM& s )
{
    s.SetState(CDControllerFSM::Idle);
}

//----------------------------------------------
// State Machine Constructor: CDControllerFSM
//  set Initial State to: Idle
//
//----------------------------------------------
CDControllerFSM::CDControllerFSM() 
 : itsState(&Idle)
{
}

=======================================

This code was generated by SMC from the following input file:

FSMName CDControllerFSM
Context CDController
Initial Idle
FSMGenerator smc.generator.cpp.SMCppGenerator
{
  Idle
  {
    Play  Playing  {}
  }

  Playing
  {
    Stop  Idle  {}
    End   Idle  {}
  }
}

You can get SMC from the resources section of www.objectmentor.com





Robert C. Martin    | "Uncle Bob"              | Software Consultants
Object Mentor Inc.  | rmartin@objectmentor.com | We'll help you get
PO Box 5757         | Tel: (800) 338-6716      | your projects done.
565 Lakeview Pkwy   | Fax: (847) 573-1658      | www.objectmentor.com
Suite 135           |                          | www.XProgramming.com
Vernon Hills, IL,   | Training and Mentoring   | www.junit.org
60061               | OO, XP, Java, C++, Python|

"One of the great commandments of science is:
    'Mistrust arguments from authority.'" -- Carl Sagan
Re: conversion to state pattern
#461
Author: Sandeep
Date: Thu, 24 Jan 2002 02:34
9 lines
1561 bytes
The following articles will guide to in converting to the state pattern:

http://www.eventhelix.com/RealtimeMantra/HierarchicalStateMachine.htm

http://www.eventhelix.com/RealtimeMantra/StateMachineInheritance.htm

Sandeep
--
www.EventHelix.com - Realtime and Embedded Software Development
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads