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