]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.h
f59b499ddbc3bad3702c42a56507c32969ddac84
[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 Base;
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                 Base *object;
39                 PollEvent events;
40
41                 Slot(Base *o, PollEvent e): object(o), events(e) { }
42         };
43
44         typedef std::list<Slot> SlotList;
45 private:
46         typedef std::map<Base *, 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
58         void set_object(Base &, PollEvent);
59         int poll();
60         int poll(const Time::TimeDelta &);
61 private:
62         void rebuild_array();
63         int do_poll(int);
64 public:
65         const SlotList &get_result() const { return poll_result; }
66 };
67
68 PollEvent poll(Base &, PollEvent);
69 PollEvent poll(Base &, PollEvent, const Time::TimeDelta &);
70
71 } // namespace IO
72 } // namespace Msp
73
74 #endif