00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Time_Value.h 00006 * 00007 * $Id: Time_Value.h 80826 2008-03-04 14:51:23Z wotte $ 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 /** 00136 * @return Sum of second field (in milliseconds) and microsecond field 00137 * (in milliseconds) and return them via the @param ms parameter. 00138 * 00139 * @note The semantics of this method differs from the sec() and 00140 * usec() methods. There is no analogous "millisecond" 00141 * component in an ACE_Time_Value. 00142 */ 00143 void msec (ACE_UINT64 &ms) const; 00144 00145 /// Converts from milli-seconds format into ACE_Time_Value format. 00146 /** 00147 * @note The semantics of this method differs from the sec() and 00148 * usec() methods. There is no analogous "millisecond" 00149 * component in an ACE_Time_Value. 00150 */ 00151 void msec (long); 00152 00153 /// Returns the value of the object as a timespec_t. 00154 operator timespec_t () const; 00155 00156 /// Returns the value of the object as a timeval. 00157 operator timeval () const; 00158 00159 /// Returns a pointer to the object as a timeval. 00160 operator const timeval *() const; 00161 00162 # if defined (ACE_WIN32) 00163 /// Returns the value of the object as a Win32 FILETIME. 00164 operator FILETIME () const; 00165 # endif /* ACE_WIN32 */ 00166 00167 // = The following are accessor/mutator methods. 00168 00169 /// Get seconds. 00170 /** 00171 * @return The second field/component of this ACE_Time_Value. 00172 * 00173 * @note The semantics of this method differs from the msec() 00174 * method. 00175 */ 00176 time_t sec (void) const; 00177 00178 /// Set seconds. 00179 void sec (time_t sec); 00180 00181 /// Get microseconds. 00182 /** 00183 * @return The microsecond field/component of this ACE_Time_Value. 00184 * 00185 * @note The semantics of this method differs from the msec() 00186 * method. 00187 */ 00188 suseconds_t usec (void) const; 00189 00190 /// Set microseconds. 00191 void usec (suseconds_t usec); 00192 00193 /** 00194 * @return Sum of second field (in microseconds) and microsecond field 00195 * and return them via the @param usec parameter. 00196 */ 00197 void to_usec (ACE_UINT64 &usec) const; 00198 00199 // = The following arithmetic methods operate on ACE_Time_Value's. 00200 00201 /// Add @a tv to this. 00202 ACE_Time_Value &operator += (const ACE_Time_Value &tv); 00203 00204 /// Add @a tv to this. 00205 ACE_Time_Value &operator += (time_t tv); 00206 00207 /// Assign @ tv to this 00208 ACE_Time_Value &operator = (const ACE_Time_Value &tv); 00209 00210 /// Assign @ tv to this 00211 ACE_Time_Value &operator = (time_t tv); 00212 00213 /// Subtract @a tv to this. 00214 ACE_Time_Value &operator -= (const ACE_Time_Value &tv); 00215 00216 /// Substract @a tv to this. 00217 ACE_Time_Value &operator -= (time_t tv); 00218 00219 /** 00220 \brief Multiply the time value by the @a d factor. 00221 \note The result of the operator is valid for results from range 00222 < (ACE_INT32_MIN, -999999), (ACE_INT32_MAX, 999999) >. Result 00223 outside this range are saturated to a limit. 00224 */ 00225 ACE_Time_Value &operator *= (double d); 00226 00227 /// Increment microseconds as postfix. 00228 /** 00229 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00230 * with ACE_Time_Value. 00231 */ 00232 ACE_Time_Value operator++ (int); 00233 00234 /// Increment microseconds as prefix. 00235 /** 00236 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00237 * with ACE_Time_Value. 00238 */ 00239 ACE_Time_Value &operator++ (void); 00240 00241 /// Decrement microseconds as postfix. 00242 /** 00243 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00244 * with ACE_Time_Value. 00245 */ 00246 ACE_Time_Value operator-- (int); 00247 00248 /// Decrement microseconds as prefix. 00249 /** 00250 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00251 * with ACE_Time_Value. 00252 */ 00253 ACE_Time_Value &operator-- (void); 00254 00255 /// Adds two ACE_Time_Value objects together, returns the sum. 00256 friend ACE_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1, 00257 const ACE_Time_Value &tv2); 00258 00259 /// Subtracts two ACE_Time_Value objects, returns the difference. 00260 friend ACE_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1, 00261 const ACE_Time_Value &tv2); 00262 00263 /// True if @a tv1 < @a tv2. 00264 friend ACE_Export bool operator < (const ACE_Time_Value &tv1, 00265 const ACE_Time_Value &tv2); 00266 00267 /// True if @a tv1 > @a tv2. 00268 friend ACE_Export bool operator > (const ACE_Time_Value &tv1, 00269 const ACE_Time_Value &tv2); 00270 00271 /// True if @a tv1 <= @a tv2. 00272 friend ACE_Export bool operator <= (const ACE_Time_Value &tv1, 00273 const ACE_Time_Value &tv2); 00274 00275 /// True if @a tv1 >= @a tv2. 00276 friend ACE_Export bool operator >= (const ACE_Time_Value &tv1, 00277 const ACE_Time_Value &tv2); 00278 00279 /// True if @a tv1 == @a tv2. 00280 friend ACE_Export bool operator == (const ACE_Time_Value &tv1, 00281 const ACE_Time_Value &tv2); 00282 00283 /// True if @a tv1 != @a tv2. 00284 friend ACE_Export bool operator != (const ACE_Time_Value &tv1, 00285 const ACE_Time_Value &tv2); 00286 00287 //@{ 00288 /// Multiplies the time value by @a d 00289 friend ACE_Export ACE_Time_Value operator * (double d, 00290 const ACE_Time_Value &tv); 00291 00292 friend ACE_Export ACE_Time_Value operator * (const ACE_Time_Value &tv, 00293 double d); 00294 //@} 00295 00296 /// Dump is a no-op. 00297 /** 00298 * The dump() method is a no-op. It's here for backwards compatibility 00299 * only, but does not dump anything. Invoking logging methods here 00300 * violates layering restrictions in ACE because this class is part 00301 * of the OS layer and @c ACE_Log_Msg is at a higher level. 00302 */ 00303 void dump (void) const; 00304 00305 # if defined (ACE_WIN32) 00306 /// Const time difference between FILETIME and POSIX time. 00307 # if defined (ACE_LACKS_LONGLONG_T) 00308 static const ACE_U_LongLong FILETIME_to_timval_skew; 00309 # else 00310 static const DWORDLONG FILETIME_to_timval_skew; 00311 # endif // ACE_LACKS_LONGLONG_T 00312 # endif /* ACE_WIN32 */ 00313 00314 private: 00315 /// Put the timevalue into a canonical form. 00316 void normalize (void); 00317 00318 /// Store the values as a timeval. 00319 #if defined (ACE_HAS_TIME_T_LONG_MISMATCH) 00320 // Windows' timeval is non-conformant, so swap in a struct that conforms 00321 // to the proper data types to represent the entire time range that this 00322 // class's API can accept. 00323 // Also, since this class can supply a pointer to a timeval that things 00324 // like select() expect, we need the OS-defined one as well. To make this 00325 // available, use a real timeval called ext_tv_ and set it up when needed. 00326 // Since this is most often for relative times that don't approach 32 bits 00327 // in size, reducing a time_t to fit should be no problem. 00328 struct { 00329 time_t tv_sec; 00330 suseconds_t tv_usec; 00331 } tv_; 00332 timeval ext_tv_; 00333 #else 00334 timeval tv_; 00335 #endif /* ACE_HAS_TIME_T_LONG_MISMATCH */ 00336 }; 00337 00338 ACE_END_VERSIONED_NAMESPACE_DECL 00339 00340 #if defined (__ACE_INLINE__) 00341 #include "ace/Time_Value.inl" 00342 #endif /* __ACE_INLINE__ */ 00343 00344 #if defined (ACE_WIN32) && defined (_WIN32_WCE) 00345 } 00346 #endif 00347 00348 #if defined (__MINGW32__) 00349 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00350 // The MingW linker has problems with the exported statics 00351 // zero and max_time with these two statics the linker will be able to 00352 // resolve the static exported symbols. 00353 static const ACE_Time_Value& __zero_time = ACE_Time_Value::zero; 00354 static const ACE_Time_Value& __max_time = ACE_Time_Value::max_time; 00355 ACE_END_VERSIONED_NAMESPACE_DECL 00356 #endif /* __MINGW32__ */ 00357 00358 #include /**/ "ace/post.h" 00359 00360 #endif /* ACE_TIME_VALUE_H */