]> git.tdb.fi Git - libs/core.git/blob - source/poll.h
Drop copyright and license notices from files
[libs/core.git] / source / poll.h
1 #ifndef MSP_IO_POLL_H_
2 #define MSP_IO_POLL_H_
3
4 #ifndef WIN32
5 #include <poll.h>
6 #endif
7 #include <list>
8 #include <map>
9 #include <vector>
10 #include <msp/time/timedelta.h>
11 #include "types.h"
12
13 namespace Msp {
14 namespace IO {
15
16 class Base;
17
18 enum PollEvent
19 {
20         P_NONE = 0,
21         P_INPUT = 1,
22         P_PRIO = 2,
23         P_OUTPUT = 4,
24         P_ERROR = 8
25 };
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, PollEvent f)
31 { return PollEvent(static_cast<int>(e)&static_cast<int>(f)); }
32
33 inline PollEvent operator~(PollEvent e)
34 { return PollEvent(~static_cast<int>(e)); }
35
36 class Poller
37 {
38 public:
39         struct Slot
40         {
41                 Base *object;
42                 PollEvent events;
43
44                 Slot(Base *o, PollEvent e): object(o), events(e) { }
45         };
46         typedef std::list<Slot> SlotSeq;
47
48         Poller();
49         void set_object(Base &, PollEvent);
50         int  poll();
51         int  poll(const Time::TimeDelta &);
52         const SlotSeq &get_result() const { return poll_result; }
53 private:
54         typedef std::map<Base *, Slot> SlotMap;
55
56 #ifdef WIN32
57         struct pollfd
58         {
59                 Handle fd;
60         };
61 #endif
62
63         SlotMap objects;
64         std::vector<pollfd> pfd;
65         bool pfd_dirty;
66         SlotSeq poll_result;
67
68         void rebuild_pfd();
69         int do_poll(int);
70 };
71
72 PollEvent poll(Base &, PollEvent);
73 PollEvent poll(Base &, PollEvent, const Time::TimeDelta &);
74
75 } // namespace IO
76 } // namespace Msp
77
78 #endif