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