]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/timer.h
Resolve GCC's 'type-punned pointer will break strict-aliasing rules' warning by break...
[wxWidgets.git] / include / wx / unix / private / timer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/private/timer.h
3 // Purpose: wxTimer for wxBase (unix)
4 // Author: Lukasz Michalski
5 // Created: 15/01/2005
6 // RCS-ID: $Id$
7 // Copyright: (c) Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNIX_PRIVATE_TIMER_H_
12 #define _WX_UNIX_PRIVATE_TIMER_H_
13
14 #if wxUSE_TIMER
15
16 #include "wx/private/timer.h"
17
18 // the type used for milliseconds is large enough for microseconds too but
19 // introduce a synonym for it to avoid confusion
20 typedef wxMilliClock_t wxUsecClock_t;
21
22 // ----------------------------------------------------------------------------
23 // wxTimer implementation class for Unix platforms
24 // ----------------------------------------------------------------------------
25
26 // NB: we have to export at least this symbol from the shared library, because
27 // it's used by wxDFB's wxCore
28 class WXDLLIMPEXP_BASE wxUnixTimerImpl : public wxTimerImpl
29 {
30 public:
31 wxUnixTimerImpl(wxTimer *timer);
32 virtual ~wxUnixTimerImpl();
33
34 virtual bool IsRunning() const;
35 virtual bool Start(int milliseconds = -1, bool oneShot = false);
36 virtual void Stop();
37
38 // for wxTimerScheduler only: resets the internal flag indicating that the
39 // timer is running
40 void MarkStopped()
41 {
42 wxASSERT_MSG( m_isRunning, wxT("stopping non-running timer?") );
43
44 m_isRunning = false;
45 }
46
47 private:
48 bool m_isRunning;
49 };
50
51 // ----------------------------------------------------------------------------
52 // wxTimerSchedule: information about a single timer, used by wxTimerScheduler
53 // ----------------------------------------------------------------------------
54
55 struct wxTimerSchedule
56 {
57 wxTimerSchedule(wxUnixTimerImpl *timer, wxUsecClock_t expiration)
58 : m_timer(timer),
59 m_expiration(expiration)
60 {
61 }
62
63 // the timer itself (we don't own this pointer)
64 wxUnixTimerImpl *m_timer;
65
66 // the time of its next expiration, in usec
67 wxUsecClock_t m_expiration;
68 };
69
70 // the linked list of all active timers, we keep it sorted by expiration time
71 WX_DECLARE_LIST(wxTimerSchedule, wxTimerList);
72
73 // ----------------------------------------------------------------------------
74 // wxTimerScheduler: class responsible for updating all timers
75 // ----------------------------------------------------------------------------
76
77 class wxTimerScheduler
78 {
79 public:
80 // get the unique timer scheduler instance
81 static wxTimerScheduler& Get()
82 {
83 if ( !ms_instance )
84 ms_instance = new wxTimerScheduler;
85
86 return *ms_instance;
87 }
88
89 // must be called on shutdown to delete the global timer scheduler
90 static void Shutdown()
91 {
92 if ( ms_instance )
93 {
94 delete ms_instance;
95 ms_instance = NULL;
96 }
97 }
98
99 // adds timer which should expire at the given absolute time to the list
100 void AddTimer(wxUnixTimerImpl *timer, wxUsecClock_t expiration);
101
102 // remove timer from the list, called automatically from timer dtor
103 void RemoveTimer(wxUnixTimerImpl *timer);
104
105
106 // the functions below are used by the event loop implementation to monitor
107 // and notify timers:
108
109 // if this function returns true, the time remaining until the next time
110 // expiration is returned in the provided parameter (always positive or 0)
111 //
112 // it returns false if there are no timers
113 bool GetNext(wxUsecClock_t *remaining) const;
114
115 // trigger the timer event for all timers which have expired, return true
116 // if any did
117 bool NotifyExpired();
118
119 private:
120 // ctor and dtor are private, this is a singleton class only created by
121 // Get() and destroyed by Shutdown()
122 wxTimerScheduler() { }
123 ~wxTimerScheduler();
124
125 // add the given timer schedule to the list in the right place
126 //
127 // we take ownership of the pointer "s" which must be heap-allocated
128 void DoAddTimer(wxTimerSchedule *s);
129
130
131 // the list of all currently active timers sorted by expiration
132 wxTimerList m_timers;
133
134 static wxTimerScheduler *ms_instance;
135 };
136
137 // this helper function currently only exists for Unix platforms but could be
138 // moved to wx/stopwatch.h if it turns out to be useful elsewhere
139 //
140 // returns the number of microseconds since the Epoch
141 extern wxUsecClock_t wxGetLocalTimeUsec();
142
143 #endif // wxUSE_TIMER
144
145 #endif // _WX_UNIX_PRIVATE_TIMER_H_