CirclingWind: eliminate attributes min_vector, max_vector
[xcsoar.git] / src / Event / Timer.hpp
blobbc34602350b87beca5cefa414c78380d577d6ee8
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #ifndef XCSOAR_EVENT_TIMER_HPP
25 #define XCSOAR_EVENT_TIMER_HPP
27 #if defined(ANDROID) || defined(USE_EGL)
28 #include <atomic>
29 #elif defined(ENABLE_SDL)
30 #include <SDL_timer.h>
31 #include <atomic>
32 #else
33 #include "Screen/Window.hpp"
34 #include "Screen/Timer.hpp"
35 #endif
37 #include <assert.h>
38 #include <stddef.h>
40 /**
41 * A timer that, once initialized, periodically calls OnTimer() after
42 * a specified amount of time, until Cancel() gets called.
44 * Initially, this class does not schedule a timer.
46 * This class is not thread safe; all of the methods must be called
47 * from the main thread.
49 * The class #WindowTimer is cheaper on WIN32; use it instead of this
50 * class if you are implementing a #Window.
52 class Timer
53 #ifdef USE_GDI
54 : private Window, private WindowTimer
55 #endif
57 #if defined(ANDROID) || defined(USE_EGL)
58 std::atomic<bool> enabled, queued;
59 unsigned ms;
60 #elif defined(ENABLE_SDL)
61 SDL_TimerID id;
63 /**
64 * True when the timer event has been pushed to the event queue.
65 * This is used to prevent duplicate items stacking on the event
66 * queue.
68 std::atomic<bool> queued;
69 #endif
71 public:
72 /**
73 * Construct a Timer object that is not set initially.
75 #if defined(ANDROID) || defined(USE_EGL)
76 Timer():enabled(false), queued(false) {}
77 #elif defined(ENABLE_SDL)
78 Timer():id(NULL), queued(false) {}
79 #else
80 Timer():WindowTimer(*(Window *)this) {
81 Window::CreateMessageWindow();
83 #endif
85 Timer(const Timer &other) = delete;
87 protected:
88 /**
89 * The move constructor may only be used on inactive timers. This
90 * shall only be used by derived classes to pass inactive instances
91 * around.
93 Timer(Timer &&other)
94 #ifdef USE_GDI
95 :WindowTimer(*(Window *)this)
96 #endif
98 assert(!IsActive());
99 assert(!other.IsActive());
102 public:
103 ~Timer() {
104 /* timer must be cleaned up explicitly */
105 assert(!IsActive());
107 #ifdef USE_EGL
108 assert(!queued.load(std::memory_order_relaxed));
109 assert(!enabled.load(std::memory_order_relaxed));
110 #endif
113 #ifdef USE_GDI
114 /* inherit WindowTimer's methods */
115 using WindowTimer::IsActive;
116 using WindowTimer::Schedule;
117 using WindowTimer::Cancel;
118 #else
121 * Is the timer active, i.e. is it waiting for the current period to
122 * end?
124 bool IsActive() const {
125 #if defined(ANDROID) || defined(USE_EGL)
126 return enabled.load(std::memory_order_relaxed);
127 #elif defined(ENABLE_SDL)
128 return id != NULL;
129 #endif
133 * Schedule the timer. Cancels the previous setting if there was
134 * one.
136 void Schedule(unsigned ms);
139 * Cancels the scheduled timer, if any. This is safe to be called
140 * while the timer is running.
142 void Cancel();
144 #endif /* !GDI */
146 protected:
148 * This method gets called after the configured time has elapsed.
149 * Implement it.
151 virtual void OnTimer() = 0;
153 #if defined(ANDROID) || defined(USE_EGL)
154 public:
155 void Invoke();
156 #elif defined(ENABLE_SDL)
157 private:
158 void Invoke();
159 static void Invoke(void *ctx);
161 Uint32 Callback(Uint32 interval);
162 static Uint32 Callback(Uint32 interval, void *param);
163 #else
164 private:
165 /* virtual methods from class Window */
166 virtual bool OnTimer(WindowTimer &timer) override;
167 #endif
170 #endif