Restore build on FreeBSD.
[getmangos.git] / dep / ACE_wrappers / ace / Time_Value.h
blobb00410e2b33698248a4f351b8b878c4fd2db89ff
1 // -*- C++ -*-
3 //=============================================================================
4 /**
5 * @file Time_Value.h
7 * $Id: Time_Value.h 81914 2008-06-11 13:56:11Z sma $
9 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
11 //=============================================================================
13 #ifndef ACE_TIME_VALUE_H
14 #define ACE_TIME_VALUE_H
16 #include /**/ "ace/pre.h"
18 #include /**/ "ace/ACE_export.h"
20 #if !defined (ACE_LACKS_PRAGMA_ONCE)
21 # pragma once
22 #endif /* ACE_LACKS_PRAGMA_ONCE */
24 # include "ace/os_include/os_time.h"
26 // Define some helpful constants.
27 // Not type-safe, and signed. For backward compatibility.
28 #define ACE_ONE_SECOND_IN_MSECS 1000L
29 suseconds_t const ACE_ONE_SECOND_IN_USECS = 1000000;
30 #define ACE_ONE_SECOND_IN_NSECS 1000000000L
32 // needed for ACE_UINT64
33 #include "ace/Basic_Types.h"
35 // This forward declaration is needed by the set() and FILETIME() functions
36 #if defined (ACE_LACKS_LONGLONG_T)
37 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
38 class ACE_Export ACE_U_LongLong;
39 ACE_END_VERSIONED_NAMESPACE_DECL
40 #endif /* ACE_LACKS_LONGLONG_T */
42 // -------------------------------------------------------------------
44 /**
45 * @class ACE_Time_Value
47 * @brief Operations on "timeval" structures, which express time in
48 * seconds (secs) and microseconds (usecs).
50 * This class centralizes all the time related processing in
51 * ACE. These time values are typically used in conjunction with OS
52 * mechanisms like <select>, <poll>, or <cond_timedwait>.
54 #if defined (ACE_WIN32) && defined (_WIN32_WCE)
55 // Something is a bit brain-damaged here and I'm not sure what... this code
56 // compiled before the OS reorg for ACE 5.4. Since then it hasn't - eVC
57 // complains that the operators that return ACE_Time_Value are C-linkage
58 // functions that can't return a C++ class. The only way I've found to
59 // defeat this is to wrap the whole class in extern "C++".
60 // - Steve Huston, 23-Aug-2004
61 extern "C++" {
62 #endif
64 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
66 class ACE_Export ACE_Time_Value
68 public:
70 /// Constant "0".
71 static const ACE_Time_Value zero;
73 /**
74 * Constant for maximum time representable. Note that this time is
75 * not intended for use with <select> or other calls that may have
76 * *their own* implementation-specific maximum time representations.
77 * Its primary use is in time computations such as those used by the
78 * dynamic subpriority strategies in the ACE_Dynamic_Message_Queue
79 * class.
81 static const ACE_Time_Value max_time;
83 // = Initialization methods.
85 /// Default Constructor.
86 ACE_Time_Value (void);
88 /// Constructor.
89 explicit ACE_Time_Value (time_t sec, suseconds_t usec = 0);
91 // = Methods for converting to/from various time formats.
93 /// Construct the ACE_Time_Value from a timeval.
94 explicit ACE_Time_Value (const struct timeval &t);
96 /// Construct the ACE_Time_Value object from a timespec_t.
97 explicit ACE_Time_Value (const timespec_t &t);
99 # if defined (ACE_WIN32)
100 /// Construct the ACE_Time_Value object from a Win32 FILETIME
101 explicit ACE_Time_Value (const FILETIME &ft);
102 # endif /* ACE_WIN32 */
104 /// Initializes the ACE_Time_Value from seconds and useconds.
105 void set (time_t sec, suseconds_t usec);
107 /// Initializes the ACE_Time_Value from a double, which is assumed to be
108 /// in second format, with any remainder treated as microseconds.
109 void set (double d);
111 /// Initializes the ACE_Time_Value from a timeval.
112 void set (const timeval &t);
114 /// Initializes the ACE_Time_Value object from a timespec_t.
115 void set (const timespec_t &t);
117 # if defined (ACE_WIN32)
118 /// Initializes the ACE_Time_Value object from a Win32 FILETIME.
119 void set (const FILETIME &ft);
120 # endif /* ACE_WIN32 */
122 /// Converts from ACE_Time_Value format into milliseconds format.
124 * @return Sum of second field (in milliseconds) and microsecond field
125 * (in milliseconds). Note that this method can overflow if
126 * the second and microsecond field values are large, so use
127 * the msec (ACE_UINT64 &ms) method instead.
129 * @note The semantics of this method differs from the sec() and
130 * usec() methods. There is no analogous "millisecond"
131 * component in an ACE_Time_Value.
133 unsigned long msec (void) const;
135 /// Converts from ACE_Time_Value format into milliseconds format.
137 * @return Sum of second field (in milliseconds) and microsecond field
138 * (in milliseconds) and return them via the @param ms parameter.
140 * @note The semantics of this method differs from the sec() and
141 * usec() methods. There is no analogous "millisecond"
142 * component in an ACE_Time_Value.
144 void msec (ACE_UINT64 &ms) const;
146 /// Converts from ACE_Time_Value format into milliseconds format.
148 * @return Sum of second field (in milliseconds) and microsecond field
149 * (in milliseconds) and return them via the @param ms parameter.
151 * @note The semantics of this method differs from the sec() and
152 * usec() methods. There is no analogous "millisecond"
153 * component in an ACE_Time_Value.
155 void msec (ACE_UINT64 &ms) /* const */;
157 /// Converts from milli-seconds format into ACE_Time_Value format.
159 * @note The semantics of this method differs from the sec() and
160 * usec() methods. There is no analogous "millisecond"
161 * component in an ACE_Time_Value.
163 void msec (long);
165 /// Converts from milli-seconds format into ACE_Time_Value format.
167 * @note The semantics of this method differs from the sec() and
168 * usec() methods. There is no analogous "millisecond"
169 * component in an ACE_Time_Value.
171 void msec (int); // converted to long then calls above.
173 /// Returns the value of the object as a timespec_t.
174 operator timespec_t () const;
176 /// Returns the value of the object as a timeval.
177 operator timeval () const;
179 /// Returns a pointer to the object as a timeval.
180 operator const timeval *() const;
182 # if defined (ACE_WIN32)
183 /// Returns the value of the object as a Win32 FILETIME.
184 operator FILETIME () const;
185 # endif /* ACE_WIN32 */
187 // = The following are accessor/mutator methods.
189 /// Get seconds.
191 * @return The second field/component of this ACE_Time_Value.
193 * @note The semantics of this method differs from the msec()
194 * method.
196 time_t sec (void) const;
198 /// Set seconds.
199 void sec (time_t sec);
201 /// Get microseconds.
203 * @return The microsecond field/component of this ACE_Time_Value.
205 * @note The semantics of this method differs from the msec()
206 * method.
208 suseconds_t usec (void) const;
210 /// Set microseconds.
211 void usec (suseconds_t usec);
214 * @return Sum of second field (in microseconds) and microsecond field
215 * and return them via the @param usec parameter.
217 void to_usec (ACE_UINT64 &usec) const;
219 // = The following arithmetic methods operate on ACE_Time_Value's.
221 /// Add @a tv to this.
222 ACE_Time_Value &operator += (const ACE_Time_Value &tv);
224 /// Add @a tv to this.
225 ACE_Time_Value &operator += (time_t tv);
227 /// Assign @ tv to this
228 ACE_Time_Value &operator = (const ACE_Time_Value &tv);
230 /// Assign @ tv to this
231 ACE_Time_Value &operator = (time_t tv);
233 /// Subtract @a tv to this.
234 ACE_Time_Value &operator -= (const ACE_Time_Value &tv);
236 /// Substract @a tv to this.
237 ACE_Time_Value &operator -= (time_t tv);
240 \brief Multiply the time value by the @a d factor.
241 \note The result of the operator is valid for results from range
242 < (ACE_INT32_MIN, -999999), (ACE_INT32_MAX, 999999) >. Result
243 outside this range are saturated to a limit.
245 ACE_Time_Value &operator *= (double d);
247 /// Increment microseconds as postfix.
249 * @note The only reason this is here is to allow the use of ACE_Atomic_Op
250 * with ACE_Time_Value.
252 ACE_Time_Value operator++ (int);
254 /// Increment microseconds as prefix.
256 * @note The only reason this is here is to allow the use of ACE_Atomic_Op
257 * with ACE_Time_Value.
259 ACE_Time_Value &operator++ (void);
261 /// Decrement microseconds as postfix.
263 * @note The only reason this is here is to allow the use of ACE_Atomic_Op
264 * with ACE_Time_Value.
266 ACE_Time_Value operator-- (int);
268 /// Decrement microseconds as prefix.
270 * @note The only reason this is here is to allow the use of ACE_Atomic_Op
271 * with ACE_Time_Value.
273 ACE_Time_Value &operator-- (void);
275 /// Adds two ACE_Time_Value objects together, returns the sum.
276 friend ACE_Export ACE_Time_Value operator + (const ACE_Time_Value &tv1,
277 const ACE_Time_Value &tv2);
279 /// Subtracts two ACE_Time_Value objects, returns the difference.
280 friend ACE_Export ACE_Time_Value operator - (const ACE_Time_Value &tv1,
281 const ACE_Time_Value &tv2);
283 /// True if @a tv1 < @a tv2.
284 friend ACE_Export bool operator < (const ACE_Time_Value &tv1,
285 const ACE_Time_Value &tv2);
287 /// True if @a tv1 > @a tv2.
288 friend ACE_Export bool operator > (const ACE_Time_Value &tv1,
289 const ACE_Time_Value &tv2);
291 /// True if @a tv1 <= @a tv2.
292 friend ACE_Export bool operator <= (const ACE_Time_Value &tv1,
293 const ACE_Time_Value &tv2);
295 /// True if @a tv1 >= @a tv2.
296 friend ACE_Export bool operator >= (const ACE_Time_Value &tv1,
297 const ACE_Time_Value &tv2);
299 /// True if @a tv1 == @a tv2.
300 friend ACE_Export bool operator == (const ACE_Time_Value &tv1,
301 const ACE_Time_Value &tv2);
303 /// True if @a tv1 != @a tv2.
304 friend ACE_Export bool operator != (const ACE_Time_Value &tv1,
305 const ACE_Time_Value &tv2);
307 //@{
308 /// Multiplies the time value by @a d
309 friend ACE_Export ACE_Time_Value operator * (double d,
310 const ACE_Time_Value &tv);
312 friend ACE_Export ACE_Time_Value operator * (const ACE_Time_Value &tv,
313 double d);
314 //@}
316 /// Dump is a no-op.
318 * The dump() method is a no-op. It's here for backwards compatibility
319 * only, but does not dump anything. Invoking logging methods here
320 * violates layering restrictions in ACE because this class is part
321 * of the OS layer and @c ACE_Log_Msg is at a higher level.
323 void dump (void) const;
325 # if defined (ACE_WIN32)
326 /// Const time difference between FILETIME and POSIX time.
327 # if defined (ACE_LACKS_LONGLONG_T)
328 static const ACE_U_LongLong FILETIME_to_timval_skew;
329 # else
330 static const DWORDLONG FILETIME_to_timval_skew;
331 # endif // ACE_LACKS_LONGLONG_T
332 # endif /* ACE_WIN32 */
334 private:
335 /// Put the timevalue into a canonical form.
336 void normalize (void);
338 /// Store the values as a timeval.
339 #if defined (ACE_HAS_TIME_T_LONG_MISMATCH)
340 // Windows' timeval is non-conformant, so swap in a struct that conforms
341 // to the proper data types to represent the entire time range that this
342 // class's API can accept.
343 // Also, since this class can supply a pointer to a timeval that things
344 // like select() expect, we need the OS-defined one as well. To make this
345 // available, use a real timeval called ext_tv_ and set it up when needed.
346 // Since this is most often for relative times that don't approach 32 bits
347 // in size, reducing a time_t to fit should be no problem.
348 struct {
349 time_t tv_sec;
350 suseconds_t tv_usec;
351 } tv_;
352 timeval ext_tv_;
353 #else
354 timeval tv_;
355 #endif /* ACE_HAS_TIME_T_LONG_MISMATCH */
358 ACE_END_VERSIONED_NAMESPACE_DECL
360 #if defined (__ACE_INLINE__)
361 #include "ace/Time_Value.inl"
362 #endif /* __ACE_INLINE__ */
364 #if defined (ACE_WIN32) && defined (_WIN32_WCE)
366 #endif
368 #if defined (__MINGW32__)
369 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
370 // The MingW linker has problems with the exported statics
371 // zero and max_time with these two statics the linker will be able to
372 // resolve the static exported symbols.
373 static const ACE_Time_Value& __zero_time = ACE_Time_Value::zero;
374 static const ACE_Time_Value& __max_time = ACE_Time_Value::max_time;
375 ACE_END_VERSIONED_NAMESPACE_DECL
376 #endif /* __MINGW32__ */
378 #include /**/ "ace/post.h"
380 #endif /* ACE_TIME_VALUE_H */