… | |
… | |
33 | struct io_watcher; |
33 | struct io_watcher; |
34 | struct time_watcher; |
34 | struct time_watcher; |
35 | |
35 | |
36 | class io_manager { |
36 | class io_manager { |
37 | vector<pollfd> pfs; |
37 | vector<pollfd> pfs; |
38 | vector<io_watcher *> iow; |
38 | vector<const io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
39 | vector<time_watcher *> tw; // actually a heap |
|
|
40 | |
|
|
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
40 | public: |
42 | public: |
|
|
43 | |
|
|
44 | void reschedule_time_watchers (); |
41 | |
45 | |
42 | // register a watcher |
46 | // register a watcher |
43 | void reg (int fd, short events, io_watcher *w); |
47 | void reg (int fd, short events, io_watcher *w); |
44 | void unreg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
45 | void reg (time_watcher *w); |
49 | void reg (time_watcher *w); |
… | |
… | |
60 | void *obj; |
64 | void *obj; |
61 | R (object::*meth)(A arg); |
65 | R (object::*meth)(A arg); |
62 | |
66 | |
63 | // a proxy is a kind of recipe on how to call a specific class method |
67 | // a proxy is a kind of recipe on how to call a specific class method |
64 | struct proxy_base { |
68 | struct proxy_base { |
65 | virtual R call (void *obj, void (object::*meth)(A), A arg) = 0; |
69 | virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
66 | }; |
70 | }; |
67 | template<class O1, class O2> |
71 | template<class O1, class O2> |
68 | struct proxy : proxy_base { |
72 | struct proxy : proxy_base { |
69 | virtual R call (void *obj, void (object::*meth)(A), A arg) |
73 | virtual R call (void *obj, R (object::*meth)(A), A arg) |
70 | { |
74 | { |
71 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
75 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
72 | (arg); |
76 | (arg); |
73 | } |
77 | } |
74 | }; |
78 | }; |
75 | |
79 | |
76 | proxy_base *prxy; |
80 | proxy_base *prxy; |
77 | |
81 | |
78 | public: |
82 | public: |
79 | template<class O1, class O2> |
83 | template<class O1, class O2> |
80 | callback (O1 *object, void (O2::*method)(A)) |
84 | callback (O1 *object, R (O2::*method)(A)) |
81 | { |
85 | { |
82 | static proxy<O1,O2> p; |
86 | static proxy<O1,O2> p; |
83 | obj = reinterpret_cast<void *>(object); |
87 | obj = reinterpret_cast<void *>(object); |
84 | meth = reinterpret_cast<void (object::*)(A)>(method); |
88 | meth = reinterpret_cast<R (object::*)(A)>(method); |
85 | prxy = &p; |
89 | prxy = &p; |
86 | } |
90 | } |
87 | |
91 | |
88 | R call(A arg) |
92 | R call(A arg) const |
89 | { |
93 | { |
90 | return prxy->call (obj, meth, arg); |
94 | return prxy->call (obj, meth, arg); |
91 | } |
95 | } |
92 | |
96 | |
93 | void stop () |
97 | R operator ()(A arg) const |
94 | { |
98 | { |
95 | iom.unreg (this); |
99 | return call (arg); |
96 | } |
100 | } |
97 | }; |
101 | }; |
98 | |
102 | |
99 | struct io_watcher : callback<void, short> { |
103 | struct io_watcher : callback<void, short> { |
100 | template<class O1, class O2> |
104 | template<class O1, class O2> |
101 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
105 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
102 | : callback<void, short>(object,method) |
106 | : callback<void, short>(object,method) |
103 | { } |
107 | { } |
104 | |
108 | |
|
|
109 | ~io_watcher () |
|
|
110 | { |
|
|
111 | iom.unreg (this); |
|
|
112 | } |
|
|
113 | |
105 | void start (int fd, short events) |
114 | void start (int fd, short events) |
106 | { |
115 | { |
107 | iom.reg (fd, events, this); |
116 | iom.reg (fd, events, this); |
108 | } |
117 | } |
109 | |
118 | |
|
|
119 | void stop () |
|
|
120 | { |
|
|
121 | iom.unreg (this); |
|
|
122 | } |
110 | }; |
123 | }; |
111 | |
124 | |
|
|
125 | #define TSTAMP_CANCEL -1. |
|
|
126 | |
112 | struct time_watcher : callback<void, tstamp &> { |
127 | struct time_watcher : callback<void, tstamp &> { |
|
|
128 | bool registered; // already registered? |
113 | tstamp at; |
129 | tstamp at; |
114 | |
130 | |
115 | template<class O1, class O2> |
131 | template<class O1, class O2> |
116 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
132 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
117 | : callback<void, tstamp &>(object,method) |
133 | : callback<void, tstamp &>(object,method) |
|
|
134 | , registered(false) |
118 | { } |
135 | { } |
119 | |
136 | |
120 | void set (tstamp when); |
137 | ~time_watcher () |
121 | |
|
|
122 | void trigger () |
|
|
123 | { |
138 | { |
124 | call (at); |
139 | iom.unreg (this); |
125 | } |
140 | } |
126 | |
141 | |
|
|
142 | void set (tstamp when); |
|
|
143 | void trigger (); |
|
|
144 | |
|
|
145 | void operator ()() |
|
|
146 | { |
|
|
147 | trigger (); |
|
|
148 | } |
|
|
149 | |
|
|
150 | void start (); |
127 | void start (tstamp when = NOW) |
151 | void start (tstamp when) |
128 | { |
152 | { |
129 | set (when); |
153 | set (when); |
|
|
154 | } |
|
|
155 | |
|
|
156 | void stop () |
|
|
157 | { |
|
|
158 | iom.unreg (this); |
|
|
159 | } |
|
|
160 | |
|
|
161 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
162 | { |
|
|
163 | stop (); |
|
|
164 | at = when; |
130 | } |
165 | } |
131 | }; |
166 | }; |
132 | |
167 | |
133 | #endif |
168 | #endif |
134 | |
169 | |