00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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" );
00113 MyState S2( "two" );
00114 MyState S3( "three" );
00115
00116 string EventOne( "ONE" );
00117 string EventTwo( "TWO" );
00118
00119 try
00120 {
00121
00122
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