Android release v6.7_preview1
[xcsoar.git] / src / RateLimiter.hpp
blob840e9253ab2eb889e83ba34dddda8e2c7ec6fcdb
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_RATE_LIMITER_HPP
25 #define XCSOAR_RATE_LIMITER_HPP
27 #include "Event/Timer.hpp"
28 #include "Time/PeriodClock.hpp"
30 /**
31 * A class that limits the rate at which events are processed. It
32 * postpones processing for a certain amount of time, and combines
33 * several events.
35 * Due to its use of timers and the event queue, this class can only
36 * be used in the main thread.
38 class RateLimiter : private Timer {
39 /**
40 * Remember the last Run() invocation.
42 PeriodClock clock;
44 unsigned period_ms, delay_ms;
46 public:
47 /**
48 * Constructor.
50 * @param period_ms the minimum duration between two invocations
51 * @param delay_ms an event is delayed by this duration to combine
52 * consecutive invocations
54 RateLimiter(unsigned _period_ms, unsigned _delay_ms=0);
56 /**
57 * Destructor. Discards any pending events.
59 ~RateLimiter() {
60 Cancel();
63 void Trigger();
65 using Timer::Cancel;
67 protected:
68 virtual void Run() = 0;
70 private:
71 /* virtual methods from class Timer */
72 virtual void OnTimer() override;
75 #endif