Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TimeoutManager.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * TimeoutManager.h
17  * Manages timeout events.
18  * Copyright (C) 2013 Simon Newton
19  */
20 
21 #ifndef COMMON_IO_TIMEOUTMANAGER_H_
22 #define COMMON_IO_TIMEOUTMANAGER_H_
23 
24 #include <queue>
25 #include <set>
26 #include <vector>
27 
28 #include "ola/Callback.h"
29 #include "ola/Clock.h"
30 #include "ola/ExportMap.h"
31 #include "ola/base/Macro.h"
32 #include "ola/thread/SchedulerInterface.h"
33 
34 namespace ola {
35 namespace io {
36 
37 
46  public :
52  TimeoutManager(ola::ExportMap *export_map, Clock *clock);
53 
54  ~TimeoutManager();
55 
66  const ola::TimeInterval &interval,
67  ola::Callback0<bool> *closure);
68 
77  const ola::TimeInterval &interval,
79 
85 
91  bool EventsPending() const {
92  return !m_events.empty();
93  }
94 
102 
103  static const char K_TIMER_VAR[];
104 
105  private :
106  class Event {
107  public:
108  explicit Event(const TimeInterval &interval, const Clock *clock)
109  : m_interval(interval) {
110  TimeStamp now;
111  clock->CurrentTime(&now);
112  m_next = now + m_interval;
113  }
114  virtual ~Event() {}
115  virtual bool Trigger() = 0;
116 
117  void UpdateTime(const TimeStamp &now) {
118  m_next = now + m_interval;
119  }
120 
121  TimeStamp NextTime() const { return m_next; }
122 
123  private:
124  TimeInterval m_interval;
125  TimeStamp m_next;
126  };
127 
128  // An event that only happens once
129  class SingleEvent: public Event {
130  public:
131  SingleEvent(const TimeInterval &interval,
132  const Clock *clock,
133  ola::BaseCallback0<void> *closure):
134  Event(interval, clock),
135  m_closure(closure) {
136  }
137 
138  virtual ~SingleEvent() {
139  if (m_closure)
140  delete m_closure;
141  }
142 
143  bool Trigger() {
144  if (m_closure) {
145  m_closure->Run();
146  // it's deleted itself at this point
147  m_closure = NULL;
148  }
149  return false;
150  }
151 
152  private:
153  ola::BaseCallback0<void> *m_closure;
154  };
155 
156  /*
157  * An event that occurs more than once. The closure can return false to
158  * indicate that it should not be called again.
159  */
160  class RepeatingEvent: public Event {
161  public:
162  RepeatingEvent(const TimeInterval &interval,
163  const Clock *clock,
164  ola::BaseCallback0<bool> *closure):
165  Event(interval, clock),
166  m_closure(closure) {
167  }
168  ~RepeatingEvent() {
169  delete m_closure;
170  }
171  bool Trigger() {
172  if (!m_closure)
173  return false;
174  return m_closure->Run();
175  }
176 
177  private:
178  ola::BaseCallback0<bool> *m_closure;
179  };
180 
181  struct ltevent {
182  bool operator()(Event *e1, Event *e2) const {
183  return e1->NextTime() > e2->NextTime();
184  }
185  };
186 
187  typedef std::priority_queue<Event*, std::vector<Event*>, ltevent>
188  event_queue_t;
189 
190  ola::ExportMap *m_export_map;
191  Clock *m_clock;
192 
193  event_queue_t m_events;
194  std::set<ola::thread::timeout_id> m_removed_timeouts;
195 
196  DISALLOW_COPY_AND_ASSIGN(TimeoutManager);
197 };
198 } // namespace io
199 } // namespace ola
200 #endif // COMMON_IO_TIMEOUTMANAGER_H_