EventLoop.h
1 /*
2  * Copyright (C) 2006-2013 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_SYSTEM_EVENTLOOP_H
30 #define PT_SYSTEM_EVENTLOOP_H
31 
32 #include <Pt/Event.h>
33 #include <Pt/Signal.h>
34 #include <Pt/Timespan.h>
35 #include <Pt/Allocator.h>
36 #include <Pt/Connectable.h>
37 #include <Pt/System/Api.h>
38 #include <Pt/System/Mutex.h>
39 #include <Pt/System/Timer.h>
40 #include <Pt/System/EventSink.h>
41 #include <map>
42 #include <deque>
43 
44 namespace Pt {
45 
46 namespace System {
47 
48 class Timer;
49 class Selectable;
50 class Selector;
51 
72 class PT_SYSTEM_API EventLoop : public Connectable
73  , public EventSink
74 {
75  friend class Selectable;
76  friend class Timer;
77 
78  public:
81  static const std::size_t WaitInfinite = static_cast<const std::size_t>(-1);
82 
85  static const std::size_t WaitMax = WaitInfinite - 1;
86 
89  virtual ~EventLoop();
90 
93  void run();
94 
97  void exit();
98 
101  void processEvents();
102 
106  { return _event; }
107 
111  { return _exited; }
112 
121  void post(Selectable& s)
122  { this->onReady(s); }
123 
125  void setReady(Selectable& s)
126  { this->onReady(s); }
127 
129  virtual Selector& selector() = 0;
130 
131  protected:
133  EventLoop();
134 
136  virtual void onRun() = 0;
137 
139  virtual void onExit() = 0;
140 
142  virtual void onCommitEvent(const Event& ev) = 0;
143 
145  virtual void onQueueEvent(const Event& ev) = 0;
146 
148  virtual void onWake() = 0;
149 
151  virtual void onProcessEvents() = 0;
152 
154  virtual void onAttachTimer(Timer& timer) = 0;
155 
157  virtual void onDetachTimer(Timer& timer) = 0;
158 
160  virtual void onAttachSelectable(Selectable&) = 0;
161 
163  virtual void onDetachSelectable(Selectable&) = 0;
164 
166  virtual void onReady(Selectable&) = 0;
167 
169  virtual void onCancel(Selectable&) = 0;
170 
171  private:
172  Signal<> _exited;
173  Signal<const Event&> _event;
174 };
175 
177 class PT_SYSTEM_API EventQueue
178 {
179  public:
180  EventQueue();
181 
182  EventQueue(Allocator& a);
183 
184  virtual ~EventQueue();
185 
186  Allocator& allocator()
187  { return *_usedalloc; }
188 
189  void exit();
190 
191  void pushEvent(const Event& event);
192 
193  bool processEvents(Signal<const Event&>& eventSignal);
194 
195  private:
196  Mutex _mutex;
197  Allocator _allocator;
198  Allocator* _usedalloc;
199  std::deque<Event*> _eventQueue;
200  bool _exited;
201 };
202 
204 class PT_SYSTEM_API TimerQueue
205 {
206  typedef std::multimap<Timespan, Timer*> TimerMap;
207 
208  public:
209  TimerQueue();
210 
211  virtual ~TimerQueue();
212 
213  void addTimer(Timer& timer);
214 
215  void removeTimer(Timer& timer);
216 
217  std::size_t processTimers();
218 
219  private:
220  TimerMap _timers;
221 };
222 
223 } // namespace System
224 
225 } // namespace Pt
226 
227 #endif // PT_SYSTEM_EVENTLOOP_H
void post(Selectable &s)
Calls the selectables run function in the loop thread.
Definition: EventLoop.h:121
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:49
Receiver for events.
Definition: EventSink.h:45
Base class for all event types.
Definition: Event.h:49
Dispatches operations through an event loop.
Definition: Selectable.h:44
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Signal< const Event & > & eventReceived()
Reports all events.
Definition: EventLoop.h:105
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
Signal & exited()
Emited when the eventloop is exited.
Definition: EventLoop.h:110
Notifies clients in constant intervals.
Definition: Timer.h:72