00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef CXXTOOLS_SYSTEM_EVENTLOOP_H
00030 #define CXXTOOLS_SYSTEM_EVENTLOOP_H
00031
00032 #include <cxxtools/event.h>
00033 #include <cxxtools/signal.h>
00034 #include <cxxtools/allocator.h>
00035 #include <cxxtools/api.h>
00036 #include <cxxtools/mutex.h>
00037 #include <cxxtools/selector.h>
00038 #include <cxxtools/eventsink.h>
00039 #include <deque>
00040
00041 namespace cxxtools {
00042
00043 class Selectable;
00044
00047 class EventLoopBase : public SelectorBase
00048 , public EventSink
00049 {
00050 public:
00053 virtual ~EventLoopBase()
00054 {}
00055
00058 void run()
00059 { this->onRun(); }
00060
00063 void processEvents()
00064 { this->onProcessEvents(); }
00065
00068 void exit()
00069 { this->onExit(); }
00070
00073 void setIdleTimeout(size_t msecs)
00074 { _timeout = msecs; }
00075
00078 unsigned int idleTimeout() const
00079 { return _timeout; }
00080
00085 Signal<> timeout;
00086
00090 Signal<const Event&> event;
00091
00094 Signal<> exited;
00095
00096 protected:
00099 EventLoopBase()
00100 : _timeout(WaitInfinite)
00101 {}
00102
00103 virtual void onRun() = 0;
00104
00105 virtual void onExit() = 0;
00106
00107 virtual void onProcessEvents() = 0;
00108
00109 private:
00110 size_t _timeout;
00111 };
00112
00139 class CXXTOOLS_API EventLoop : public EventLoopBase
00140 {
00141 public:
00144 EventLoop();
00145
00148 virtual ~EventLoop();
00149
00150 protected:
00151 virtual void onAdd( Selectable& s );
00152
00153 virtual void onRemove( Selectable& s );
00154
00155 virtual void onReinit(Selectable& s);
00156
00157 virtual void onChanged(Selectable& s);
00158
00159 virtual void onRun();
00160
00161 virtual bool onWait(std::size_t msecs);
00162
00163 virtual void onWake();
00164
00165 virtual void onExit();
00166
00167 virtual void onCommitEvent(const Event& event);
00168
00169 virtual void onProcessEvents();
00170
00171 private:
00172 bool _exitLoop;
00173 SelectorImpl* _selector;
00174 Allocator _allocator;
00175 std::deque<Event* > _eventQueue;
00176 RecursiveMutex _queueMutex;
00177 };
00178
00179 }
00180
00181 #endif