00001 #ifndef __CHRONO_HPP__
00002 #define __CHRONO_HPP__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 #ifndef __cplusplus
00055 #error Must use C++ compiler
00056 #endif
00057
00058
00059
00060 #include "arch.h"
00061
00062 #define SYNC_CHRONO
00063
00064
00065
00066
00067 class Chrono {
00068 private :
00069 unsigned long _ms;
00070 int _on;
00071
00072 inline unsigned long timer_ms( void );
00073
00074 public :
00075 Chrono( void );
00076
00077 void reset( void );
00078 void start( void );
00079 void stop( void );
00080 void restart( void );
00081 void restop( void );
00082
00083 int on( void );
00084 unsigned long ms( void );
00085
00086 };
00087
00088
00089
00090
00091 #if !defined(__OS_MSDOS__) && !defined(__OS_WIN31__)
00092 #include <stdlib.h>
00093 #include <time.h>
00094 #endif
00095
00096 inline unsigned long Chrono::timer_ms( void )
00097 {
00098 #if defined(__OS_MSDOS__) || defined(__OS_WIN31__)
00099 static unsigned long i=0;
00100 i = (*((unsigned long far *)((void _seg *)0x0000+(void near *)0x046C)))*55;
00101 return i;
00102 #else
00103 return time(NULL)*1000;
00104 #endif
00105 }
00106
00107 inline Chrono::Chrono( void )
00108 {
00109 _ms = _on = 0;
00110 }
00111
00112 inline void Chrono::reset( void )
00113 {
00114 if (_on)
00115 _ms = timer_ms();
00116 else
00117 _ms = 0;
00118 }
00119
00120 inline void Chrono::start( void )
00121 {
00122 if (!_on) {
00123 #ifdef SYNC_CHRONO
00124 unsigned long ot = timer_ms();
00125 while (ot==timer_ms()) {};
00126 #endif
00127 _ms = timer_ms() - _ms;
00128 _on = 1;
00129 }
00130 }
00131
00132 inline void Chrono::stop( void )
00133 {
00134 if (_on) {
00135 _ms = timer_ms() - _ms;
00136 _on = 0;
00137 }
00138 }
00139
00140 inline void Chrono::restart( void )
00141 {
00142 reset();
00143 start();
00144 }
00145
00146 inline void Chrono::restop( void )
00147 {
00148 stop();
00149 reset();
00150 }
00151
00152 inline int Chrono::on( void )
00153 {
00154 return _on;
00155 }
00156
00157 inline unsigned long Chrono::ms( void )
00158 {
00159 return ( (_on) ? timer_ms() - _ms : _ms);
00160 }
00161
00162
00163
00164 #endif