]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.h
09e00e5bbedb6313bea0832d09eb0e451a5ffe3d
[libs/core.git] / source / io / poll.h
1 #ifndef MSP_IO_POLL_H_
2 #define MSP_IO_POLL_H_
3
4 #include <list>
5 #include <map>
6 #include <vector>
7 #include <msp/time/timedelta.h>
8
9 namespace Msp {
10 namespace IO {
11
12 class EventObject;
13
14 enum PollEvent
15 {
16         P_NONE = 0,
17         P_INPUT = 1,
18         P_PRIO = 2,
19         P_OUTPUT = 4,
20         P_ERROR = 8
21 };
22
23 inline PollEvent operator|(PollEvent e, PollEvent f)
24 { return PollEvent(static_cast<int>(e)|static_cast<int>(f)); }
25
26 inline PollEvent operator&(PollEvent e, PollEvent f)
27 { return PollEvent(static_cast<int>(e)&static_cast<int>(f)); }
28
29 inline PollEvent operator~(PollEvent e)
30 { return PollEvent(~static_cast<int>(e)); }
31
32
33 class Poller
34 {
35 public:
36         struct Slot
37         {
38                 EventObject *object;
39                 PollEvent events;
40
41                 Slot(EventObject *o, PollEvent e): object(o), events(e) { }
42         };
43
44         typedef std::list<Slot> SlotList;
45 private:
46         typedef std::map<EventObject *, PollEvent> EventMap;
47
48         struct Private;
49
50         EventMap objects;
51         Private *priv;
52         bool objs_changed;
53         SlotList poll_result;
54
55 public:
56         Poller();
57         ~Poller();
58
59         void set_object(EventObject &, PollEvent);
60         int poll();
61         int poll(const Time::TimeDelta &);
62 private:
63         void rebuild_array();
64         int do_poll(int);
65         void platform_poll(int);
66 public:
67         const SlotList &get_result() const { return poll_result; }
68 };
69
70 PollEvent poll(EventObject &, PollEvent);
71 PollEvent poll(EventObject &, PollEvent, const Time::TimeDelta &);
72
73 } // namespace IO
74 } // namespace Msp
75
76 #endif