1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/stopwatch.h
3 // Purpose:     wxStopWatch and global time-related functions
4 // Author:      Julian Smart (wxTimer), Sylvain Bougnoux (wxStopWatch),
5 //              Vadim Zeitlin (time functions, current wxStopWatch)
6 // Created:     26.06.03 (extracted from wx/timer.h)
7 // Copyright:   (c) 1998-2003 Julian Smart, Sylvain Bougnoux
8 //              (c) 2011 Vadim Zeitlin
9 // Licence:     wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11 
12 #ifndef _WX_STOPWATCH_H_
13 #define _WX_STOPWATCH_H_
14 
15 #include "wx/defs.h"
16 #include "wx/longlong.h"
17 
18 // Time-related functions are also available via this header for compatibility
19 // but you should include wx/time.h directly if you need only them and not
20 // wxStopWatch itself.
21 #include "wx/time.h"
22 
23 // ----------------------------------------------------------------------------
24 // wxStopWatch: measure time intervals with up to 1ms resolution
25 // ----------------------------------------------------------------------------
26 
27 #if wxUSE_STOPWATCH
28 
29 class WXDLLIMPEXP_BASE wxStopWatch
30 {
31 public:
32     // ctor starts the stop watch
wxStopWatch()33     wxStopWatch() { m_pauseCount = 0; Start(); }
34 
35     // Start the stop watch at the moment t0 expressed in milliseconds (i.e.
36     // calling Time() immediately afterwards returns t0). This can be used to
37     // restart an existing stopwatch.
38     void Start(long t0 = 0);
39 
40     // pause the stop watch
Pause()41     void Pause()
42     {
43         if ( m_pauseCount++ == 0 )
44             m_elapsedBeforePause = GetCurrentClockValue() - m_t0;
45     }
46 
47     // resume it
Resume()48     void Resume()
49     {
50         wxASSERT_MSG( m_pauseCount > 0,
51                       wxT("Resuming stop watch which is not paused") );
52 
53         if ( --m_pauseCount == 0 )
54         {
55             DoStart();
56             m_t0 -= m_elapsedBeforePause;
57         }
58     }
59 
60     // Get elapsed time since the last Start() in microseconds.
61     wxLongLong TimeInMicro() const;
62 
63     // get elapsed time since the last Start() in milliseconds
Time()64     long Time() const { return (TimeInMicro()/1000).ToLong(); }
65 
66 private:
67     // Really starts the stop watch. The initial time is set to current clock
68     // value.
69     void DoStart();
70 
71     // Returns the current clock value in its native units.
72     wxLongLong GetCurrentClockValue() const;
73 
74     // Return the frequency of the clock used in its ticks per second.
75     wxLongLong GetClockFreq() const;
76 
77 
78     // The clock value when the stop watch was last started. Its units vary
79     // depending on the platform.
80     wxLongLong m_t0;
81 
82     // The elapsed time as of last Pause() call (only valid if m_pauseCount >
83     // 0) in the same units as m_t0.
84     wxLongLong m_elapsedBeforePause;
85 
86     // if > 0, the stop watch is paused, otherwise it is running
87     int m_pauseCount;
88 };
89 
90 #endif // wxUSE_STOPWATCH
91 
92 #endif // _WX_STOPWATCH_H_
93