00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Time_Value.h 00006 * 00007 * $Id: Time_Value.h 90683 2010-06-17 22:07:42Z shuston $ 00008 * 00009 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00010 */ 00011 //============================================================================= 00012 00013 #ifndef ACE_TIME_VALUE_H 00014 #define ACE_TIME_VALUE_H 00015 00016 #include /**/ "ace/pre.h" 00017 00018 #include /**/ "ace/ACE_export.h" 00019 00020 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00021 # pragma once 00022 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00023 00024 # include "ace/os_include/os_time.h" 00025 00026 // Define some helpful constants. 00027 // Not type-safe, and signed. For backward compatibility. 00028 #define ACE_ONE_SECOND_IN_MSECS 1000L 00029 suseconds_t const ACE_ONE_SECOND_IN_USECS = 1000000; 00030 #define ACE_ONE_SECOND_IN_NSECS 1000000000L 00031 00032 // needed for ACE_UINT64 00033 #include "ace/Basic_Types.h" 00034 00035 // needed to determine if iostreams are present 00036 #include "ace/iosfwd.h" 00037 00038 // This forward declaration is needed by the set() and FILETIME() functions 00039 #if defined (ACE_LACKS_LONGLONG_T) 00040 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00041 class ACE_Export ACE_U_LongLong; 00042 ACE_END_VERSIONED_NAMESPACE_DECL 00043 #endif /* ACE_LACKS_LONGLONG_T */ 00044 00045 // ------------------------------------------------------------------- 00046 00047 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00048 00049 00050 /** 00051 * @class ACE_Time_Value 00052 * 00053 * @brief Operations on "timeval" structures, which express time in 00054 * seconds (secs) and microseconds (usecs). 00055 * 00056 * This class centralizes all the time related processing in 00057 * ACE. These time values are typically used in conjunction with OS 00058 * mechanisms like <select>, <poll>, or <cond_timedwait>. 00059 */ 00060 class ACE_Export ACE_Time_Value 00061 { 00062 public: 00063 00064 /// Constant "0". 00065 static const ACE_Time_Value zero; 00066 00067 /** 00068 * Constant for maximum time representable. Note that this time is 00069 * not intended for use with <select> or other calls that may have 00070 * *their own* implementation-specific maximum time representations. 00071 * Its primary use is in time computations such as those used by the 00072 * dynamic subpriority strategies in the ACE_Dynamic_Message_Queue 00073 * class. 00074 */ 00075 static const ACE_Time_Value max_time; 00076 00077 // = Initialization methods. 00078 00079 /// Default Constructor. 00080 ACE_Time_Value (void); 00081 00082 /// Constructor. 00083 explicit ACE_Time_Value (time_t sec, suseconds_t usec = 0); 00084 00085 // = Methods for converting to/from various time formats. 00086 00087 /// Construct the ACE_Time_Value from a timeval. 00088 explicit ACE_Time_Value (const struct timeval &t); 00089 00090 /// Construct the ACE_Time_Value object from a timespec_t. 00091 explicit ACE_Time_Value (const timespec_t &t); 00092 00093 # if defined (ACE_WIN32) 00094 /// Construct the ACE_Time_Value object from a Win32 FILETIME 00095 explicit ACE_Time_Value (const FILETIME &ft); 00096 # endif /* ACE_WIN32 */ 00097 00098 /// Initializes the ACE_Time_Value from seconds and useconds. 00099 void set (time_t sec, suseconds_t usec); 00100 00101 /// Initializes the ACE_Time_Value from a double, which is assumed to be 00102 /// in second format, with any remainder treated as microseconds. 00103 void set (double d); 00104 00105 /// Initializes the ACE_Time_Value from a timeval. 00106 void set (const timeval &t); 00107 00108 /// Initializes the ACE_Time_Value object from a timespec_t. 00109 void set (const timespec_t &t); 00110 00111 # if defined (ACE_WIN32) 00112 /// Initializes the ACE_Time_Value object from a Win32 FILETIME. 00113 void set (const FILETIME &ft); 00114 # endif /* ACE_WIN32 */ 00115 00116 /// Converts from ACE_Time_Value format into milliseconds format. 00117 /** 00118 * @return Sum of second field (in milliseconds) and microsecond field 00119 * (in milliseconds). Note that this method can overflow if 00120 * the second and microsecond field values are large, so use 00121 * the msec (ACE_UINT64 &ms) method instead. 00122 * 00123 * @note The semantics of this method differs from the sec() and 00124 * usec() methods. There is no analogous "millisecond" 00125 * component in an ACE_Time_Value. 00126 */ 00127 unsigned long msec (void) const; 00128 00129 /// Converts from ACE_Time_Value format into milliseconds format. 00130 /** 00131 * @return Sum of second field (in milliseconds) and microsecond field 00132 * (in milliseconds). 00133 * 00134 * @note The semantics of this method differs from the sec() and 00135 * usec() methods. There is no analogous "millisecond" 00136 * component in an ACE_Time_Value. 00137 */ 00138 ACE_UINT64 get_msec () const; 00139 00140 /// Converts from ACE_Time_Value format into milliseconds format. 00141 /** 00142 * @return Sum of second field (in milliseconds) and microsecond field 00143 * (in milliseconds) and return them via the @param ms parameter. 00144 * 00145 * @note The semantics of this method differs from the sec() and 00146 * usec() methods. There is no analogous "millisecond" 00147 * component in an ACE_Time_Value. 00148 * 00149 * @deprecated Use get_msec() instead. 00150 */ 00151 void msec (ACE_UINT64 &ms) const; 00152 00153 /// Converts from ACE_Time_Value format into milliseconds format. 00154 /** 00155 * @return Sum of second field (in milliseconds) and microsecond field 00156 * (in milliseconds) and return them via the @param ms parameter. 00157 * 00158 * @note The semantics of this method differs from the sec() and 00159 * usec() methods. There is no analogous "millisecond" 00160 * component in an ACE_Time_Value. 00161 * 00162 * @deprecated Use get_msec() instead. 00163 */ 00164 void msec (ACE_UINT64 &ms) /* const */; 00165 00166 /// Converts from milli-seconds format into ACE_Time_Value format. 00167 /** 00168 * @note The semantics of this method differs from the sec() and 00169 * usec() methods. There is no analogous "millisecond" 00170 * component in an ACE_Time_Value. 00171 */ 00172 void set_msec (const ACE_UINT64 &ms); 00173 00174 /// Converts from milli-seconds format into ACE_Time_Value format. 00175 /** 00176 * @note The semantics of this method differs from the sec() and 00177 * usec() methods. There is no analogous "millisecond" 00178 * component in an ACE_Time_Value. 00179 */ 00180 void msec (long); 00181 00182 /// Converts from milli-seconds format into ACE_Time_Value format. 00183 /** 00184 * @note The semantics of this method differs from the sec() and 00185 * usec() methods. There is no analogous "millisecond" 00186 * component in an ACE_Time_Value. 00187 */ 00188 void msec (int); // converted to long then calls above. 00189 00190 /// Returns the value of the object as a timespec_t. 00191 operator timespec_t () const; 00192 00193 /// Returns the value of the object as a timeval. 00194 operator timeval () const; 00195 00196 /// Returns a pointer to the object as a timeval. 00197 operator const timeval *() const; 00198 00199 # if defined (ACE_WIN32) 00200 /// Returns the value of the object as a Win32 FILETIME. 00201 operator FILETIME () const; 00202 # endif /* ACE_WIN32 */ 00203 00204 // = The following are accessor/mutator methods. 00205 00206 /// Get seconds. 00207 /** 00208 * @return The second field/component of this ACE_Time_Value. 00209 * 00210 * @note The semantics of this method differs from the msec() 00211 * method. 00212 */ 00213 time_t sec (void) const; 00214 00215 /// Set seconds. 00216 void sec (time_t sec); 00217 00218 /// Get microseconds. 00219 /** 00220 * @return The microsecond field/component of this ACE_Time_Value. 00221 * 00222 * @note The semantics of this method differs from the msec() 00223 * method. 00224 */ 00225 suseconds_t usec (void) const; 00226 00227 /// Set microseconds. 00228 void usec (suseconds_t usec); 00229 00230 /** 00231 * @return Sum of second field (in microseconds) and microsecond field 00232 * and return them via the @param usec parameter. 00233 */ 00234 void to_usec (ACE_UINT64 &usec) const; 00235 00236 // = The following arithmetic methods operate on ACE_Time_Value's. 00237 00238 /// Add @a tv to this. 00239 ACE_Time_Value &operator += (const ACE_Time_Value &tv); 00240 00241 /// Add @a tv to this. 00242 ACE_Time_Value &operator += (time_t tv); 00243 00244 /// Assign @ tv to this 00245 ACE_Time_Value &operator = (const ACE_Time_Value &tv); 00246 00247 /// Assign @ tv to this 00248 ACE_Time_Value &operator = (time_t tv); 00249 00250 /// Subtract @a tv to this. 00251 ACE_Time_Value &operator -= (const ACE_Time_Value &tv); 00252 00253 /// Substract @a tv to this. 00254 ACE_Time_Value &operator -= (time_t tv); 00255 00256 /** 00257 \brief Multiply the time value by the @a d factor. 00258 \note The result of the operator is valid for results from range 00259 < (ACE_INT32_MIN, -999999), (ACE_INT32_MAX, 999999) >. Result 00260 outside this range are saturated to a limit. 00261 */ 00262 ACE_Time_Value &operator *= (double d); 00263 00264 /// Increment microseconds as postfix. 00265 /** 00266 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00267 * with ACE_Time_Value. 00268 */ 00269 ACE_Time_Value operator++ (int); 00270 00271 /// Increment microseconds as prefix. 00272 /** 00273 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00274 * with ACE_Time_Value. 00275 */ 00276 ACE_Time_Value &operator++ (void); 00277 00278 /// Decrement microseconds as postfix. 00279 /** 00280 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00281 * with ACE_Time_Value. 00282 */ 00283 ACE_Time_Value operator-- (int); 00284 00285 /// Decrement microseconds as prefix. 00286 /** 00287 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00288 * with ACE_Time_Value. 00289 */ 00290 ACE_Time_Value &operator-- (void); 00291 00292 /// Adds two ACE_Time_Value objects together, returns the sum. 00293 friend ACE_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1, 00294 const ACE_Time_Value &tv2); 00295 00296 /// Subtracts two ACE_Time_Value objects, returns the difference. 00297 friend ACE_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1, 00298 const ACE_Time_Value &tv2); 00299 00300 /// True if @a tv1 < @a tv2. 00301 friend ACE_Export bool operator < (const ACE_Time_Value &tv1, 00302 const ACE_Time_Value &tv2); 00303 00304 /// True if @a tv1 > @a tv2. 00305 friend ACE_Export bool operator > (const ACE_Time_Value &tv1, 00306 const ACE_Time_Value &tv2); 00307 00308 /// True if @a tv1 <= @a tv2. 00309 friend ACE_Export bool operator <= (const ACE_Time_Value &tv1, 00310 const ACE_Time_Value &tv2); 00311 00312 /// True if @a tv1 >= @a tv2. 00313 friend ACE_Export bool operator >= (const ACE_Time_Value &tv1, 00314 const ACE_Time_Value &tv2); 00315 00316 /// True if @a tv1 == @a tv2. 00317 friend ACE_Export bool operator == (const ACE_Time_Value &tv1, 00318 const ACE_Time_Value &tv2); 00319 00320 /// True if @a tv1 != @a tv2. 00321 friend ACE_Export bool operator != (const ACE_Time_Value &tv1, 00322 const ACE_Time_Value &tv2); 00323 00324 //@{ 00325 /// Multiplies the time value by @a d 00326 friend ACE_Export ACE_Time_Value operator * (double d, 00327 const ACE_Time_Value &tv); 00328 00329 friend ACE_Export ACE_Time_Value operator * (const ACE_Time_Value &tv, 00330 double d); 00331 //@} 00332 00333 /// Dump is a no-op. 00334 /** 00335 * The dump() method is a no-op. It's here for backwards compatibility 00336 * only, but does not dump anything. Invoking logging methods here 00337 * violates layering restrictions in ACE because this class is part 00338 * of the OS layer and @c ACE_Log_Msg is at a higher level. 00339 */ 00340 void dump (void) const; 00341 00342 # if defined (ACE_WIN32) 00343 /// Const time difference between FILETIME and POSIX time. 00344 # if defined (ACE_LACKS_LONGLONG_T) 00345 static const ACE_U_LongLong FILETIME_to_timval_skew; 00346 # else 00347 static const DWORDLONG FILETIME_to_timval_skew; 00348 # endif // ACE_LACKS_LONGLONG_T 00349 # endif /* ACE_WIN32 */ 00350 00351 private: 00352 /// Put the timevalue into a canonical form. 00353 void normalize (bool saturate = false); 00354 00355 /// Store the values as a timeval. 00356 #if defined (ACE_HAS_TIME_T_LONG_MISMATCH) 00357 // Windows' timeval is non-conformant, so swap in a struct that conforms 00358 // to the proper data types to represent the entire time range that this 00359 // class's API can accept. 00360 // Also, since this class can supply a pointer to a timeval that things 00361 // like select() expect, we need the OS-defined one as well. To make this 00362 // available, use a real timeval called ext_tv_ and set it up when needed. 00363 // Since this is most often for relative times that don't approach 32 bits 00364 // in size, reducing a time_t to fit should be no problem. 00365 struct { 00366 time_t tv_sec; 00367 suseconds_t tv_usec; 00368 } tv_; 00369 timeval ext_tv_; 00370 #else 00371 timeval tv_; 00372 #endif /* ACE_HAS_TIME_T_LONG_MISMATCH */ 00373 }; 00374 00375 #ifdef ACE_HAS_CPP98_IOSTREAMS 00376 extern ACE_Export ostream &operator<<( ostream &o, const ACE_Time_Value &v ); 00377 #endif 00378 00379 ACE_END_VERSIONED_NAMESPACE_DECL 00380 00381 #if defined (__ACE_INLINE__) 00382 #include "ace/Time_Value.inl" 00383 #endif /* __ACE_INLINE__ */ 00384 00385 #if defined (__MINGW32__) 00386 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00387 // The MingW linker has problems with the exported statics 00388 // zero and max_time with these two statics the linker will be able to 00389 // resolve the static exported symbols. 00390 static const ACE_Time_Value& __zero_time = ACE_Time_Value::zero; 00391 static const ACE_Time_Value& __max_time = ACE_Time_Value::max_time; 00392 ACE_END_VERSIONED_NAMESPACE_DECL 00393 #endif /* __MINGW32__ */ 00394 00395 #include /**/ "ace/post.h" 00396 00397 #endif /* ACE_TIME_VALUE_H */