… | |
… | |
91 | |
91 | |
92 | #endif |
92 | #endif |
93 | |
93 | |
94 | /* using a template here would require quite a bit more lines, |
94 | /* using a template here would require quite a bit more lines, |
95 | * so a macro solution was chosen */ |
95 | * so a macro solution was chosen */ |
96 | #define EV_DECLARE_WATCHER(cppstem,cstem) \ |
96 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
97 | \ |
|
|
98 | extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \ |
|
|
99 | \ |
97 | \ |
100 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
98 | struct cppstem : ev_ ## cstem, callback<cppstem> \ |
101 | { \ |
99 | { \ |
102 | EV_CONSTRUCT (cppstem) \ |
100 | EV_CONSTRUCT (cppstem) \ |
103 | { \ |
101 | { \ |
104 | ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \ |
102 | ev_init (static_cast<ev_ ## cstem *>(this), thunk); \ |
105 | } \ |
103 | } \ |
106 | \ |
104 | \ |
107 | bool is_active () const \ |
105 | bool is_active () const \ |
108 | { \ |
106 | { \ |
109 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
107 | return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \ |
… | |
… | |
125 | } \ |
123 | } \ |
126 | \ |
124 | \ |
127 | void operator ()(int events = EV_UNDEF) \ |
125 | void operator ()(int events = EV_UNDEF) \ |
128 | { \ |
126 | { \ |
129 | return call (this, events); \ |
127 | return call (this, events); \ |
|
|
128 | } \ |
|
|
129 | \ |
|
|
130 | ~cppstem () \ |
|
|
131 | { \ |
|
|
132 | stop (); \ |
130 | } \ |
133 | } \ |
131 | \ |
134 | \ |
132 | private: \ |
135 | private: \ |
133 | \ |
136 | \ |
134 | cppstem (const cppstem &o) \ |
137 | cppstem (const cppstem &o) \ |
135 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
138 | : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ |
136 | { /* disabled */ } \ |
139 | { /* disabled */ } \ |
|
|
140 | \ |
137 | void operator =(const cppstem &o) { /* disabled */ } \ |
141 | void operator =(const cppstem &o) { /* disabled */ } \ |
138 | \ |
142 | \ |
|
|
143 | static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \ |
|
|
144 | { \ |
|
|
145 | (*static_cast<cppstem *>(w))(revents); \ |
|
|
146 | } \ |
|
|
147 | \ |
139 | public: |
148 | public: |
140 | |
149 | |
|
|
150 | #define EV_END_WATCHER(cppstem,cstem) \ |
|
|
151 | }; |
|
|
152 | |
141 | EV_DECLARE_WATCHER (io, io) |
153 | EV_BEGIN_WATCHER (io, io) |
142 | void set (int fd, int events) |
154 | void set (int fd, int events) |
143 | { |
155 | { |
144 | int active = is_active (); |
156 | int active = is_active (); |
145 | if (active) stop (); |
157 | if (active) stop (); |
146 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
158 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
… | |
… | |
158 | void start (int fd, int events) |
170 | void start (int fd, int events) |
159 | { |
171 | { |
160 | set (fd, events); |
172 | set (fd, events); |
161 | start (); |
173 | start (); |
162 | } |
174 | } |
163 | }; |
175 | EV_END_WATCHER (io, io) |
164 | |
176 | |
165 | EV_DECLARE_WATCHER (timer, timer) |
177 | EV_BEGIN_WATCHER (timer, timer) |
166 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
178 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
167 | { |
179 | { |
168 | int active = is_active (); |
180 | int active = is_active (); |
169 | if (active) stop (); |
181 | if (active) stop (); |
170 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
182 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
… | |
… | |
179 | |
191 | |
180 | void again () |
192 | void again () |
181 | { |
193 | { |
182 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
194 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
183 | } |
195 | } |
184 | }; |
196 | EV_END_WATCHER (timer, timer) |
185 | |
197 | |
|
|
198 | #if EV_PERIODICS |
186 | EV_DECLARE_WATCHER (periodic, periodic) |
199 | EV_BEGIN_WATCHER (periodic, periodic) |
187 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
200 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
188 | { |
201 | { |
189 | int active = is_active (); |
202 | int active = is_active (); |
190 | if (active) stop (); |
203 | if (active) stop (); |
191 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
204 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
… | |
… | |
200 | |
213 | |
201 | void again () |
214 | void again () |
202 | { |
215 | { |
203 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
216 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
204 | } |
217 | } |
205 | }; |
218 | EV_END_WATCHER (periodic, periodic) |
|
|
219 | #endif |
206 | |
220 | |
|
|
221 | EV_BEGIN_WATCHER (idle, idle) |
207 | EV_DECLARE_WATCHER (idle, idle) |
222 | EV_END_WATCHER (idle, idle) |
208 | }; |
|
|
209 | |
223 | |
|
|
224 | EV_BEGIN_WATCHER (prepare, prepare) |
210 | EV_DECLARE_WATCHER (prepare, prepare) |
225 | EV_END_WATCHER (prepare, prepare) |
211 | }; |
|
|
212 | |
226 | |
|
|
227 | EV_BEGIN_WATCHER (check, check) |
213 | EV_DECLARE_WATCHER (check, check) |
228 | EV_END_WATCHER (check, check) |
214 | }; |
|
|
215 | |
229 | |
216 | EV_DECLARE_WATCHER (sig, signal) |
230 | EV_BEGIN_WATCHER (sig, signal) |
217 | void set (int signum) |
231 | void set (int signum) |
218 | { |
232 | { |
219 | int active = is_active (); |
233 | int active = is_active (); |
220 | if (active) stop (); |
234 | if (active) stop (); |
221 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
235 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
… | |
… | |
225 | void start (int signum) |
239 | void start (int signum) |
226 | { |
240 | { |
227 | set (signum); |
241 | set (signum); |
228 | start (); |
242 | start (); |
229 | } |
243 | } |
230 | }; |
244 | EV_END_WATCHER (sig, signal) |
231 | |
245 | |
232 | EV_DECLARE_WATCHER (child, child) |
246 | EV_BEGIN_WATCHER (child, child) |
233 | void set (int pid) |
247 | void set (int pid) |
234 | { |
248 | { |
235 | int active = is_active (); |
249 | int active = is_active (); |
236 | if (active) stop (); |
250 | if (active) stop (); |
237 | ev_child_set (static_cast<ev_child *>(this), pid); |
251 | ev_child_set (static_cast<ev_child *>(this), pid); |
… | |
… | |
241 | void start (int pid) |
255 | void start (int pid) |
242 | { |
256 | { |
243 | set (pid); |
257 | set (pid); |
244 | start (); |
258 | start (); |
245 | } |
259 | } |
246 | }; |
260 | EV_END_WATCHER (child, child) |
247 | |
261 | |
248 | #undef EV_CONSTRUCT |
262 | #undef EV_CONSTRUCT |
249 | #undef EV_DECLARE_WATCHER |
263 | #undef EV_BEGIN_WATCHER |
250 | } |
264 | } |
251 | |
265 | |
252 | #endif |
266 | #endif |
253 | |
267 | |