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/noncopyable.h>
35 #include <cxxtools/connectable.h>
36 #include <cxxtools/api.h>
37 #include <map>
38 
39 namespace cxxtools {
40 
41  class Timer;
42  class Selectable;
43  class Application;
44  class SelectorImpl;
45 
83  , protected NonCopyable
84  {
85  friend class Selectable;
86  friend class Timer;
87 
88  public:
89  static const std::size_t WaitInfinite = static_cast<const std::size_t>(-1);
90 
92  virtual ~SelectorBase();
93 
99  void add(Selectable& s);
100 
103  void remove(Selectable& s);
104 
112  void add(Timer& timer);
113 
118  void remove(Timer& timer);
119 
129  bool wait(std::size_t msecs = WaitInfinite);
130 
137  void wake();
138 
139  protected:
141  SelectorBase();
142 
143  void onAddTimer(Timer& timer);
144 
145  void onRemoveTimer( Timer& timer );
146 
147  void onTimerChanged( Timer& timer );
148 
153  virtual void onAdd(Selectable&) = 0;
154 
159  virtual void onRemove(Selectable&) = 0;
160 
165  virtual void onReinit(Selectable&) = 0;
166 
171  virtual void onChanged(Selectable& s) = 0;
172 
173  virtual bool onWait(std::size_t msecs) = 0;
174 
175  virtual void onWake() = 0;
176 
177  private:
182  bool updateTimer(size_t& timeout);
183 
185  typedef std::multimap<Timespan, Timer*> TimerMap;
186 
188  TimerMap _timers;
189 
190  void* _reserved;
191  };
192 
194  {
195  public:
196  Selector();
197 
198  virtual ~Selector();
199 
200  SelectorImpl& impl();
201 
202  protected:
203  void onAdd( Selectable& dev );
204 
205  void onRemove( Selectable& dev );
206 
207  void onReinit(Selectable&);
208 
209  void onChanged(Selectable&);
210 
211  bool onWait(std::size_t msecs = WaitInfinite);
212 
213  void onWake();
214 
215  private:
217  class SelectorImpl* _impl;
218  };
219 
220 } //namespace cxxtools
221 
222 #endif