]> git.saurik.com Git - wxWidgets.git/blob - include/wx/stopwatch.h
189f0434cc389c434dab5b4abba85c6ba892ce48
[wxWidgets.git] / include / wx / stopwatch.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/stopwatch.h
3 // Purpose: wxStopWatch and global time-related functions
4 // Author: Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch)
5 // Created: 26.06.03 (extracted from wx/timer.h)
6 // RCS-ID: $Id$
7 // Copyright: (c) 1998-2003 Julian Smart, Sylvain Bougnoux
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_STOPWATCH_H_
12 #define _WX_STOPWATCH_H_
13
14 #include "wx/defs.h"
15 #include "wx/longlong.h"
16
17 // ----------------------------------------------------------------------------
18 // wxStopWatch: measure time intervals with up to 1ms resolution
19 // ----------------------------------------------------------------------------
20
21 #if wxUSE_STOPWATCH
22
23 class WXDLLIMPEXP_BASE wxStopWatch
24 {
25 public:
26 // ctor starts the stop watch
27 wxStopWatch() { m_pauseCount = 0; Start(); }
28
29 // Start the stop watch at the moment t0 expressed in milliseconds (i.e.
30 // calling Time() immediately afterwards returns t0). This can be used to
31 // restart an existing stopwatch.
32 void Start(long t0 = 0);
33
34 // pause the stop watch
35 void Pause()
36 {
37 if ( m_pauseCount++ == 0 )
38 m_elapsedBeforePause = GetCurrentClockValue() - m_t0;
39 }
40
41 // resume it
42 void Resume()
43 {
44 wxASSERT_MSG( m_pauseCount > 0,
45 wxT("Resuming stop watch which is not paused") );
46
47 if ( --m_pauseCount == 0 )
48 {
49 DoStart();
50 m_t0 -= m_elapsedBeforePause;
51 }
52 }
53
54 // Get elapsed time since the last Start() in microseconds.
55 wxLongLong TimeInMicro() const;
56
57 // get elapsed time since the last Start() in milliseconds
58 long Time() const { return (TimeInMicro()/1000).ToLong(); }
59
60 private:
61 // Really starts the stop watch. The initial time is set to current clock
62 // value.
63 void DoStart();
64
65 // Returns the current clock value in its native units.
66 wxLongLong GetCurrentClockValue() const;
67
68 // Return the frequency of the clock used in its ticks per second.
69 wxLongLong GetClockFreq() const;
70
71
72 // The clock value when the stop watch was last started. Its units vary
73 // depending on the platform.
74 wxLongLong m_t0;
75
76 // The elapsed time as of last Pause() call (only valid if m_pauseCount >
77 // 0) in the same units as m_t0.
78 wxLongLong m_elapsedBeforePause;
79
80 // if > 0, the stop watch is paused, otherwise it is running
81 int m_pauseCount;
82 };
83
84 #endif // wxUSE_STOPWATCH
85
86 #if wxUSE_LONGLONG && WXWIN_COMPATIBILITY_2_6
87
88 // Starts a global timer
89 // -- DEPRECATED: use wxStopWatch instead
90 wxDEPRECATED( void WXDLLIMPEXP_BASE wxStartTimer() );
91
92 // Gets elapsed milliseconds since last wxStartTimer or wxGetElapsedTime
93 // -- DEPRECATED: use wxStopWatch instead
94 wxDEPRECATED( long WXDLLIMPEXP_BASE wxGetElapsedTime(bool resetTimer = true) );
95
96 #endif // wxUSE_LONGLONG && WXWIN_COMPATIBILITY_2_6
97
98 // ----------------------------------------------------------------------------
99 // global time functions
100 // ----------------------------------------------------------------------------
101
102 // Get number of seconds since local time 00:00:00 Jan 1st 1970.
103 extern long WXDLLIMPEXP_BASE wxGetLocalTime();
104
105 // Get number of seconds since GMT 00:00:00, Jan 1st 1970.
106 extern long WXDLLIMPEXP_BASE wxGetUTCTime();
107
108 #if wxUSE_LONGLONG
109 typedef wxLongLong wxMilliClock_t;
110 inline long wxMilliClockToLong(wxLongLong ll) { return ll.ToLong(); }
111 #else
112 typedef double wxMilliClock_t;
113 inline long wxMilliClockToLong(double d) { return wx_truncate_cast(long, d); }
114 #endif // wxUSE_LONGLONG
115
116 // Get number of milliseconds since local time 00:00:00 Jan 1st 1970
117 extern wxMilliClock_t WXDLLIMPEXP_BASE wxGetLocalTimeMillis();
118
119 #define wxGetCurrentTime() wxGetLocalTime()
120
121 // on some really old systems gettimeofday() doesn't have the second argument,
122 // define wxGetTimeOfDay() to hide this difference
123 #ifdef HAVE_GETTIMEOFDAY
124 #ifdef WX_GETTIMEOFDAY_NO_TZ
125 #define wxGetTimeOfDay(tv) gettimeofday(tv)
126 #else
127 #define wxGetTimeOfDay(tv) gettimeofday((tv), NULL)
128 #endif
129 #endif // HAVE_GETTIMEOFDAY
130
131 #endif // _WX_STOPWATCH_H_