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/allocator.h>
35 #include <cxxtools/api.h>
36 #include <cxxtools/mutex.h>
37 #include <cxxtools/selector.h>
38 #include <cxxtools/eventsink.h>
39 #include <deque>
40 
41 namespace cxxtools {
42 
43  class Selectable;
44 
47  class EventLoopBase : public SelectorBase
48  , public EventSink
49  {
50  public:
53  virtual ~EventLoopBase()
54  {}
55 
58  void run()
59  { this->onRun(); }
60 
64  { this->onProcessEvents(); }
65 
68  void exit()
69  { this->onExit(); }
70 
73  void setIdleTimeout(size_t msecs)
74  { _timeout = msecs; }
75 
78  unsigned int idleTimeout() const
79  { return _timeout; }
80 
85  Signal<> timeout;
86 
90  Signal<const Event&> event;
91 
94  Signal<> exited;
95 
96  protected:
100  : _timeout(WaitInfinite)
101  {}
102 
103  virtual void onRun() = 0;
104 
105  virtual void onExit() = 0;
106 
107  virtual void onProcessEvents() = 0;
108 
109  private:
110  size_t _timeout;
111  };
112 
140  {
141  public:
144  EventLoop();
145 
148  virtual ~EventLoop();
149 
150  protected:
151  virtual void onAdd( Selectable& s );
152 
153  virtual void onRemove( Selectable& s );
154 
155  virtual void onReinit(Selectable& s);
156 
157  virtual void onChanged(Selectable& s);
158 
159  virtual void onRun();
160 
161  virtual bool onWait(std::size_t msecs);
162 
163  virtual void onWake();
164 
165  virtual void onExit();
166 
167  virtual void onCommitEvent(const Event& event);
168 
169  virtual void onProcessEvents();
170 
171  private:
172  bool _exitLoop;
173  SelectorImpl* _selector;
174  Allocator _allocator;
175  std::deque<Event* > _eventQueue;
176  RecursiveMutex _queueMutex;
177  };
178 
179 } // namespace cxxtools
180 
181 #endif