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