]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.h
Make sure all classes have sensible copy semantics
[libs/core.git] / source / io / poll.h
1 #ifndef MSP_IO_POLL_H_
2 #define MSP_IO_POLL_H_
3
4 #include <list>
5 #include <map>
6 #include <vector>
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::list<Slot> SlotList;
47 private:
48         typedef std::map<EventObject *, PollEvent> EventMap;
49
50         struct Private;
51
52         EventMap objects;
53         Private *priv;
54         bool objs_changed;
55         SlotList poll_result;
56
57 public:
58         Poller();
59         ~Poller();
60
61         void set_object(EventObject &, PollEvent);
62         int poll();
63         int poll(const Time::TimeDelta &);
64 private:
65         void rebuild_array();
66         int do_poll(int);
67         void platform_poll(int);
68 public:
69         const SlotList &get_result() const { return poll_result; }
70 };
71
72 PollEvent poll(EventObject &, PollEvent);
73 PollEvent poll(EventObject &, PollEvent, const Time::TimeDelta &);
74
75 } // namespace IO
76 } // namespace Msp
77
78 #endif