Go to the documentation of this file.00001 #ifndef __TIMER_CPP__
00002 #define __TIMER_CPP__
00003
00004 #include <utilities/exception.h>
00005 #include <utilities/timer.h>
00006 #include <utilities/testing_utilities.h>
00007
00008 namespace nitro
00009 {
00010 Timer::Timer( void )
00011 {
00012 Timeout = 1000;
00013
00014 Instant = true;
00015
00016 Single = false;
00017 }
00018
00019 void Timer::SetTimeout( const std::size_t theTimeout )
00020 {
00021 try
00022 {
00023 Timeout = theTimeout;
00024 }
00025 catch( nitro::exception e )
00026 {
00027 throw( nitro::exception( std::string( "Timer::SetTimeout( const std::size_t theTimeout )::" ) + e.what() , e.code() ) );
00028 }
00029 catch( ... )
00030 {
00031 throw( nitro::exception( std::string( "Timer::SetTimeout( const std::size_t theTimeout )::An error occured" ) , 0 ) );
00032 }
00033 }
00034
00035 void Timer::SetInstant( const bool theInstant )
00036 {
00037 try
00038 {
00039 Instant = theInstant;
00040 }
00041 catch( nitro::exception e )
00042 {
00043 throw( nitro::exception( std::string( "Timer::SetInstant( const bool theInstant )::" ) + e.what() , e.code() ) );
00044 }
00045 catch( ... )
00046 {
00047 throw( nitro::exception( std::string( "Timer::SetInstant( const bool theInstant )::An error occured" ) , 0 ) );
00048 }
00049 }
00050
00051 void Timer::SetSingle( const bool theSingle )
00052 {
00053 try
00054 {
00055 Single = theSingle;
00056 }
00057 catch( nitro::exception e )
00058 {
00059 throw( nitro::exception( std::string( "Timer::SetSingle( const bool theSingle )::" ) + e.what() , e.code() ) );
00060 }
00061 catch( ... )
00062 {
00063 throw( nitro::exception( std::string( "Timer::SetSingle( const bool theSingle )::An error occured" ) , 0 ) );
00064 }
00065 }
00066
00067 void * _TimerWrapperFunction( void * Param )
00068 {
00069 Timer * TimerPtr( ( Timer * ) Param );
00070
00071 if( TimerPtr->Instant )
00072 {
00073 TimerPtr->Action();
00074
00075 if( TimerPtr->Single )
00076 {
00077 return( NULL );
00078 }
00079 }
00080
00081 for( ; true ; )
00082 {
00083 ThreadAbstraction::Sleep( TimerPtr->Timeout );
00084
00085 TimerPtr->Action();
00086
00087 if( TimerPtr->Single )
00088 {
00089 return( NULL );
00090 }
00091 }
00092 }
00093
00094 void Timer::Run( void )
00095 {
00096 try
00097 {
00098 TimerThread.CreateThread( _TimerWrapperFunction , ( void * )( Timer * )this );
00099 }
00100 catch( nitro::exception e )
00101 {
00102 throw( nitro::exception( std::string( "Timer::Run( void )::" ) + e.what() , e.code() ) );
00103 }
00104 catch( ... )
00105 {
00106 throw( nitro::exception( std::string( "Timer::Run( void )::An error occured" ) , 0 ) );
00107 }
00108 }
00109
00110 void Timer::Action( void )
00111 {
00112 try
00113 {
00114 }
00115 catch( nitro::exception e )
00116 {
00117 throw( nitro::exception( std::string( "Timer::Action( void )::" ) + e.what() , e.code() ) );
00118 }
00119 catch( ... )
00120 {
00121 throw( nitro::exception( std::string( "Timer::Action( void )::An error occured" ) , 0 ) );
00122 }
00123 }
00124
00125 Timer::~Timer()
00126 {
00127 try
00128 {
00129 }
00130 catch( nitro::exception e )
00131 {
00132 }
00133 catch( ... )
00134 {
00135 }
00136 }
00137
00138
00139
00140
00141 BEGIN_TESTING_SECTION()
00142
00143 #ifdef ENABLE_AUTOTESTING
00144
00145 class TestTimer : public Timer{
00146 public:
00147 virtual void Action( void )
00148 {
00149 std::cout<<"TEST PASSED"<<std::endl;
00150 }
00151 };
00152
00153 TestTimer t;
00154
00155 NITRO_EXPORTING void tstCreateTimer( void )
00156 {
00157 t.Run();
00158 }
00159
00160 #endif
00161
00162 END_TESTING_SECTION()
00163 }
00164
00165 #endif