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 
102 
106  { return _event; }
107 
111  { return _exited; }
112 
120  void post(Selectable& s)
121  {
122  onReady(s);
123  wake();
124  }
125 
128  {
129  onReady(s);
130  }
131 
133  virtual Selector& selector() = 0;
134 
135  protected:
137  EventLoop();
138 
140  virtual void onRun() = 0;
141 
143  virtual void onExit() = 0;
144 
146  virtual void onCommitEvent(const Event& ev) = 0;
147 
149  virtual void onQueueEvent(const Event& ev) = 0;
150 
152  virtual void onWake() = 0;
153 
155  virtual void onProcessEvents() = 0;
156 
158  virtual void onAttachTimer(Timer& timer) = 0;
159 
161  virtual void onDetachTimer(Timer& timer) = 0;
162 
164  virtual void onAttachSelectable(Selectable&) = 0;
165 
167  virtual void onDetachSelectable(Selectable&) = 0;
168 
170  virtual void onReady(Selectable&) = 0;
171 
173  virtual void onCancel(Selectable&) = 0;
174 
175  private:
176  Signal<> _exited;
177  Signal<const Event&> _event;
178 };
179 
181 class PT_SYSTEM_API EventQueue
182 {
183  public:
184  EventQueue();
185 
186  EventQueue(Allocator& a);
187 
188  virtual ~EventQueue();
189 
190  Allocator& allocator()
191  { return *_usedalloc; }
192 
193  void exit();
194 
195  void pushEvent(const Event& event);
196 
197  bool processEvents(Signal<const Event&>& eventSignal);
198 
199  private:
200  Mutex _mutex;
201  Allocator _allocator;
202  Allocator* _usedalloc;
203  std::deque<Event*> _eventQueue;
204  bool _exited;
205 };
206 
208 class PT_SYSTEM_API TimerQueue
209 {
210  typedef std::multimap<Timespan, Timer*> TimerMap;
211 
212  public:
213  TimerQueue();
214 
215  virtual ~TimerQueue();
216 
217  void addTimer(Timer& timer);
218 
219  void removeTimer(Timer& timer);
220 
221  std::size_t processTimers();
222 
223  private:
224  TimerMap _timers;
225 };
226 
227 } // namespace System
228 
229 } // namespace Pt
230 
231 #endif // PT_SYSTEM_EVENTLOOP_H
Core module.
Definition: Allocator.h:33
Base class for all event types.
Definition: Event.h:50
void run()
Starts the loop.
Notifies clients in constant intervals.
Definition: Timer.h:73
Signal & exited()
Emited when the eventloop is exited.
Definition: EventLoop.h:110
Multicast Signal to call multiple slots.
Definition: Api-Signal.h:111
void processEvents()
Process all queued events.
virtual void onQueueEvent(const Event &ev)=0
Only queues an event.
void setReady(Selectable &s)
Sets the Selectable as ready without waking the loop.
Definition: EventLoop.h:127
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:50
void exit()
Stops the loop.
virtual void onWake()=0
Triggers event processing.
Dispatches operations through an event loop.
Definition: Selectable.h:45
Allocator interface.
Definition: Allocator.h:82
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:74
virtual ~EventLoop()
Destructor.
virtual void onCommitEvent(const Event &ev)=0
Queues an event and triggers event processing.
void post(Selectable &s)
Posts the loop to run a selectable.
Definition: EventLoop.h:120
Receiver for events.
Definition: EventSink.h:46
Signal< const Event & > & eventReceived()
Reports all events.
Definition: EventLoop.h:105