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