]> git.tdb.fi Git - libs/core.git/blob - source/io/poll.cpp
Mark overridden virtual functions as such
[libs/core.git] / source / io / poll.cpp
1 #include <stdexcept>
2 #include <msp/core/algorithm.h>
3 #include "eventobject.h"
4 #include "poll.h"
5 #include "poll_platform.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace IO {
11
12 Poller::Poller():
13         priv(new Private)
14 { }
15
16 Poller::~Poller()
17 {
18         delete priv;
19 }
20
21 void Poller::set_object(EventObject &obj, PollEvent ev)
22 {
23         // Verify that the object has an event handle
24         if(ev)
25                 obj.get_event_handle();
26
27         auto i = find_member(objects, &obj, &PolledObject::object);
28         if(i!=objects.end())
29         {
30                 if(ev)
31                         i->events = ev;
32                 else
33                 {
34                         *i = objects.back();
35                         objects.pop_back();
36                         objs_changed = true;
37                 }
38                 events_changed = true;
39                 return;
40         }
41
42         if(!ev)
43                 return;
44
45 #ifdef _WIN32
46         if(objects.size()>=MAXIMUM_WAIT_OBJECTS)
47                 throw logic_error("Maximum number of wait objects reached");
48 #endif
49
50         objects.push_back(PolledObject(&obj, ev));
51         objs_changed = true;
52 }
53
54 unsigned Poller::poll()
55 {
56         return do_poll(-1);
57 }
58
59 unsigned Poller::poll(const Time::TimeDelta &timeout)
60 {
61         if(timeout<Time::zero)
62                 throw invalid_argument("Poller::poll");
63
64         return do_poll(static_cast<int>(timeout/Time::msec));
65 }
66
67 unsigned Poller::do_poll(int timeout)
68 {
69         if(objs_changed || events_changed)
70         {
71                 rebuild_array();
72                 events_changed = false;
73                 objs_changed = false;
74         }
75
76         poll_result.clear();
77
78         platform_poll(timeout);
79
80         return poll_result.size();
81 }
82
83
84 PollEvent platform_poll(EventObject &, PollEvent, int);
85
86 PollEvent poll(EventObject &obj, PollEvent pe)
87 {
88         return platform_poll(obj, pe, -1);
89 }
90
91 PollEvent poll(EventObject &obj, PollEvent pe, const Time::TimeDelta &timeout)
92 {
93         if(timeout<Time::zero)
94                 throw invalid_argument("poll");
95
96         return platform_poll(obj, pe, static_cast<int>(timeout/Time::msec));
97 }
98
99 } // namespace IO
100 } // namespace Msp