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