Time_Value.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Time_Value.inl,v 4.39 2006/03/23 03:43:20 john_c Exp
00004 
00005 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00006 
00007 #if defined (ACE_WIN32) && defined (_WIN32_WCE)
00008 // Something is a bit brain-damaged here and I'm not sure what... this code
00009 // compiled before the OS reorg for ACE 5.4. Since then it hasn't - eVC
00010 // complains that the operators that return ACE_Time_Value are C-linkage
00011 // functions that can't return a C++ class. The only way I've found to
00012 // defeat this is to wrap the whole class in extern "C++".
00013 //    - Steve Huston, 23-Aug-2004
00014 extern "C++" {
00015 #endif
00016 
00017 // Returns the value of the object as a timeval.
00018 
00019 ACE_INLINE
00020 ACE_Time_Value::operator timeval () const
00021 {
00022   // ACE_OS_TRACE ("ACE_Time_Value::operator timeval");
00023   return this->tv_;
00024 }
00025 
00026 ACE_INLINE void
00027 ACE_Time_Value::set (const timeval &tv)
00028 {
00029   // ACE_OS_TRACE ("ACE_Time_Value::set");
00030   this->tv_.tv_sec = tv.tv_sec;
00031   this->tv_.tv_usec = tv.tv_usec;
00032 
00033   this->normalize ();
00034 }
00035 
00036 ACE_INLINE
00037 ACE_Time_Value::ACE_Time_Value (const struct timeval &tv)
00038   // : tv_ ()
00039 {
00040   // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
00041   this->set (tv);
00042 }
00043 
00044 ACE_INLINE
00045 ACE_Time_Value::operator const timeval * () const
00046 {
00047   // ACE_OS_TRACE ("ACE_Time_Value::operator const timeval *");
00048   return (const timeval *) &this->tv_;
00049 }
00050 
00051 ACE_INLINE void
00052 ACE_Time_Value::set (time_t sec, suseconds_t usec)
00053 {
00054   // ACE_OS_TRACE ("ACE_Time_Value::set");
00055 #if defined (ACE_WIN64) \
00056     || (defined (ACE_WIN32) && !defined (_USE_32BIT_TIME_T))
00057   // Win64 uses 'long' (32 bit) timeval and 64-bit time_t, so we have
00058   // to get these back in range.
00059   if (sec > LONG_MAX)
00060     this->tv_.tv_sec = LONG_MAX;
00061   else if (sec < LONG_MIN)
00062     this->tv_.tv_sec = LONG_MIN;
00063   else
00064     this->tv_.tv_sec = static_cast<long> (sec);
00065 #else
00066   this->tv_.tv_sec = sec;
00067 #endif
00068   this->tv_.tv_usec = usec;
00069 #if __GNUC__
00070   if (__builtin_constant_p(sec) &&
00071       __builtin_constant_p(usec) &&
00072       (sec >= 0 && usec >= 0 && usec < ACE_ONE_SECOND_IN_USECS))
00073     return;
00074 #endif
00075   this->normalize ();
00076 }
00077 
00078 ACE_INLINE void
00079 ACE_Time_Value::set (double d)
00080 {
00081   // ACE_OS_TRACE ("ACE_Time_Value::set");
00082   long l = (long) d;
00083   this->tv_.tv_sec = l;
00084   this->tv_.tv_usec = (suseconds_t) ((d - (double) l) * ACE_ONE_SECOND_IN_USECS + .5);
00085   this->normalize ();
00086 }
00087 
00088 // Initializes a timespec_t.  Note that this approach loses precision
00089 // since it converts the nano-seconds into micro-seconds.  But then
00090 // again, do any real systems have nano-second timer precision?!
00091 
00092 ACE_INLINE void
00093 ACE_Time_Value::set (const timespec_t &tv)
00094 {
00095   // ACE_OS_TRACE ("ACE_Time_Value::set");
00096 
00097   this->set (tv.tv_sec,
00098              tv.tv_nsec / 1000); // Convert nanoseconds into microseconds.
00099 }
00100 
00101 ACE_INLINE
00102 ACE_Time_Value::ACE_Time_Value (void)
00103   // : tv_ ()
00104 {
00105   // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
00106   this->set (0, 0);
00107 }
00108 
00109 ACE_INLINE
00110 ACE_Time_Value::ACE_Time_Value (time_t sec, suseconds_t usec)
00111 {
00112   // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
00113   this->set (sec, usec);
00114 }
00115 
00116 // Returns number of seconds.
00117 
00118 ACE_INLINE time_t
00119 ACE_Time_Value::sec (void) const
00120 {
00121   // ACE_OS_TRACE ("ACE_Time_Value::sec");
00122   return this->tv_.tv_sec;
00123 }
00124 
00125 // Sets the number of seconds.
00126 
00127 ACE_INLINE void
00128 ACE_Time_Value::sec (time_t sec)
00129 {
00130   // ACE_OS_TRACE ("ACE_Time_Value::sec");
00131 #if defined (ACE_WIN64) \
00132   || (defined (ACE_WIN32) && !defined (_USE_32BIT_TIME_T))
00133   // Win64 uses 'long' (32 bit) timeval and 64-bit time_t, so we have
00134   // to get these back in range.
00135   if (sec > LONG_MAX)
00136     this->tv_.tv_sec = LONG_MAX;
00137   else if (sec < LONG_MIN)
00138     this->tv_.tv_sec = LONG_MIN;
00139   else
00140     this->tv_.tv_sec = static_cast<long> (sec);
00141 #else
00142   this->tv_.tv_sec = sec;
00143 #endif
00144 }
00145 
00146 // Converts from Time_Value format into milli-seconds format.
00147 
00148 ACE_INLINE unsigned long
00149 ACE_Time_Value::msec (void) const
00150 {
00151   // ACE_OS_TRACE ("ACE_Time_Value::msec");
00152   return this->tv_.tv_sec * 1000 + this->tv_.tv_usec / 1000;
00153 }
00154 
00155 #if !defined (ACE_LACKS_LONGLONG_T)
00156 ACE_INLINE void
00157 ACE_Time_Value::msec (ACE_UINT64 &ms) const
00158 {
00159   // ACE_OS_TRACE ("ACE_Time_Value::msec");
00160   ms = static_cast<ACE_UINT64> (this->tv_.tv_sec);
00161   ms *= 1000;
00162   ms += (this->tv_.tv_usec / 1000);
00163 }
00164 #endif /*ACE_LACKS_LONGLONG_T*/
00165 
00166 // Converts from milli-seconds format into Time_Value format.
00167 
00168 ACE_INLINE void
00169 ACE_Time_Value::msec (long milliseconds)
00170 {
00171   // ACE_OS_TRACE ("ACE_Time_Value::msec");
00172   // Convert millisecond units to seconds;
00173   this->tv_.tv_sec = milliseconds / 1000;
00174   // Convert remainder to microseconds;
00175   this->tv_.tv_usec = (milliseconds - (this->tv_.tv_sec * 1000)) * 1000;
00176 }
00177 
00178 // Returns number of micro-seconds.
00179 
00180 ACE_INLINE suseconds_t
00181 ACE_Time_Value::usec (void) const
00182 {
00183   // ACE_OS_TRACE ("ACE_Time_Value::usec");
00184   return this->tv_.tv_usec;
00185 }
00186 
00187 // Sets the number of micro-seconds.
00188 
00189 ACE_INLINE void
00190 ACE_Time_Value::usec (suseconds_t usec)
00191 {
00192   // ACE_OS_TRACE ("ACE_Time_Value::usec");
00193   this->tv_.tv_usec = usec;
00194 }
00195 
00196 #if !defined (ACE_LACKS_LONGLONG_T)
00197 ACE_INLINE void
00198 ACE_Time_Value::to_usec (ACE_UINT64 &usec) const
00199 {
00200   // ACE_OS_TRACE ("ACE_Time_Value::to_usec");
00201   usec = static_cast<ACE_UINT64> (this->tv_.tv_sec);
00202   usec *= 1000000;
00203   usec += this->tv_.tv_usec;
00204 }
00205 #endif /*ACE_LACKS_LONGLONG_T*/
00206 
00207 ACE_INLINE ACE_Time_Value
00208 operator * (double d, const ACE_Time_Value &tv)
00209 {
00210   return ACE_Time_Value (tv) *= d;
00211 }
00212 
00213 ACE_INLINE ACE_Time_Value
00214 operator * (const ACE_Time_Value &tv, double d)
00215 {
00216   return ACE_Time_Value (tv) *= d;
00217 }
00218 
00219 // True if tv1 > tv2.
00220 
00221 ACE_INLINE bool
00222 operator > (const ACE_Time_Value &tv1,
00223             const ACE_Time_Value &tv2)
00224 {
00225   // ACE_OS_TRACE ("operator >");
00226   if (tv1.sec () > tv2.sec ())
00227     return 1;
00228   else if (tv1.sec () == tv2.sec ()
00229            && tv1.usec () > tv2.usec ())
00230     return 1;
00231   else
00232     return 0;
00233 }
00234 
00235 // True if tv1 >= tv2.
00236 
00237 ACE_INLINE bool
00238 operator >= (const ACE_Time_Value &tv1,
00239              const ACE_Time_Value &tv2)
00240 {
00241   // ACE_OS_TRACE ("operator >=");
00242   if (tv1.sec () > tv2.sec ())
00243     return 1;
00244   else if (tv1.sec () == tv2.sec ()
00245            && tv1.usec () >= tv2.usec ())
00246     return 1;
00247   else
00248     return 0;
00249 }
00250 
00251 // Returns the value of the object as a timespec_t.
00252 
00253 ACE_INLINE
00254 ACE_Time_Value::operator timespec_t () const
00255 {
00256   // ACE_OS_TRACE ("ACE_Time_Value::operator timespec_t");
00257   timespec_t tv;
00258   tv.tv_sec = this->sec ();
00259   // Convert microseconds into nanoseconds.
00260   tv.tv_nsec = this->tv_.tv_usec * 1000;
00261   return tv;
00262 }
00263 
00264 // Initializes the ACE_Time_Value object from a timespec_t.
00265 
00266 ACE_INLINE
00267 ACE_Time_Value::ACE_Time_Value (const timespec_t &tv)
00268   // : tv_ ()
00269 {
00270   // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
00271   this->set (tv);
00272 }
00273 
00274 // True if tv1 < tv2.
00275 
00276 ACE_INLINE bool
00277 operator < (const ACE_Time_Value &tv1,
00278             const ACE_Time_Value &tv2)
00279 {
00280   // ACE_OS_TRACE ("operator <");
00281   return tv2 > tv1;
00282 }
00283 
00284 // True if tv1 >= tv2.
00285 
00286 ACE_INLINE bool
00287 operator <= (const ACE_Time_Value &tv1,
00288              const ACE_Time_Value &tv2)
00289 {
00290   // ACE_OS_TRACE ("operator <=");
00291   return tv2 >= tv1;
00292 }
00293 
00294 // True if tv1 == tv2.
00295 
00296 ACE_INLINE bool
00297 operator == (const ACE_Time_Value &tv1,
00298              const ACE_Time_Value &tv2)
00299 {
00300   // ACE_OS_TRACE ("operator ==");
00301   return tv1.sec () == tv2.sec ()
00302     && tv1.usec () == tv2.usec ();
00303 }
00304 
00305 // True if tv1 != tv2.
00306 
00307 ACE_INLINE bool
00308 operator != (const ACE_Time_Value &tv1,
00309              const ACE_Time_Value &tv2)
00310 {
00311   // ACE_OS_TRACE ("operator !=");
00312   return !(tv1 == tv2);
00313 }
00314 
00315 // Add TV to this.
00316 
00317 ACE_INLINE ACE_Time_Value &
00318 ACE_Time_Value::operator+= (const ACE_Time_Value &tv)
00319 {
00320   // ACE_OS_TRACE ("ACE_Time_Value::operator+=");
00321   this->sec (this->sec () + tv.sec ());
00322   this->usec (this->usec () + tv.usec ());
00323   this->normalize ();
00324   return *this;
00325 }
00326 
00327 ACE_INLINE ACE_Time_Value &
00328 ACE_Time_Value::operator+= (time_t tv)
00329 {
00330   // ACE_OS_TRACE ("ACE_Time_Value::operator+=");
00331   this->sec (this->sec () + tv);
00332   return *this;
00333 }
00334 
00335 ACE_INLINE ACE_Time_Value &
00336 ACE_Time_Value::operator= (const ACE_Time_Value &tv)
00337 {
00338   // ACE_OS_TRACE ("ACE_Time_Value::operator=");
00339   this->sec (tv.sec ());
00340   this->usec (tv.usec ());
00341   return *this;
00342 }
00343 
00344 ACE_INLINE ACE_Time_Value &
00345 ACE_Time_Value::operator= (time_t tv)
00346 {
00347   // ACE_OS_TRACE ("ACE_Time_Value::operator=");
00348   this->sec (tv);
00349   this->usec (0);
00350   return *this;
00351 }
00352 
00353 // Subtract TV to this.
00354 
00355 ACE_INLINE ACE_Time_Value &
00356 ACE_Time_Value::operator-= (const ACE_Time_Value &tv)
00357 {
00358   // ACE_OS_TRACE ("ACE_Time_Value::operator-=");
00359   this->sec (this->sec () - tv.sec ());
00360   this->usec (this->usec () - tv.usec ());
00361   this->normalize ();
00362   return *this;
00363 }
00364 
00365 ACE_INLINE ACE_Time_Value &
00366 ACE_Time_Value::operator-= (time_t tv)
00367 {
00368   // ACE_OS_TRACE ("ACE_Time_Value::operator-=");
00369   this->sec (this->sec () - tv);
00370   return *this;
00371 }
00372 
00373 // Adds two ACE_Time_Value objects together, returns the sum.
00374 
00375 ACE_INLINE ACE_Time_Value
00376 operator + (const ACE_Time_Value &tv1,
00377             const ACE_Time_Value &tv2)
00378 {
00379   // ACE_OS_TRACE ("operator +");
00380   ACE_Time_Value sum (tv1);
00381   sum += tv2;
00382 
00383   return sum;
00384 }
00385 
00386 // Subtracts two ACE_Time_Value objects, returns the difference.
00387 
00388 ACE_INLINE ACE_Time_Value
00389 operator - (const ACE_Time_Value &tv1,
00390             const ACE_Time_Value &tv2)
00391 {
00392   // ACE_OS_TRACE ("operator -");
00393   ACE_Time_Value delta (tv1);
00394   delta -= tv2;
00395 
00396   return delta;
00397 }
00398 
00399 #if defined (ACE_WIN32) && defined (_WIN32_WCE)
00400 }
00401 #endif
00402 
00403 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 09:42:07 2006 for ACE by doxygen 1.3.6