Time_Value.inl

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // $Id: Time_Value.inl 80826 2008-03-04 14:51:23Z wotte $
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 ACE_INLINE void
00155 ACE_Time_Value::msec (ACE_UINT64 &ms) const
00156 {
00157   // ACE_OS_TRACE ("ACE_Time_Value::msec");
00158   ms = ACE_Utils::truncate_cast<ACE_UINT64> (this->tv_.tv_sec);
00159   ms *= 1000;
00160   ms += (this->tv_.tv_usec / 1000);
00161 }
00162 
00163 // Converts from milli-seconds format into Time_Value format.
00164 
00165 ACE_INLINE void
00166 ACE_Time_Value::msec (long milliseconds)
00167 {
00168   // ACE_OS_TRACE ("ACE_Time_Value::msec");
00169   // Convert millisecond units to seconds;
00170   long secs = milliseconds / 1000;
00171   this->tv_.tv_sec = secs;
00172   // Convert remainder to microseconds;
00173   this->tv_.tv_usec = (milliseconds - (secs * 1000)) * 1000;
00174 }
00175 
00176 // Returns number of micro-seconds.
00177 
00178 ACE_INLINE suseconds_t
00179 ACE_Time_Value::usec (void) const
00180 {
00181   // ACE_OS_TRACE ("ACE_Time_Value::usec");
00182   return this->tv_.tv_usec;
00183 }
00184 
00185 // Sets the number of micro-seconds.
00186 
00187 ACE_INLINE void
00188 ACE_Time_Value::usec (suseconds_t usec)
00189 {
00190   // ACE_OS_TRACE ("ACE_Time_Value::usec");
00191   this->tv_.tv_usec = usec;
00192 }
00193 
00194 ACE_INLINE void
00195 ACE_Time_Value::to_usec (ACE_UINT64 & usec) const
00196 {
00197   // ACE_OS_TRACE ("ACE_Time_Value::to_usec");
00198 
00199 #if defined (ACE_LACKS_UNSIGNEDLONGLONG_T)
00200   usec = ACE_U_LongLong (static_cast<long long> (this->tv_.tv_sec));
00201 #elif defined (ACE_LACKS_LONGLONG_T)
00202   // No native 64-bit type, meaning time_t is most likely 32 bits.
00203   usec = ACE_U_LongLong (this->tv_.tv_sec);
00204 #else
00205   usec = static_cast<ACE_UINT64> (this->tv_.tv_sec);
00206 #endif  /* ACE_LACKS_LONG_LONG_T */
00207   usec *= 1000000;
00208   usec += this->tv_.tv_usec;
00209 }
00210 
00211 ACE_INLINE ACE_Time_Value
00212 operator * (double d, const ACE_Time_Value &tv)
00213 {
00214   return ACE_Time_Value (tv) *= d;
00215 }
00216 
00217 ACE_INLINE ACE_Time_Value
00218 operator * (const ACE_Time_Value &tv, double d)
00219 {
00220   return ACE_Time_Value (tv) *= d;
00221 }
00222 
00223 // True if tv1 > tv2.
00224 
00225 ACE_INLINE bool
00226 operator > (const ACE_Time_Value &tv1,
00227             const ACE_Time_Value &tv2)
00228 {
00229   // ACE_OS_TRACE ("operator >");
00230   if (tv1.sec () > tv2.sec ())
00231     return 1;
00232   else if (tv1.sec () == tv2.sec ()
00233            && tv1.usec () > tv2.usec ())
00234     return 1;
00235   else
00236     return 0;
00237 }
00238 
00239 // True if tv1 >= tv2.
00240 
00241 ACE_INLINE bool
00242 operator >= (const ACE_Time_Value &tv1,
00243              const ACE_Time_Value &tv2)
00244 {
00245   // ACE_OS_TRACE ("operator >=");
00246   if (tv1.sec () > tv2.sec ())
00247     return 1;
00248   else if (tv1.sec () == tv2.sec ()
00249            && tv1.usec () >= tv2.usec ())
00250     return 1;
00251   else
00252     return 0;
00253 }
00254 
00255 // Returns the value of the object as a timespec_t.
00256 
00257 ACE_INLINE
00258 ACE_Time_Value::operator timespec_t () const
00259 {
00260   // ACE_OS_TRACE ("ACE_Time_Value::operator timespec_t");
00261   timespec_t tv;
00262   tv.tv_sec = this->sec ();
00263   // Convert microseconds into nanoseconds.
00264   tv.tv_nsec = this->tv_.tv_usec * 1000;
00265   return tv;
00266 }
00267 
00268 // Initializes the ACE_Time_Value object from a timespec_t.
00269 
00270 ACE_INLINE
00271 ACE_Time_Value::ACE_Time_Value (const timespec_t &tv)
00272   // : tv_ ()
00273 {
00274   // ACE_OS_TRACE ("ACE_Time_Value::ACE_Time_Value");
00275   this->set (tv);
00276 }
00277 
00278 // True if tv1 < tv2.
00279 
00280 ACE_INLINE bool
00281 operator < (const ACE_Time_Value &tv1,
00282             const ACE_Time_Value &tv2)
00283 {
00284   // ACE_OS_TRACE ("operator <");
00285   return tv2 > tv1;
00286 }
00287 
00288 // True if tv1 >= tv2.
00289 
00290 ACE_INLINE bool
00291 operator <= (const ACE_Time_Value &tv1,
00292              const ACE_Time_Value &tv2)
00293 {
00294   // ACE_OS_TRACE ("operator <=");
00295   return tv2 >= tv1;
00296 }
00297 
00298 // True if tv1 == tv2.
00299 
00300 ACE_INLINE bool
00301 operator == (const ACE_Time_Value &tv1,
00302              const ACE_Time_Value &tv2)
00303 {
00304   // ACE_OS_TRACE ("operator ==");
00305   return tv1.sec () == tv2.sec ()
00306     && tv1.usec () == tv2.usec ();
00307 }
00308 
00309 // True if tv1 != tv2.
00310 
00311 ACE_INLINE bool
00312 operator != (const ACE_Time_Value &tv1,
00313              const ACE_Time_Value &tv2)
00314 {
00315   // ACE_OS_TRACE ("operator !=");
00316   return !(tv1 == tv2);
00317 }
00318 
00319 // Add TV to this.
00320 
00321 ACE_INLINE ACE_Time_Value &
00322 ACE_Time_Value::operator+= (const ACE_Time_Value &tv)
00323 {
00324   // ACE_OS_TRACE ("ACE_Time_Value::operator+=");
00325   this->sec (this->sec () + tv.sec ());
00326   this->usec (this->usec () + tv.usec ());
00327   this->normalize ();
00328   return *this;
00329 }
00330 
00331 ACE_INLINE ACE_Time_Value &
00332 ACE_Time_Value::operator+= (time_t tv)
00333 {
00334   // ACE_OS_TRACE ("ACE_Time_Value::operator+=");
00335   this->sec (this->sec () + tv);
00336   return *this;
00337 }
00338 
00339 ACE_INLINE ACE_Time_Value &
00340 ACE_Time_Value::operator= (const ACE_Time_Value &tv)
00341 {
00342   // ACE_OS_TRACE ("ACE_Time_Value::operator=");
00343   this->sec (tv.sec ());
00344   this->usec (tv.usec ());
00345   return *this;
00346 }
00347 
00348 ACE_INLINE ACE_Time_Value &
00349 ACE_Time_Value::operator= (time_t tv)
00350 {
00351   // ACE_OS_TRACE ("ACE_Time_Value::operator=");
00352   this->sec (tv);
00353   this->usec (0);
00354   return *this;
00355 }
00356 
00357 // Subtract TV to this.
00358 
00359 ACE_INLINE ACE_Time_Value &
00360 ACE_Time_Value::operator-= (const ACE_Time_Value &tv)
00361 {
00362   // ACE_OS_TRACE ("ACE_Time_Value::operator-=");
00363   this->sec (this->sec () - tv.sec ());
00364   this->usec (this->usec () - tv.usec ());
00365   this->normalize ();
00366   return *this;
00367 }
00368 
00369 ACE_INLINE ACE_Time_Value &
00370 ACE_Time_Value::operator-= (time_t tv)
00371 {
00372   // ACE_OS_TRACE ("ACE_Time_Value::operator-=");
00373   this->sec (this->sec () - tv);
00374   return *this;
00375 }
00376 
00377 // Adds two ACE_Time_Value objects together, returns the sum.
00378 
00379 ACE_INLINE ACE_Time_Value
00380 operator + (const ACE_Time_Value &tv1,
00381             const ACE_Time_Value &tv2)
00382 {
00383   // ACE_OS_TRACE ("operator +");
00384   ACE_Time_Value sum (tv1);
00385   sum += tv2;
00386 
00387   return sum;
00388 }
00389 
00390 // Subtracts two ACE_Time_Value objects, returns the difference.
00391 
00392 ACE_INLINE ACE_Time_Value
00393 operator - (const ACE_Time_Value &tv1,
00394             const ACE_Time_Value &tv2)
00395 {
00396   // ACE_OS_TRACE ("operator -");
00397   ACE_Time_Value delta (tv1);
00398   delta -= tv2;
00399 
00400   return delta;
00401 }
00402 
00403 #if defined (ACE_WIN32) && defined (_WIN32_WCE)
00404 }
00405 #endif
00406 
00407 ACE_END_VERSIONED_NAMESPACE_DECL

Generated on Tue Feb 2 17:18:43 2010 for ACE by  doxygen 1.4.7