Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Time_Value.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Time_Value.h
00006  *
00007  *  $Id: Time_Value.h,v 1.1.1.4 2003/02/21 18:36:32 chad 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/OS_Export.h"
00019 #include "ace/ACE_export.h"
00020 
00021 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00022 # pragma once
00023 #endif /* ACE_LACKS_PRAGMA_ONCE */
00024 
00025 
00026 # if !defined (ACE_HAS_WINCE) && !defined (ACE_PSOS_DIAB_MIPS)
00027 #     include /**/ <time.h>
00028 #   if defined (__Lynx__)
00029 #     include /**/ <st.h>
00030 #     include /**/ <sem.h>
00031 #   endif /* __Lynx__ */
00032 # endif /* ACE_HAS_WINCE ACE_PSOS_DIAB_MIPS */
00033 
00034 # if defined (ACE_LACKS_SYSTIME_H)
00035 // Some platforms may need to include this, but I suspect that most
00036 // will get it from <time.h>
00037 #   if defined (VXWORKS)
00038 #     include /**/ <sys/times.h>
00039 #   else
00040 #     include /**/ <sys/time.h>
00041 #   endif /* VXWORKS */
00042 # endif /* ACE_LACKS_SYSTIME_H */
00043 
00044 // HP-UX 10.20 doesn't define timespec_t - it defined struct timespec.
00045 #if defined (HPUX_10)
00046 typedef struct timespec timespec_t;
00047 #endif /* HPUX_10 */
00048 
00049 // Define some helpful constants.
00050 // Not type-safe, and signed.  For backward compatibility.
00051 #define ACE_ONE_SECOND_IN_MSECS 1000L
00052 #define ACE_ONE_SECOND_IN_USECS 1000000L
00053 #define ACE_ONE_SECOND_IN_NSECS 1000000000L
00054 
00055 // -------------------------------------------------------------------
00056 // These forward declarations are only used to circumvent a bug in
00057 // MSVC 6.0 compiler.  They shouldn't cause any problem for other
00058 // compilers and they can be removed once MS release a SP that contains
00059 // the fix.
00060 class ACE_Time_Value;
00061 ACE_OS_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1,
00062                                          const ACE_Time_Value &tv2);
00063 
00064 ACE_OS_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1,
00065                                          const ACE_Time_Value &tv2);
00066 
00067 // This forward declaration is needed by the set() and FILETIME() functions
00068 #if defined (ACE_LACKS_LONGLONG_T)
00069 class ACE_Export ACE_U_LongLong;
00070 #endif  /* ACE_LACKS_LONGLONG_T */
00071 // -------------------------------------------------------------------
00072 
00073 # if !defined (ACE_HAS_POSIX_TIME) && !defined (ACE_PSOS)
00074 // Definition per POSIX.
00075 typedef struct timespec
00076 {
00077   /// Seconds
00078   time_t tv_sec;
00079   /// Nanoseconds
00080   long tv_nsec;
00081 } timespec_t;
00082 # elif defined (ACE_HAS_BROKEN_POSIX_TIME)
00083 // OSF/1 defines struct timespec in <sys/timers.h> - Tom Marrs
00084 #   include /**/ <sys/timers.h>
00085 # endif /* !ACE_HAS_POSIX_TIME */
00086 
00087 # if defined(ACE_LACKS_TIMESPEC_T)
00088 typedef struct timespec timespec_t;
00089 # endif /* ACE_LACKS_TIMESPEC_T */
00090 
00091 // -------------------------------------------------------------------
00092 
00093 /**
00094  * @class ACE_Time_Value
00095  *
00096  * @brief Operations on "timeval" structures, which express time in
00097  * seconds (secs) and microseconds (usecs).
00098  *
00099  * This class centralizes all the time related processing in
00100  * ACE.  These time values are typically used in conjunction with OS
00101  * mechanisms like <select>, <poll>, or <cond_timedwait>.
00102  */
00103 class ACE_OS_Export ACE_Time_Value
00104 {
00105 public:
00106 
00107   /// Constant "0".
00108   static const ACE_Time_Value zero;
00109 
00110   /**
00111    * Constant for maximum time representable.  Note that this time is
00112    * not intended for use with <select> or other calls that may have
00113    * *their own* implementation-specific maximum time representations.
00114    * Its primary use is in time computations such as those used by the
00115    * dynamic subpriority strategies in the ACE_Dynamic_Message_Queue
00116    * class.
00117    */
00118   static const ACE_Time_Value max_time;
00119 
00120   // = Initialization methods.
00121 
00122   /// Default Constructor.
00123   ACE_Time_Value (void);
00124 
00125   /// Constructor.
00126   ACE_Time_Value (long sec, long usec = 0);
00127 
00128   // = Methods for converting to/from various time formats.
00129 
00130   /// Construct the ACE_Time_Value from a timeval.
00131   ACE_Time_Value (const struct timeval &t);
00132 
00133   /// Construct the ACE_Time_Value object from a timespec_t.
00134   ACE_Time_Value (const timespec_t &t);
00135 
00136 # if defined (ACE_WIN32)
00137   /// Construct the ACE_Time_Value object from a Win32 FILETIME
00138   ACE_Time_Value (const FILETIME &ft);
00139 # endif /* ACE_WIN32 */
00140 
00141   /// Initializes the ACE_Time_Value from two longs.
00142   void set (long sec, long usec);
00143 
00144   /// Initializes the ACE_Time_Value from a double, which is assumed to be
00145   /// in second format, with any remainder treated as microseconds.
00146   void set (double d);
00147 
00148   /// Initializes the ACE_Time_Value from a timeval.
00149   void set (const timeval &t);
00150 
00151   /// Initializes the ACE_Time_Value object from a timespec_t.
00152   void set (const timespec_t &t);
00153 
00154 # if defined (ACE_WIN32)
00155   ///  Initializes the ACE_Time_Value object from a Win32 FILETIME.
00156   void set (const FILETIME &ft);
00157 # endif /* ACE_WIN32 */
00158 
00159   /// Converts from ACE_Time_Value format into milli-seconds format.
00160   /**
00161    * @return Sum of second field (in milliseconds) and microsecond field
00162    *         (in milliseconds).
00163    *
00164    * @note The semantics of this method differs from the sec() and
00165    *       usec() methods.  There is no analogous "millisecond"
00166    *       component in an ACE_Time_Value.
00167    */
00168   long msec (void) const;
00169 
00170   /// Converts from milli-seconds format into ACE_Time_Value format.
00171   /**
00172    * @note The semantics of this method differs from the sec() and
00173    *       usec() methods.  There is no analogous "millisecond"
00174    *       component in an ACE_Time_Value.
00175    */
00176   void msec (long);
00177 
00178   /// Returns the value of the object as a timespec_t.
00179   operator timespec_t () const;
00180 
00181   /// Returns the value of the object as a timeval.
00182   operator timeval () const;
00183 
00184   /// Returns a pointer to the object as a timeval.
00185   operator const timeval *() const;
00186 
00187 # if defined (ACE_WIN32)
00188   /// Returns the value of the object as a Win32 FILETIME.
00189   operator FILETIME () const;
00190 # endif /* ACE_WIN32 */
00191 
00192   // = The following are accessor/mutator methods.
00193 
00194   /// Get seconds.
00195   /**
00196    * @return The second field/component of this ACE_Time_Value.
00197    *
00198    * @note The semantics of this method differs from the msec()
00199    *       method.
00200    */
00201   long sec (void) const;
00202 
00203   /// Set seconds.
00204   void sec (long sec);
00205 
00206   /// Get microseconds.
00207   /**
00208    * @return The microsecond field/component of this ACE_Time_Value.
00209    *
00210    * @note The semantics of this method differs from the msec()
00211    *       method.
00212    */
00213   long usec (void) const;
00214 
00215   /// Set microseconds.
00216   void usec (long usec);
00217 
00218   // = The following arithmetic methods operate on ACE_Time_Value's.
00219 
00220   /// Add @a tv to this.
00221   ACE_Time_Value &operator += (const ACE_Time_Value &tv);
00222 
00223   /// Subtract @a tv to this.
00224   ACE_Time_Value &operator -= (const ACE_Time_Value &tv);
00225 
00226   /// Multiply the time value by the @a d factor, which must be >= 0.
00227   ACE_Time_Value &operator *= (double d);
00228 
00229   /// Increment microseconds as postfix.
00230   /**
00231    * @note The only reason this is here is to allow the use of ACE_Atomic_Op
00232    * with ACE_Time_Value.
00233    */
00234   ACE_Time_Value operator++ (int);
00235 
00236   /// Increment microseconds as prefix.
00237   /**
00238    * @note The only reason this is here is to allow the use of ACE_Atomic_Op
00239    * with ACE_Time_Value.
00240    */
00241   ACE_Time_Value &operator++ (void);
00242 
00243   /// Decrement microseconds as postfix.
00244   /**
00245    * @note The only reason this is here is to allow the use of ACE_Atomic_Op
00246    * with ACE_Time_Value.
00247    */
00248   ACE_Time_Value operator-- (int);
00249 
00250   /// Decrement microseconds as prefix.
00251   /**
00252    * @note The only reason this is here is to allow the use of ACE_Atomic_Op
00253    * with ACE_Time_Value.
00254    */
00255   ACE_Time_Value &operator-- (void);
00256 
00257   /// Adds two ACE_Time_Value objects together, returns the sum.
00258   friend ACE_OS_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1,
00259                                                   const ACE_Time_Value &tv2);
00260 
00261   /// Subtracts two ACE_Time_Value objects, returns the difference.
00262   friend ACE_OS_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1,
00263                                                   const ACE_Time_Value &tv2);
00264 
00265   /// True if @a tv1 < @a tv2.
00266   friend ACE_OS_Export int operator < (const ACE_Time_Value &tv1,
00267                                        const ACE_Time_Value &tv2);
00268 
00269   /// True if @a tv1 > @a tv2.
00270   friend ACE_OS_Export int operator > (const ACE_Time_Value &tv1,
00271                                        const ACE_Time_Value &tv2);
00272 
00273   /// True if @a tv1 <= @a tv2.
00274   friend ACE_OS_Export int operator <= (const ACE_Time_Value &tv1,
00275                                         const ACE_Time_Value &tv2);
00276 
00277   /// True if @a tv1 >= @a tv2.
00278   friend ACE_OS_Export int operator >= (const ACE_Time_Value &tv1,
00279                                         const ACE_Time_Value &tv2);
00280 
00281   /// True if @a tv1 == @a tv2.
00282   friend ACE_OS_Export int operator == (const ACE_Time_Value &tv1,
00283                                         const ACE_Time_Value &tv2);
00284 
00285   /// True if @a tv1 != @a tv2.
00286   friend ACE_OS_Export int operator != (const ACE_Time_Value &tv1,
00287                                         const ACE_Time_Value &tv2);
00288 
00289   //@{
00290   /// Multiplies the time value by @a d
00291   friend ACE_OS_Export ACE_Time_Value operator * (double d,
00292                                                   const ACE_Time_Value &tv);
00293 
00294   friend ACE_OS_Export ACE_Time_Value operator * (const ACE_Time_Value &tv,
00295                                                   double d);
00296   //@}
00297 
00298   /// Dump is a no-op.
00299   /**
00300    * The dump() method is a no-op.  It's here for backwards compatibility
00301    * only, but does not dump anything. Invoking logging methods here
00302    * violates layering restrictions in ACE because this class is part
00303    * of the OS layer and @c ACE_Log_Msg is at a higher level.
00304    */
00305   void dump (void) const;
00306 
00307 # if defined (ACE_WIN32)
00308   /// Const time difference between FILETIME and POSIX time.
00309 #  if defined (ACE_LACKS_LONGLONG_T)
00310   static const ACE_U_LongLong FILETIME_to_timval_skew;
00311 #  else
00312   static const DWORDLONG FILETIME_to_timval_skew;
00313 #  endif // ACE_LACKS_LONGLONG_T
00314 # endif /* ACE_WIN32 */
00315 
00316 private:
00317   /// Put the timevalue into a canonical form.
00318   void normalize (void);
00319 
00320   /// Store the values as a timeval.
00321   timeval tv_;
00322 };
00323 
00324 #if defined (__ACE_INLINE__)
00325 #include "ace/Time_Value.inl"
00326 #endif /* __ACE_INLINE__ */
00327 
00328 #include "ace/post.h"
00329 
00330 #endif /* ACE_TIME_VALUE_H */

Generated on Mon Jun 16 11:21:47 2003 for ACE by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002