]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.h
Use braced initializer lists in place of constructors when possible
[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;
40                 PollEvent events;
41         };
42
43 private:
44         struct Private;
45
46         std::vector<PolledObject> objects;
47         Private *priv = nullptr;
48         bool events_changed = false;
49         bool objs_changed = false;
50         std::vector<PolledObject> poll_result;
51
52 public:
53         Poller();
54         ~Poller();
55
56         void set_object(EventObject &, PollEvent);
57         unsigned poll();
58         unsigned poll(const Time::TimeDelta &);
59 private:
60         void rebuild_array();
61         unsigned do_poll(int);
62         void platform_poll(int);
63 public:
64         const std::vector<PolledObject> &get_result() const { return poll_result; }
65 };
66
67 PollEvent poll(EventObject &, PollEvent);
68 PollEvent poll(EventObject &, PollEvent, const Time::TimeDelta &);
69
70 } // namespace IO
71 } // namespace Msp
72
73 #endif