00001 // -*- C++ -*- 00002 00003 //============================================================================= 00004 /** 00005 * @file Time_Value.h 00006 * 00007 * Time_Value.h,v 4.59 2006/02/10 10:37:02 jwillemsen Exp 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 #define ACE_ONE_SECOND_IN_USECS 1000000L 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 #if !defined (ACE_LACKS_LONGLONG_T) 00197 /** 00198 * @return Sum of second field (in microseconds) and microsecond field 00199 * and return them via the @param usec parameter. 00200 */ 00201 void to_usec (ACE_UINT64 &usec) const; 00202 00203 #endif /*ACE_LACKS_LONGLONG_T */ 00204 00205 // = The following arithmetic methods operate on ACE_Time_Value's. 00206 00207 /// Add @a tv to this. 00208 ACE_Time_Value &operator += (const ACE_Time_Value &tv); 00209 00210 /// Add @a tv to this. 00211 ACE_Time_Value &operator += (time_t tv); 00212 00213 /// Assign @ tv to this 00214 ACE_Time_Value &operator = (const ACE_Time_Value &tv); 00215 00216 /// Assign @ tv to this 00217 ACE_Time_Value &operator = (time_t tv); 00218 00219 /// Subtract @a tv to this. 00220 ACE_Time_Value &operator -= (const ACE_Time_Value &tv); 00221 00222 /// Substract @a tv to this. 00223 ACE_Time_Value &operator -= (time_t tv); 00224 00225 /** 00226 \brief Multiply the time value by the @a d factor. 00227 \note The result of the operator is valid for results from range 00228 < (ACE_INT32_MIN, -999999), (ACE_INT32_MAX, 999999) >. Result 00229 outside this range are saturated to a limit. 00230 */ 00231 ACE_Time_Value &operator *= (double d); 00232 00233 /// Increment microseconds as postfix. 00234 /** 00235 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00236 * with ACE_Time_Value. 00237 */ 00238 ACE_Time_Value operator++ (int); 00239 00240 /// Increment microseconds as prefix. 00241 /** 00242 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00243 * with ACE_Time_Value. 00244 */ 00245 ACE_Time_Value &operator++ (void); 00246 00247 /// Decrement microseconds as postfix. 00248 /** 00249 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00250 * with ACE_Time_Value. 00251 */ 00252 ACE_Time_Value operator-- (int); 00253 00254 /// Decrement microseconds as prefix. 00255 /** 00256 * @note The only reason this is here is to allow the use of ACE_Atomic_Op 00257 * with ACE_Time_Value. 00258 */ 00259 ACE_Time_Value &operator-- (void); 00260 00261 /// Adds two ACE_Time_Value objects together, returns the sum. 00262 friend ACE_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1, 00263 const ACE_Time_Value &tv2); 00264 00265 /// Subtracts two ACE_Time_Value objects, returns the difference. 00266 friend ACE_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1, 00267 const ACE_Time_Value &tv2); 00268 00269 /// True if @a tv1 < @a tv2. 00270 friend ACE_Export bool operator < (const ACE_Time_Value &tv1, 00271 const ACE_Time_Value &tv2); 00272 00273 /// True if @a tv1 > @a tv2. 00274 friend ACE_Export bool operator > (const ACE_Time_Value &tv1, 00275 const ACE_Time_Value &tv2); 00276 00277 /// True if @a tv1 <= @a tv2. 00278 friend ACE_Export bool operator <= (const ACE_Time_Value &tv1, 00279 const ACE_Time_Value &tv2); 00280 00281 /// True if @a tv1 >= @a tv2. 00282 friend ACE_Export bool operator >= (const ACE_Time_Value &tv1, 00283 const ACE_Time_Value &tv2); 00284 00285 /// True if @a tv1 == @a tv2. 00286 friend ACE_Export bool operator == (const ACE_Time_Value &tv1, 00287 const ACE_Time_Value &tv2); 00288 00289 /// True if @a tv1 != @a tv2. 00290 friend ACE_Export bool operator != (const ACE_Time_Value &tv1, 00291 const ACE_Time_Value &tv2); 00292 00293 //@{ 00294 /// Multiplies the time value by @a d 00295 friend ACE_Export ACE_Time_Value operator * (double d, 00296 const ACE_Time_Value &tv); 00297 00298 friend ACE_Export ACE_Time_Value operator * (const ACE_Time_Value &tv, 00299 double d); 00300 //@} 00301 00302 /// Dump is a no-op. 00303 /** 00304 * The dump() method is a no-op. It's here for backwards compatibility 00305 * only, but does not dump anything. Invoking logging methods here 00306 * violates layering restrictions in ACE because this class is part 00307 * of the OS layer and @c ACE_Log_Msg is at a higher level. 00308 */ 00309 void dump (void) const; 00310 00311 # if defined (ACE_WIN32) 00312 /// Const time difference between FILETIME and POSIX time. 00313 # if defined (ACE_LACKS_LONGLONG_T) 00314 static const ACE_U_LongLong FILETIME_to_timval_skew; 00315 # else 00316 static const DWORDLONG FILETIME_to_timval_skew; 00317 # endif // ACE_LACKS_LONGLONG_T 00318 # endif /* ACE_WIN32 */ 00319 00320 private: 00321 /// Put the timevalue into a canonical form. 00322 void normalize (void); 00323 00324 /// Store the values as a timeval. 00325 timeval tv_; 00326 }; 00327 00328 ACE_END_VERSIONED_NAMESPACE_DECL 00329 00330 #if defined (__ACE_INLINE__) 00331 #include "ace/Time_Value.inl" 00332 #endif /* __ACE_INLINE__ */ 00333 00334 #if defined (ACE_WIN32) && defined (_WIN32_WCE) 00335 } 00336 #endif 00337 00338 #if defined (__MINGW32__) 00339 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00340 // The MingW linker has problems with the exported statics 00341 // zero and max_time with these two statics the linker will be able to 00342 // resolve the static exported symbols. 00343 static const ACE_Time_Value& __zero_time = ACE_Time_Value::zero; 00344 static const ACE_Time_Value& __max_time = ACE_Time_Value::max_time; 00345 ACE_END_VERSIONED_NAMESPACE_DECL 00346 #endif /* __MINGW32__ */ 00347 00348 #include /**/ "ace/post.h" 00349 00350 #endif /* ACE_TIME_VALUE_H */