Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/general/include/Timer.h

Go to the documentation of this file.
00001 /*
00002  *   $Id: Timer.h,v 1.4 1996/11/21 15:03:57 pechkin Exp $
00003  */
00004 
00005 // Copyright (C) 1995 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 
00008 // Contents: Declaration of class Timer
00009 //
00010 // Principal Author: Roger Needham
00011 //
00012 // Status: complete
00013 //
00014 // Revision History:
00015 //
00016 // 07/96 Alexey M. implemeted IPC tools
00017 
00018 #ifndef _TIMER_H_
00019 #define _TIMER_H_
00020 
00021 
00022 #include <sys/time.h>
00023 #include <iostream.h>
00024 #include <IPC.h>
00025 #include "config.h"
00026 
00027 //@db system porting
00028 
00029 #if defined(BSD)
00030 extern "C" int gettimeofday(void*, void*);
00031 #elif defined(SYSV)
00032 extern "C" int gettimeofday(struct timeval *, void *);
00033 #endif
00034 
00035 
00036 //---------------------------------------------------------------------------//
00037 //--------------------------------- Timer -----------------------------------//
00038 //---------------------------------------------------------------------------//
00039 
00040 // The resolution is 1 milisecond, with maximum interval of 2^31 / 1000 seconds
00041 // (24.9 days).
00042 
00043 class Timer
00044 {
00045 public:
00046 
00047   /////////////////////////////////////////////////////////////////////////
00048   //                                                                     //
00049   // Constructors:                                                       //
00050   //                                                                     //
00051   /////////////////////////////////////////////////////////////////////////
00052 
00053   Timer(int miliseconds) { reset( miliseconds ); }
00054 
00055   /////////////////////////////////////////////////////////////////////////
00056   //                                                                     //
00057   // Accessors:                                                          //
00058   //                                                                     //
00059   /////////////////////////////////////////////////////////////////////////
00060 
00061   bool expired( ) const {
00062          timeval tv;
00063          gettimeofday( &tv, 0 );
00064          return ( tv.tv_sec > alarmSecs ||
00065                                  ( tv.tv_sec == alarmSecs ) && ( tv.tv_usec >= alarmUSecs )
00066                                  );
00067   }
00068 
00069   void reset(int miliseconds) {
00070          timeval tv;
00071          gettimeofday( &tv, 0 );
00072          alarmSecs = tv.tv_sec + ( miliseconds / 1000 );
00073          alarmUSecs = tv.tv_usec + ( miliseconds % 1000 ) * 1000;
00074   }
00075 
00076   /////////////////////////////////////////////////////////////////////////
00077   //                                                                     //
00078   // IPC         :                                                       //
00079   //                                                                     //
00080   /////////////////////////////////////////////////////////////////////////
00081 
00082   friend ostream& operator < ( ostream& ostr, const Timer& T )
00083   {
00084     ostr < T.alarmSecs;
00085     ostr < T.alarmUSecs;
00086     return ostr;
00087   }
00088   
00089   friend istream& operator > ( istream& istr, Timer& T)
00090   {
00091     istr > T.alarmSecs;
00092     istr > T.alarmUSecs;
00093     return istr;
00094   }
00095 private:
00096 
00097   /////////////////////////////////////////////////////////////////////////
00098   //                                                                     //
00099   // Data Members:                                                       //
00100   //                                                                     //
00101   /////////////////////////////////////////////////////////////////////////
00102 
00103   long alarmSecs, alarmUSecs;
00104 
00105 };
00106 
00107 #endif

Generated at Tue Jun 19 09:49:35 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001