Time_Value.inl

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

Generated on Sun Jan 27 12:05:41 2008 for ACE by doxygen 1.3.6