]> git.tdb.fi Git - libs/core.git/blob - source/poll.h
Initial revision
[libs/core.git] / source / poll.h
1 /* $Id$
2
3 This file is part of libmspio
4 Copyright © 2007 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7 #ifndef MSP_IO_POLL_H_
8 #define MSP_IO_POLL_H_
9
10 #ifndef WIN32
11 #include <poll.h>
12 #endif
13 #include <list>
14 #include <map>
15 #include <vector>
16 #include <msp/time/timedelta.h>
17 #include "types.h"
18
19 namespace Msp {
20 namespace IO {
21
22 class Base;
23
24 enum PollEvent
25 {
26         P_NONE=0,
27         P_INPUT=1,
28         P_PRIO=2,
29         P_OUTPUT=4,
30         P_ERROR=8
31 };
32
33 inline PollEvent operator|(PollEvent e, PollEvent f)
34 { return PollEvent((int)e|(int)f); }
35
36 inline PollEvent operator&(PollEvent e, PollEvent f)
37 { return PollEvent((int)e&(int)f); }
38
39 inline PollEvent operator~(PollEvent e)
40 { return PollEvent(~(int)e); }
41
42 class Poller
43 {
44 public:
45         struct Slot
46         {
47                 Base *object;
48                 PollEvent events;
49
50                 Slot(Base *o, PollEvent e): object(o), events(e) { }
51         };
52         typedef std::list<Slot> SlotSeq;
53
54         Poller();
55         void set_object(Base &, PollEvent);
56         int  poll();
57         int  poll(const Time::TimeDelta &);
58         const SlotSeq &get_result() const { return poll_result; }
59 private:
60         typedef std::map<Base *, Slot> SlotMap;
61
62 #ifdef WIN32
63         struct pollfd
64         {
65                 Handle fd;
66         };
67 #endif
68
69         SlotMap objects;
70         std::vector<pollfd> pfd;
71         bool pfd_dirty;
72         SlotSeq poll_result;
73
74         void rebuild_pfd();
75         int do_poll(int);
76 };
77
78 PollEvent poll(Base &, PollEvent);
79 PollEvent poll(Base &, PollEvent, const Time::TimeDelta &);
80
81 } // namespace IO
82 } // namespace Msp
83
84 #endif