]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.h
Move files to prepare for assimilation into core
[libs/core.git] / source / io / 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
37 class Poller
38 {
39 public:
40         struct Slot
41         {
42                 Base *object;
43                 PollEvent events;
44
45                 Slot(Base *o, PollEvent e): object(o), events(e) { }
46         };
47
48         typedef std::list<Slot> SlotSeq;
49 private:
50         typedef std::map<Base *, Slot> SlotMap;
51
52 #ifdef WIN32
53         struct pollfd
54         {
55                 Handle fd;
56         };
57 #endif
58
59         SlotMap objects;
60         std::vector<pollfd> pfd;
61         bool pfd_dirty;
62         SlotSeq poll_result;
63
64         void rebuild_pfd();
65         int do_poll(int);
66
67 public:
68         Poller();
69
70         void set_object(Base &, PollEvent);
71         int poll();
72         int poll(const Time::TimeDelta &);
73         const SlotSeq &get_result() const { return poll_result; }
74 };
75
76 PollEvent poll(Base &, PollEvent);
77 PollEvent poll(Base &, PollEvent, const Time::TimeDelta &);
78
79 } // namespace IO
80 } // namespace Msp
81
82 #endif