selector.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
3  * Copyright (C) 2006-2007 Marc Boris Duerner
4  * Copyright (C) 2006-2007 PTV AG
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * As a special exception, you may use this file as part of a free
12  * software library without restriction. Specifically, if other files
13  * instantiate templates or use macros or inline functions from this
14  * file, or you compile this file and link it with other files to
15  * produce an executable, this file does not by itself cause the
16  * resulting executable to be covered by the GNU General Public
17  * License. This exception does not however invalidate any other
18  * reasons why the executable file might be covered by the GNU Library
19  * General Public License.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  * Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30 #ifndef CXXTOOLS_SYSTEM_SELECTOR_H
31 #define CXXTOOLS_SYSTEM_SELECTOR_H
32 
33 #include <cxxtools/timespan.h>
34 #include <cxxtools/connectable.h>
35 #include <map>
36 
37 namespace cxxtools {
38 
39  class Timer;
40  class Selectable;
41  class Application;
42  class SelectorImpl;
43 
78  class SelectorBase : public Connectable
79  {
80  friend class Selectable;
81  friend class Timer;
82 
83 #if __cplusplus >= 201103L
84  SelectorBase(SelectorBase&) = delete;
85  SelectorBase& operator=(SelectorBase&) = delete;
86 #else
88  SelectorBase& operator=(SelectorBase&) { return *this; }
89 #endif
90 
91  public:
92  static const int WaitInfinite = -1;
93 
95  virtual ~SelectorBase();
96 
102  void add(Selectable& s);
103 
106  void remove(Selectable& s);
107 
115  void add(Timer& timer);
116 
121  void remove(Timer& timer);
122 
143  bool wait(Milliseconds msecs = WaitInfinite);
144 
158  bool waitUntil(Timespan t);
159 
166  void wake();
167 
175 
176  protected:
178  SelectorBase();
179 
180  void onAddTimer(Timer& timer);
181 
182  void onRemoveTimer(Timer& timer);
183 
184  void onTimerChanged(Timer& timer);
185 
190  virtual void onAdd(Selectable&) = 0;
191 
196  virtual void onRemove(Selectable&) = 0;
197 
202  virtual void onReinit(Selectable&) = 0;
203 
208  virtual void onChanged(Selectable& s) = 0;
209 
210  virtual bool onWaitUntil(Timespan timeout) = 0;
211 
212  virtual void onWake() = 0;
213 
214  private:
219  bool updateTimer(Timespan& timeout);
220 
222  typedef std::multimap<Timespan, Timer*> TimerMap;
223 
225  TimerMap _timers;
226 
227  void* _reserved;
228  };
229 
230  class Selector : public SelectorBase
231  {
232  public:
233  Selector();
234 
235  virtual ~Selector();
236 
237  SelectorImpl& impl();
238 
239  protected:
240  void onAdd( Selectable& dev );
241 
242  void onRemove( Selectable& dev );
243 
244  void onReinit(Selectable&);
245 
246  void onChanged(Selectable&);
247 
248  bool onWaitUntil(Timespan timeout);
249 
250  void onWake();
251 
252  private:
254  class SelectorImpl* _impl;
255  };
256 
257 } //namespace cxxtools
258 
259 #endif