]> git.tdb.fi Git - libs/core.git/blob - source/io/eventdispatcher.h
04d037e29df5fe5085127a74b75a1bfe66d01ccb
[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 "poll.h"
8
9 namespace Msp {
10 namespace IO {
11
12 /**
13 Put your I/O objects inside one of these to get signaled when something happens
14 on some of them.
15 */
16 class EventDispatcher: public sigc::trackable
17 {
18 private:
19         struct Slot
20         {
21                 EventObject *obj;
22                 sigc::connection evch_conn;
23                 sigc::connection del_conn;
24
25                 Slot(EventObject *o): obj(o) { }
26         };
27
28         typedef std::map<EventObject *, Slot> SlotMap;
29
30         Poller poller;
31         SlotMap objects;
32
33 public:
34         EventDispatcher();
35
36         void add(EventObject &);
37         void remove(EventObject &);
38
39         /** Checks for and dispatches events.  If there are no events available,
40         blocks until there are. */
41         void tick();
42
43         /** Checks for and dispatches events.  If there are no events available,
44         waits at most the specified time before returning. */
45         void tick(const Time::TimeDelta &);
46
47 private:
48         void object_events_changed(PollEvent, EventObject *);
49         void object_deleted(EventObject *);
50         void dispatch();
51 };
52
53 } // namespace IO
54 } // namespace Msp
55
56 #endif