]> git.tdb.fi Git - libs/core.git/blob - source/io/eventdispatcher.h
b542d67e74d3fa896811b4a04263e9554e4f8902
[libs/core.git] / source / io / eventdispatcher.h
1 #ifndef MSP_IO_EVENTDISPATCHER_H_
2 #define MSP_IO_EVENTDISPATCHER_H_
3
4 #include <sigc++/connection.h>
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: public sigc::trackable
18 {
19 private:
20         struct Slot
21         {
22                 EventObject *obj;
23                 sigc::connection evch_conn;
24                 sigc::connection del_conn;
25
26                 Slot(EventObject *o): obj(o) { }
27         };
28
29         typedef std::map<EventObject *, Slot> SlotMap;
30
31         Poller poller;
32         SlotMap objects;
33
34 public:
35         EventDispatcher();
36         ~EventDispatcher();
37
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 object_events_changed(PollEvent, EventObject *);
55         void object_deleted(EventObject *);
56         void dispatch();
57 };
58
59 } // namespace IO
60 } // namespace Msp
61
62 #endif