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