Main Page | Class List | File List | Class Members | File Members

TransitionFunctions.cpp

Go to the documentation of this file.
00001 
00005 //
00006 // Date:        15 August   2005   Initial
00007 //
00008 // Copyright (c) Sergey Satskiy 2003 - 2005
00009 //               <sergesatsky@yahoo.com>
00010 //
00011 // Permission to copy, use, modify, sell and distribute this software 
00012 // is granted provided this copyright notice appears in all copies. 
00013 // This software is provided "as is" without express or implied
00014 // warranty, and with no claim as to its suitability for any purpose.
00015 //
00016 
00017 
00018 #include <iostream>
00019 #include <string>
00020 using namespace std;
00021 
00022 #include "fsm.h"
00023 using namespace FSM;
00024 
00025 
00026 
00028 class MyState : public StateBase< MyState, string >
00029 {
00030     private:
00031         string      StateName;      
00032 
00033     public:
00037         MyState( const string &  Name ) : StateName( Name ) 
00038         {}
00039     
00042         MyState() : StateName( "" ) 
00043         {}
00044 
00049         bool operator==( const MyState & RHS ) const 
00050         { return this->StateName == RHS.StateName; }
00051     
00055         string  GetName( void ) const
00056         { return StateName; }
00057 };
00058 
00059 
00060 
00061 
00067 void f1( MyState &  From, string &  Event, MyState &  To );
00068 void f1( MyState &  From, string &  Event, MyState &  To )
00069 {
00070     cout << "Function f1." << endl 
00071          << "    From:  "  << From.GetName() << endl
00072          << "    To:    "  << To.GetName()   << endl
00073          << "    Event: "  << Event          << endl;
00074 }
00075 
00076 
00082 void f2( MyState &  From, string &  Event, MyState &  To );
00083 void f2( MyState &  From, string &  Event, MyState &  To )
00084 {
00085     cout << "Function f2." << endl 
00086          << "    From:  "  << From.GetName() << endl
00087          << "    To:    "  << To.GetName()   << endl
00088          << "    Event: "  << Event << endl;
00089 }
00090 
00091 
00097 void f3( MyState &  From, string &  Event, MyState &  To );
00098 void f3( MyState &  From, string &  Event, MyState &  To )
00099 {
00100     cout << "Function f3." << endl 
00101          << "    From:  "  << From.GetName() << endl
00102          << "    To:    "  << To.GetName()   << endl
00103          << "    Event: "  << Event << endl;
00104 }
00105 
00106 
00107 
00108 
00109 int main( void )
00110 {
00111    
00112     MyState         S1( "one" );            // First machine state
00113     MyState         S2( "two" );            // Second machine state
00114     MyState         S3( "three" );          // Third machine state
00115 
00116     string          EventOne( "ONE" );      // First event
00117     string          EventTwo( "TWO" );      // Second event
00118 
00119     try
00120     {
00121     
00122             // Help defines of state and event types
00123 
00124         #define FSMStateType    MyState
00125         #define FSMEventType    string
00126 
00127         SFuncStateMachine< MyState, string >        FuncMachine(
00128             FUNCFSM_BEGIN( S1 )
00129         
00130             FUNCFSM_STATES            S1        <<  S2      <<  S3
00131 
00132             FUNCFSM_EVENT( "ONE" )    S2[f1]    <<  S3[f2]  <<  S1[f1]
00133             FUNCFSM_EVENT( "TWO" )    S2[f3]    <<  NONE    <<  EXCEPTION
00134 
00135             FUNCFSM_END
00136                                                   );
00137     
00138         #undef FSMStateType
00139         #undef FSMEventType
00140 
00141 
00142         cout << "Functions are linked to transitions" << endl;
00143         cout << "Start state: " << FuncMachine.GetCurrentState().GetName() << endl;
00144 
00145 
00146     
00147         FuncMachine << EventOne;
00148         cout << "State must be 'two': " << FuncMachine.GetCurrentState().GetName() << endl;
00149         FuncMachine << EventOne;
00150         cout << "State must be 'three': " << FuncMachine.GetCurrentState().GetName() << endl;
00151     
00152         FuncMachine << EventOne;
00153         FuncMachine << EventTwo;
00154         cout << "State must be 'two': " << FuncMachine.GetCurrentState().GetName() << endl;
00155 
00156     }
00157     catch ( exception &  Exception )
00158     {
00159         cout << "Exception is cought. Message: '" << Exception.what() << "'" << endl;
00160         return 1;
00161     }
00162     catch ( ... )
00163     {
00164         cout << "Unknown exception is cought" << endl;
00165         return 2;
00166     }
00167     
00168     return 0;
00169 }
00170 

Generated on Mon Aug 15 06:36:01 2005 for Transitions Bound Functions FSM by  doxygen 1.4.4