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