eventloop.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
3  * Copyright (C) 2006-2008 Marc Boris Duerner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 #ifndef CXXTOOLS_SYSTEM_EVENTLOOP_H
30 #define CXXTOOLS_SYSTEM_EVENTLOOP_H
31 
32 #include <cxxtools/event.h>
33 #include <cxxtools/signal.h>
34 #include <cxxtools/selector.h>
35 #include <cxxtools/eventsink.h>
36 #include <cxxtools/timespan.h>
37 
38 namespace cxxtools
39 {
40 
41  class Selectable;
42 
45  class EventLoopBase : public SelectorBase
46  , public EventSink
47  {
48  public:
51  virtual ~EventLoopBase()
52  {}
53 
56  void run()
57  { this->onRun(); }
58 
62  { this->onProcessEvents(); }
63 
66  void exit()
67  { this->onExit(); }
68 
72  { _timeout = msecs; }
73 
77  { return _timeout; }
78 
83  Signal<> timeout;
84 
87  Signal<> idle;
88 
91  Signal<const Event&> event;
92 
95  Signal<> exited;
96 
97  protected:
101  : _timeout(WaitInfinite)
102  {}
103 
104  virtual void onRun() = 0;
105 
106  virtual void onExit() = 0;
107 
108  virtual void onProcessEvents() = 0;
109 
110  private:
111  Timespan _timeout;
112  };
113 
140  class EventLoop : public EventLoopBase
141  {
142  public:
145  EventLoop();
146 
149  virtual ~EventLoop();
150 
151  protected:
152  virtual void onAdd( Selectable& s );
153 
154  virtual void onRemove( Selectable& s );
155 
156  virtual void onReinit(Selectable& s);
157 
158  virtual void onChanged(Selectable& s);
159 
160  virtual void onRun();
161 
162  virtual bool onWaitUntil(Timespan timeout);
163 
164  virtual void onWake();
165 
166  virtual void onExit();
167 
168  virtual void onQueueEvent(const Event& event, bool priority);
169 
170  virtual void onCommitEvent(const Event& event, bool priority);
171 
172  virtual void onProcessEvents();
173 
174  private:
175  class Impl;
176  Impl* _impl;
177  };
178 
179 } // namespace cxxtools
180 
181 #endif