]> git.tdb.fi Git - libs/core.git/blob - source/io/eventdispatcher.h
Refactor signal connection handling in EventDispatcher
[libs/core.git] / source / io / eventdispatcher.h
1 #ifndef MSP_IO_EVENTDISPATCHER_H_
2 #define MSP_IO_EVENTDISPATCHER_H_
3
4 #include <set>
5 #include <sigc++/trackable.h>
6 #include <msp/time/timedelta.h>
7 #include <msp/time/timer.h>
8 #include "poll.h"
9
10 namespace Msp {
11 namespace IO {
12
13 /**
14 Put your I/O objects inside one of these to get signaled when something happens
15 on some of them.
16 */
17 class EventDispatcher
18 {
19 private:
20         struct Slot: public sigc::trackable
21         {
22                 EventDispatcher &disp;
23                 EventObject &obj;
24
25                 Slot(EventDispatcher &, EventObject &);
26
27                 void connect_signals() const;
28                 void events_changed(PollEvent) const;
29                 void deleted() const;
30
31                 bool operator<(const Slot &o) const { return &obj<&o.obj; }
32         };
33
34         Poller poller;
35         std::set<Slot> objects;
36
37 public:
38         void add(EventObject &);
39         void remove(EventObject &);
40
41         /** Checks for and dispatches events.  If there are no events available,
42         blocks until there are. */
43         void tick();
44
45         /** Checks for and dispatches events.  If there are no events available,
46         waits at most the specified time before returning. */
47         void tick(const Time::TimeDelta &);
48
49         /** Checks for and dispatches events.  If there are no events available,
50         waits until the timer's next timeout before returning. */
51         void tick(const Time::Timer &);
52
53 private:
54         void dispatch();
55 };
56
57 } // namespace IO
58 } // namespace Msp
59
60 #endif