]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.h
Use nullptr instead of 0 for pointers
[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/noncopyable.h>
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         P_HANGUP = 16
22 };
23
24 inline PollEvent operator|(PollEvent e, PollEvent f)
25 { return PollEvent(static_cast<int>(e)|static_cast<int>(f)); }
26
27 inline PollEvent operator&(PollEvent e, PollEvent f)
28 { return PollEvent(static_cast<int>(e)&static_cast<int>(f)); }
29
30 inline PollEvent operator~(PollEvent e)
31 { return PollEvent(~static_cast<int>(e)); }
32
33
34 class Poller: private NonCopyable
35 {
36 public:
37         struct PolledObject
38         {
39                 EventObject *object = nullptr;
40                 PollEvent events = P_NONE;
41
42                 PolledObject(EventObject *o, PollEvent e): object(o), events(e) { }
43         };
44
45 private:
46         struct Private;
47
48         std::vector<PolledObject> objects;
49         Private *priv = nullptr;
50         bool events_changed = false;
51         bool objs_changed = false;
52         std::vector<PolledObject> poll_result;
53
54 public:
55         Poller();
56         ~Poller();
57
58         void set_object(EventObject &, PollEvent);
59         unsigned poll();
60         unsigned poll(const Time::TimeDelta &);
61 private:
62         void rebuild_array();
63         unsigned do_poll(int);
64         void platform_poll(int);
65 public:
66         const std::vector<PolledObject> &get_result() const { return poll_result; }
67 };
68
69 PollEvent poll(EventObject &, PollEvent);
70 PollEvent poll(EventObject &, PollEvent, const Time::TimeDelta &);
71
72 } // namespace IO
73 } // namespace Msp
74
75 #endif