1 | /* |
1 | /* |
2 | * libev simple C++ wrapper classes |
2 | * libev simple C++ wrapper classes |
3 | * |
3 | * |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007,2008,2010 Marc Alexander Lehmann <libev@schmorp.de> |
5 | * All rights reserved. |
5 | * All rights reserved. |
6 | * |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
7 | * Redistribution and use in source and binary forms, with or without modifica- |
8 | * tion, are permitted provided that the following conditions are met: |
8 | * tion, are permitted provided that the following conditions are met: |
9 | * |
9 | * |
… | |
… | |
44 | # include EV_H |
44 | # include EV_H |
45 | #else |
45 | #else |
46 | # include "ev.h" |
46 | # include "ev.h" |
47 | #endif |
47 | #endif |
48 | |
48 | |
49 | #ifndef EV_CXX_EXCEPTIONS |
49 | #ifndef EV_USE_STDEXCEPT |
50 | #define EV_CXX_EXCEPTIONS 1 |
50 | # define EV_USE_STDEXCEPT 1 |
51 | #endif |
51 | #endif |
52 | |
52 | |
53 | #undef EV_THROW |
53 | #if EV_USE_STDEXCEPT |
54 | #if EV_CXX_EXCEPTIONS |
|
|
55 | # include <stdexcept> |
54 | # include <stdexcept> |
56 | # define EV_THROW(exception) throw (exception) |
|
|
57 | #else |
|
|
58 | # define EV_THROW(exception) |
|
|
59 | #endif |
55 | #endif |
60 | |
56 | |
61 | namespace ev { |
57 | namespace ev { |
62 | |
58 | |
63 | typedef ev_tstamp tstamp; |
59 | typedef ev_tstamp tstamp; |
64 | |
60 | |
65 | enum { |
61 | enum |
|
|
62 | { |
66 | UNDEF = EV_UNDEF, |
63 | UNDEF = EV_UNDEF, |
67 | NONE = EV_NONE, |
64 | NONE = EV_NONE, |
68 | READ = EV_READ, |
65 | READ = EV_READ, |
69 | WRITE = EV_WRITE, |
66 | WRITE = EV_WRITE, |
|
|
67 | #if EV_COMPAT3 |
70 | TIMEOUT = EV_TIMEOUT, |
68 | TIMEOUT = EV_TIMEOUT, |
|
|
69 | #endif |
|
|
70 | TIMER = EV_TIMER, |
71 | PERIODIC = EV_PERIODIC, |
71 | PERIODIC = EV_PERIODIC, |
72 | SIGNAL = EV_SIGNAL, |
72 | SIGNAL = EV_SIGNAL, |
73 | CHILD = EV_CHILD, |
73 | CHILD = EV_CHILD, |
74 | STAT = EV_STAT, |
74 | STAT = EV_STAT, |
75 | IDLE = EV_IDLE, |
75 | IDLE = EV_IDLE, |
76 | CHECK = EV_CHECK, |
76 | CHECK = EV_CHECK, |
77 | PREPARE = EV_PREPARE, |
77 | PREPARE = EV_PREPARE, |
78 | FORK = EV_FORK, |
78 | FORK = EV_FORK, |
|
|
79 | ASYNC = EV_ASYNC, |
79 | EMBED = EV_EMBED, |
80 | EMBED = EV_EMBED, |
|
|
81 | # undef ERROR // some systems stupidly #define ERROR |
80 | ERROR = EV_ERROR, |
82 | ERROR = EV_ERROR |
81 | }; |
83 | }; |
82 | |
84 | |
83 | enum |
85 | enum |
84 | { |
86 | { |
85 | AUTO = EVFLAG_AUTO, |
87 | AUTO = EVFLAG_AUTO, |
86 | NOENV = EVFLAG_NOENV, |
88 | NOENV = EVFLAG_NOENV, |
87 | FORKCHECK = EVFLAG_FORKCHECK, |
89 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
90 | |
88 | SELECT = EVBACKEND_SELECT, |
91 | SELECT = EVBACKEND_SELECT, |
89 | POLL = EVBACKEND_POLL, |
92 | POLL = EVBACKEND_POLL, |
90 | EPOLL = EVBACKEND_EPOLL, |
93 | EPOLL = EVBACKEND_EPOLL, |
91 | KQUEUE = EVBACKEND_KQUEUE, |
94 | KQUEUE = EVBACKEND_KQUEUE, |
92 | DEVPOLL = EVBACKEND_DEVPOLL, |
95 | DEVPOLL = EVBACKEND_DEVPOLL, |
93 | PORT = EVBACKEND_PORT |
96 | PORT = EVBACKEND_PORT |
94 | }; |
97 | }; |
95 | |
98 | |
96 | enum |
99 | enum |
97 | { |
100 | { |
|
|
101 | #if EV_COMPAT3 |
98 | NONBLOCK = EVLOOP_NONBLOCK, |
102 | NONBLOCK = EVLOOP_NONBLOCK, |
99 | ONESHOT = EVLOOP_ONESHOT |
103 | ONESHOT = EVLOOP_ONESHOT, |
|
|
104 | #endif |
|
|
105 | NOWAIT = EVRUN_NOWAIT, |
|
|
106 | ONCE = EVRUN_ONCE |
100 | }; |
107 | }; |
101 | |
108 | |
102 | enum how_t |
109 | enum how_t |
103 | { |
110 | { |
104 | ONE = EVUNLOOP_ONE, |
111 | ONE = EVBREAK_ONE, |
105 | ALL = EVUNLOOP_ALL |
112 | ALL = EVBREAK_ALL |
106 | }; |
113 | }; |
107 | |
114 | |
108 | #if EV_CXX_EXCEPTIONS |
115 | struct bad_loop |
109 | struct bad_loop : std::runtime_error |
116 | #if EV_USE_STDEXCEPT |
|
|
117 | : std::runtime_error |
|
|
118 | #endif |
110 | { |
119 | { |
|
|
120 | #if EV_USE_STDEXCEPT |
111 | bad_loop () |
121 | bad_loop () |
112 | : std::runtime_error ("loop can't be initialized") |
122 | : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") |
113 | { |
|
|
114 | } |
123 | { |
115 | }; |
124 | } |
116 | #endif |
125 | #endif |
|
|
126 | }; |
117 | |
127 | |
118 | #ifdef EV_AX |
128 | #ifdef EV_AX |
119 | # undef EV_AX |
129 | # undef EV_AX |
120 | #endif |
130 | #endif |
121 | |
131 | |
… | |
… | |
131 | # define EV_AX_ |
141 | # define EV_AX_ |
132 | #endif |
142 | #endif |
133 | |
143 | |
134 | struct loop_ref |
144 | struct loop_ref |
135 | { |
145 | { |
136 | |
|
|
137 | loop_ref (EV_P) |
146 | loop_ref (EV_P) throw () |
138 | #if EV_MULTIPLICITY |
147 | #if EV_MULTIPLICITY |
139 | EV_THROW (bad_loop) : EV_AX (EV_A) |
148 | : EV_AX (EV_A) |
140 | #endif |
149 | #endif |
141 | { |
|
|
142 | #if EV_MULTIPLICIY && EV_CXX_EXCEPTIONS |
|
|
143 | if (!EV_A) |
|
|
144 | throw bad_loop (); |
|
|
145 | #endif |
|
|
146 | } |
150 | { |
|
|
151 | } |
147 | |
152 | |
148 | bool operator== (const loop_ref &other) const throw () |
153 | bool operator == (const loop_ref &other) const throw () |
149 | { |
154 | { |
150 | #if EV_MULTIPLICITY |
155 | #if EV_MULTIPLICITY |
151 | return this->EV_AX == other.EV_AX; |
156 | return EV_AX == other.EV_AX; |
152 | #else |
157 | #else |
153 | return true; |
158 | return true; |
154 | #endif |
159 | #endif |
155 | } |
160 | } |
156 | |
161 | |
157 | bool operator!= (const loop_ref &other) const throw () |
162 | bool operator != (const loop_ref &other) const throw () |
158 | { |
163 | { |
159 | #if EV_MULTIPLICITY |
164 | #if EV_MULTIPLICITY |
160 | return ! (*this == other); |
165 | return ! (*this == other); |
161 | #else |
166 | #else |
162 | return false; |
167 | return false; |
163 | #endif |
168 | #endif |
164 | } |
169 | } |
165 | |
170 | |
166 | #if EV_MULTIPLICITY |
171 | #if EV_MULTIPLICITY |
167 | bool operator== (struct ev_loop *other) const throw () |
172 | bool operator == (const EV_P) const throw () |
168 | { |
173 | { |
169 | return this->EV_AX == other; |
174 | return this->EV_AX == EV_A; |
170 | } |
|
|
171 | |
|
|
172 | bool operator!= (struct ev_loop *other) const throw () |
|
|
173 | { |
175 | } |
174 | return ! (*this == other); |
176 | |
|
|
177 | bool operator != (const EV_P) const throw () |
175 | } |
178 | { |
176 | |
|
|
177 | bool operator== (const struct ev_loop *other) const throw () |
|
|
178 | { |
|
|
179 | return this->EV_AX == other; |
|
|
180 | } |
|
|
181 | |
|
|
182 | bool operator!= (const struct ev_loop *other) const throw () |
|
|
183 | { |
|
|
184 | return (*this == other); |
179 | return (*this == EV_A); |
185 | } |
180 | } |
186 | |
181 | |
187 | operator struct ev_loop * () const throw () |
182 | operator struct ev_loop * () const throw () |
188 | { |
183 | { |
189 | return EV_AX; |
184 | return EV_AX; |
… | |
… | |
198 | { |
193 | { |
199 | return EV_AX == ev_default_loop (0); |
194 | return EV_AX == ev_default_loop (0); |
200 | } |
195 | } |
201 | #endif |
196 | #endif |
202 | |
197 | |
|
|
198 | #if EV_COMPAT3 |
203 | void loop (int flags = 0) |
199 | void loop (int flags = 0) |
204 | { |
200 | { |
205 | ev_loop (EV_AX_ flags); |
201 | ev_run (EV_AX_ flags); |
206 | } |
202 | } |
207 | |
203 | |
208 | void unloop (how_t how = ONE) throw () |
204 | void unloop (how_t how = ONE) throw () |
209 | { |
205 | { |
210 | ev_unloop (EV_AX_ how); |
206 | ev_break (EV_AX_ how); |
|
|
207 | } |
|
|
208 | #endif |
|
|
209 | |
|
|
210 | void run (int flags = 0) |
|
|
211 | { |
|
|
212 | ev_run (EV_AX_ flags); |
|
|
213 | } |
|
|
214 | |
|
|
215 | void break_loop (how_t how = ONE) throw () |
|
|
216 | { |
|
|
217 | ev_break (EV_AX_ how); |
211 | } |
218 | } |
212 | |
219 | |
213 | void post_fork () throw () |
220 | void post_fork () throw () |
214 | { |
221 | { |
215 | #if EV_MULTIPLICITY |
222 | #if EV_MULTIPLICITY |
… | |
… | |
217 | #else |
224 | #else |
218 | ev_default_fork (); |
225 | ev_default_fork (); |
219 | #endif |
226 | #endif |
220 | } |
227 | } |
221 | |
228 | |
222 | unsigned int count () const throw () |
|
|
223 | { |
|
|
224 | return ev_loop_count (EV_AX); |
|
|
225 | } |
|
|
226 | |
|
|
227 | unsigned int backend () const throw () |
229 | unsigned int backend () const throw () |
228 | { |
230 | { |
229 | return ev_backend (EV_AX); |
231 | return ev_backend (EV_AX); |
230 | } |
232 | } |
231 | |
233 | |
… | |
… | |
242 | void unref () throw () |
244 | void unref () throw () |
243 | { |
245 | { |
244 | ev_unref (EV_AX); |
246 | ev_unref (EV_AX); |
245 | } |
247 | } |
246 | |
248 | |
|
|
249 | #if EV_FEATURE_API |
|
|
250 | unsigned int iteration () const throw () |
|
|
251 | { |
|
|
252 | return ev_iteration (EV_AX); |
|
|
253 | } |
|
|
254 | |
|
|
255 | unsigned int depth () const throw () |
|
|
256 | { |
|
|
257 | return ev_depth (EV_AX); |
|
|
258 | } |
|
|
259 | |
247 | void set_io_collect_interval (tstamp interval) throw () |
260 | void set_io_collect_interval (tstamp interval) throw () |
248 | { |
261 | { |
249 | ev_set_io_collect_interval (EV_AX_ interval); |
262 | ev_set_io_collect_interval (EV_AX_ interval); |
250 | } |
263 | } |
251 | |
264 | |
252 | void set_timeout_collect_interval (tstamp interval) throw () |
265 | void set_timeout_collect_interval (tstamp interval) throw () |
253 | { |
266 | { |
254 | ev_set_timeout_collect_interval (EV_AX_ interval); |
267 | ev_set_timeout_collect_interval (EV_AX_ interval); |
255 | } |
268 | } |
|
|
269 | #endif |
256 | |
270 | |
257 | // function callback |
271 | // function callback |
258 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw () |
272 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw () |
259 | { |
273 | { |
260 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
274 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
261 | } |
275 | } |
262 | |
276 | |
263 | // method callback |
277 | // method callback |
… | |
… | |
265 | void once (int fd, int events, tstamp timeout, K *object) throw () |
279 | void once (int fd, int events, tstamp timeout, K *object) throw () |
266 | { |
280 | { |
267 | once (fd, events, timeout, method_thunk<K, method>, object); |
281 | once (fd, events, timeout, method_thunk<K, method>, object); |
268 | } |
282 | } |
269 | |
283 | |
|
|
284 | // default method == operator () |
|
|
285 | template<class K> |
|
|
286 | void once (int fd, int events, tstamp timeout, K *object) throw () |
|
|
287 | { |
|
|
288 | once (fd, events, timeout, method_thunk<K, &K::operator ()>, object); |
|
|
289 | } |
|
|
290 | |
270 | template<class K, void (K::*method)(int)> |
291 | template<class K, void (K::*method)(int)> |
271 | static void method_thunk (int revents, void* arg) |
292 | static void method_thunk (int revents, void *arg) |
272 | { |
|
|
273 | K *obj = static_cast<K *>(arg); |
|
|
274 | (obj->*method) (revents); |
|
|
275 | } |
293 | { |
|
|
294 | static_cast<K *>(arg)->*method |
|
|
295 | (revents); |
|
|
296 | } |
276 | |
297 | |
277 | // const method callback |
298 | // no-argument method callback |
278 | template<class K, void (K::*method)(int) const> |
|
|
279 | void once (int fd, int events, tstamp timeout, const K *object) throw () |
|
|
280 | { |
|
|
281 | once (fd, events, timeout, const_method_thunk<K, method>, object); |
|
|
282 | } |
|
|
283 | |
|
|
284 | template<class K, void (K::*method)(int) const> |
|
|
285 | static void const_method_thunk (int revents, void* arg) |
|
|
286 | { |
|
|
287 | K *obj = static_cast<K *>(arg); |
|
|
288 | (obj->*method) (revents); |
|
|
289 | } |
|
|
290 | |
|
|
291 | // simple method callback |
|
|
292 | template<class K, void (K::*method)()> |
299 | template<class K, void (K::*method)()> |
293 | void once (int fd, int events, tstamp timeout, K *object) throw () |
300 | void once (int fd, int events, tstamp timeout, K *object) throw () |
294 | { |
301 | { |
295 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
302 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
296 | } |
303 | } |
297 | |
304 | |
298 | template<class K, void (K::*method)()> |
305 | template<class K, void (K::*method)()> |
299 | static void method_noargs_thunk (int revents, void* arg) |
306 | static void method_noargs_thunk (int revents, void *arg) |
300 | { |
307 | { |
301 | K *obj = static_cast<K *>(arg); |
308 | static_cast<K *>(arg)->*method |
302 | (obj->*method) (); |
309 | (); |
303 | } |
310 | } |
304 | |
311 | |
305 | // simpler function callback |
312 | // simpler function callback |
306 | template<void (*cb)(int)> |
313 | template<void (*cb)(int)> |
307 | void once (int fd, int events, tstamp timeout) throw () |
314 | void once (int fd, int events, tstamp timeout) throw () |
308 | { |
315 | { |
309 | once (fd, events, timeout, simpler_func_thunk<cb>); |
316 | once (fd, events, timeout, simpler_func_thunk<cb>); |
310 | } |
317 | } |
311 | |
318 | |
312 | template<void (*cb)(int)> |
319 | template<void (*cb)(int)> |
313 | static void simpler_func_thunk (int revents, void* arg) |
320 | static void simpler_func_thunk (int revents, void *arg) |
314 | { |
321 | { |
|
|
322 | (*cb) |
315 | (*cb) (revents); |
323 | (revents); |
316 | } |
324 | } |
317 | |
325 | |
318 | // simplest function callback |
326 | // simplest function callback |
319 | template<void (*cb)()> |
327 | template<void (*cb)()> |
320 | void once (int fd, int events, tstamp timeout) throw () |
328 | void once (int fd, int events, tstamp timeout) throw () |
321 | { |
329 | { |
322 | once (fd, events, timeout, simplest_func_thunk<cb>); |
330 | once (fd, events, timeout, simplest_func_thunk<cb>); |
323 | } |
331 | } |
324 | |
332 | |
325 | template<void (*cb)()> |
333 | template<void (*cb)()> |
326 | static void simplest_func_thunk (int revents, void* arg) |
334 | static void simplest_func_thunk (int revents, void *arg) |
327 | { |
335 | { |
328 | (*cb) (); |
336 | (*cb) |
|
|
337 | (); |
329 | } |
338 | } |
330 | |
339 | |
331 | void feed_fd_event (int fd, int revents) throw () |
340 | void feed_fd_event (int fd, int revents) throw () |
332 | { |
341 | { |
333 | ev_feed_fd_event (EV_AX_ fd, revents); |
342 | ev_feed_fd_event (EV_AX_ fd, revents); |
… | |
… | |
343 | #endif |
352 | #endif |
344 | |
353 | |
345 | }; |
354 | }; |
346 | |
355 | |
347 | #if EV_MULTIPLICITY |
356 | #if EV_MULTIPLICITY |
348 | struct dynamic_loop: loop_ref |
357 | struct dynamic_loop : loop_ref |
349 | { |
358 | { |
350 | |
359 | |
351 | dynamic_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
360 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
352 | : loop_ref (ev_loop_new (flags)) |
361 | : loop_ref (ev_loop_new (flags)) |
353 | { |
362 | { |
|
|
363 | if (!EV_AX) |
|
|
364 | throw bad_loop (); |
354 | } |
365 | } |
355 | |
366 | |
356 | ~dynamic_loop () throw () |
367 | ~dynamic_loop () throw () |
357 | { |
368 | { |
358 | ev_loop_destroy (EV_AX); |
369 | ev_loop_destroy (EV_AX); |
… | |
… | |
366 | dynamic_loop & operator= (const dynamic_loop &); |
377 | dynamic_loop & operator= (const dynamic_loop &); |
367 | |
378 | |
368 | }; |
379 | }; |
369 | #endif |
380 | #endif |
370 | |
381 | |
371 | struct default_loop: loop_ref |
382 | struct default_loop : loop_ref |
372 | { |
383 | { |
373 | |
|
|
374 | default_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
384 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
375 | #if EV_MULTIPLICITY |
385 | #if EV_MULTIPLICITY |
376 | : loop_ref (ev_default_loop (flags)) |
386 | : loop_ref (ev_default_loop (flags)) |
377 | { |
387 | #endif |
378 | } |
388 | { |
|
|
389 | if ( |
|
|
390 | #if EV_MULTIPLICITY |
|
|
391 | !EV_AX |
379 | #else |
392 | #else |
380 | { |
|
|
381 | #if EV_CXX_EXCEPTIONS |
|
|
382 | int r = |
|
|
383 | #endif |
|
|
384 | ev_default_loop (flags); |
393 | !ev_default_loop (flags) |
385 | #if EV_CXX_EXCEPTIONS |
394 | #endif |
386 | if (!r) |
395 | ) |
387 | throw bad_loop (); |
396 | throw bad_loop (); |
388 | #endif |
|
|
389 | } |
397 | } |
390 | #endif |
|
|
391 | |
398 | |
392 | ~default_loop () throw () |
399 | ~default_loop () throw () |
393 | { |
400 | { |
394 | ev_default_destroy (); |
401 | ev_default_destroy (); |
395 | #if EV_MULTIPLICITY |
|
|
396 | EV_AX = 0; |
|
|
397 | #endif |
|
|
398 | } |
402 | } |
399 | |
403 | |
400 | private: |
404 | private: |
401 | |
|
|
402 | default_loop (const default_loop &); |
405 | default_loop (const default_loop &); |
403 | |
|
|
404 | default_loop & operator= (const default_loop &); |
406 | default_loop &operator = (const default_loop &); |
405 | |
|
|
406 | }; |
407 | }; |
407 | |
408 | |
408 | inline loop_ref get_default_loop () throw () |
409 | inline loop_ref get_default_loop () throw () |
409 | { |
410 | { |
410 | #if EV_MULTIPLICITY |
411 | #if EV_MULTIPLICITY |
… | |
… | |
431 | struct base : ev_watcher |
432 | struct base : ev_watcher |
432 | { |
433 | { |
433 | #if EV_MULTIPLICITY |
434 | #if EV_MULTIPLICITY |
434 | EV_PX; |
435 | EV_PX; |
435 | |
436 | |
|
|
437 | // loop set |
436 | void set (EV_PX) throw () |
438 | void set (EV_P) throw () |
437 | { |
439 | { |
438 | this->EV_A = EV_A; |
440 | this->EV_A = EV_A; |
439 | } |
441 | } |
440 | #endif |
442 | #endif |
441 | |
443 | |
… | |
… | |
445 | #endif |
447 | #endif |
446 | { |
448 | { |
447 | ev_init (this, 0); |
449 | ev_init (this, 0); |
448 | } |
450 | } |
449 | |
451 | |
450 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
452 | void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
451 | { |
453 | { |
452 | this->data = data; |
454 | this->data = (void *)data; |
453 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
455 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
456 | } |
|
|
457 | |
|
|
458 | // function callback |
|
|
459 | template<void (*function)(watcher &w, int)> |
|
|
460 | void set (void *data = 0) throw () |
|
|
461 | { |
|
|
462 | set_ (data, function_thunk<function>); |
|
|
463 | } |
|
|
464 | |
|
|
465 | template<void (*function)(watcher &w, int)> |
|
|
466 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
467 | { |
|
|
468 | function |
|
|
469 | (*static_cast<watcher *>(w), revents); |
454 | } |
470 | } |
455 | |
471 | |
456 | // method callback |
472 | // method callback |
457 | template<class K, void (K::*method)(watcher &w, int)> |
473 | template<class K, void (K::*method)(watcher &w, int)> |
458 | void set (K *object) throw () |
474 | void set (K *object) throw () |
459 | { |
475 | { |
460 | set_ (object, method_thunk<K, method>); |
476 | set_ (object, method_thunk<K, method>); |
461 | } |
477 | } |
462 | |
478 | |
|
|
479 | // default method == operator () |
|
|
480 | template<class K> |
|
|
481 | void set (K *object) throw () |
|
|
482 | { |
|
|
483 | set_ (object, method_thunk<K, &K::operator ()>); |
|
|
484 | } |
|
|
485 | |
463 | template<class K, void (K::*method)(watcher &w, int)> |
486 | template<class K, void (K::*method)(watcher &w, int)> |
464 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
487 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
465 | { |
488 | { |
466 | K *obj = static_cast<K *>(w->data); |
489 | (static_cast<K *>(w->data)->*method) |
467 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
468 | } |
|
|
469 | |
|
|
470 | // const method callback |
|
|
471 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
472 | void set (const K *object) throw () |
|
|
473 | { |
|
|
474 | set_ (object, const_method_thunk<K, method>); |
|
|
475 | } |
|
|
476 | |
|
|
477 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
478 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
479 | { |
|
|
480 | K *obj = static_cast<K *>(w->data); |
|
|
481 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
482 | } |
|
|
483 | |
|
|
484 | // function callback |
|
|
485 | template<void (*function)(watcher &w, int)> |
|
|
486 | void set (void *data = 0) throw () |
|
|
487 | { |
|
|
488 | set_ (data, function_thunk<function>); |
|
|
489 | } |
|
|
490 | |
|
|
491 | template<void (*function)(watcher &w, int)> |
|
|
492 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
493 | { |
|
|
494 | function (*static_cast<watcher *>(w), revents); |
490 | (*static_cast<watcher *>(w), revents); |
495 | } |
491 | } |
496 | |
492 | |
497 | // simple callback |
493 | // no-argument callback |
498 | template<class K, void (K::*method)()> |
494 | template<class K, void (K::*method)()> |
499 | void set (K *object) throw () |
495 | void set (K *object) throw () |
500 | { |
496 | { |
501 | set_ (object, method_noargs_thunk<K, method>); |
497 | set_ (object, method_noargs_thunk<K, method>); |
502 | } |
498 | } |
503 | |
499 | |
504 | template<class K, void (K::*method)()> |
500 | template<class K, void (K::*method)()> |
505 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
501 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
506 | { |
502 | { |
507 | K *obj = static_cast<K *>(w->data); |
503 | (static_cast<K *>(w->data)->*method) |
508 | (obj->*method) (); |
504 | (); |
509 | } |
505 | } |
510 | |
506 | |
511 | void operator ()(int events = EV_UNDEF) |
507 | void operator ()(int events = EV_UNDEF) |
512 | { |
508 | { |
|
|
509 | return |
513 | return ev_cb (static_cast<ev_watcher *>(this)) |
510 | ev_cb (static_cast<ev_watcher *>(this)) |
514 | (static_cast<ev_watcher *>(this), events); |
511 | (static_cast<ev_watcher *>(this), events); |
515 | } |
512 | } |
516 | |
513 | |
517 | bool is_active () const throw () |
514 | bool is_active () const throw () |
518 | { |
515 | { |
519 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
516 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
… | |
… | |
575 | ev_set_syserr_cb (cb); |
572 | ev_set_syserr_cb (cb); |
576 | } |
573 | } |
577 | |
574 | |
578 | #if EV_MULTIPLICITY |
575 | #if EV_MULTIPLICITY |
579 | #define EV_CONSTRUCT(cppstem,cstem) \ |
576 | #define EV_CONSTRUCT(cppstem,cstem) \ |
580 | (EV_PX = get_default_loop ()) throw () \ |
577 | (EV_PX = get_default_loop ()) throw () \ |
581 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
578 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
582 | { \ |
579 | { \ |
583 | } |
580 | } |
584 | #else |
581 | #else |
585 | #define EV_CONSTRUCT(cppstem,cstem) \ |
582 | #define EV_CONSTRUCT(cppstem,cstem) \ |
586 | () throw () \ |
583 | () throw () \ |
587 | { \ |
584 | { \ |
588 | } |
585 | } |
589 | #endif |
586 | #endif |
590 | |
587 | |
591 | /* using a template here would require quite a bit more lines, |
588 | /* using a template here would require quite a bit more lines, |
592 | * so a macro solution was chosen */ |
589 | * so a macro solution was chosen */ |
593 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
590 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
594 | \ |
591 | \ |
595 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
592 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
596 | { \ |
593 | { \ |
597 | void start () throw () \ |
594 | void start () throw () \ |
598 | { \ |
595 | { \ |
599 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
596 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
600 | } \ |
597 | } \ |
601 | \ |
598 | \ |
602 | void stop () throw () \ |
599 | void stop () throw () \ |
603 | { \ |
600 | { \ |
604 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
601 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
605 | } \ |
602 | } \ |
606 | \ |
603 | \ |
607 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
604 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
608 | \ |
605 | \ |
609 | ~cppstem () throw () \ |
606 | ~cppstem () throw () \ |
610 | { \ |
607 | { \ |
611 | stop (); \ |
608 | stop (); \ |
612 | } \ |
609 | } \ |
613 | \ |
610 | \ |
614 | using base<ev_ ## cstem, cppstem>::set; \ |
611 | using base<ev_ ## cstem, cppstem>::set; \ |
615 | \ |
612 | \ |
616 | private: \ |
613 | private: \ |
617 | \ |
614 | \ |
618 | cppstem (const cppstem &o); \ |
615 | cppstem (const cppstem &o); \ |
619 | \ |
616 | \ |
620 | cppstem & operator =(const cppstem &o); \ |
617 | cppstem &operator =(const cppstem &o); \ |
621 | \ |
618 | \ |
622 | public: |
619 | public: |
623 | |
620 | |
624 | #define EV_END_WATCHER(cppstem,cstem) \ |
621 | #define EV_END_WATCHER(cppstem,cstem) \ |
625 | }; |
622 | }; |
… | |
… | |
665 | |
662 | |
666 | void again () throw () |
663 | void again () throw () |
667 | { |
664 | { |
668 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
665 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
669 | } |
666 | } |
|
|
667 | |
|
|
668 | ev_tstamp remaining () |
|
|
669 | { |
|
|
670 | return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this)); |
|
|
671 | } |
670 | EV_END_WATCHER (timer, timer) |
672 | EV_END_WATCHER (timer, timer) |
671 | |
673 | |
672 | #if EV_PERIODIC_ENABLE |
674 | #if EV_PERIODIC_ENABLE |
673 | EV_BEGIN_WATCHER (periodic, periodic) |
675 | EV_BEGIN_WATCHER (periodic, periodic) |
674 | void set (ev_tstamp at, ev_tstamp interval = 0.) throw () |
676 | void set (ev_tstamp at, ev_tstamp interval = 0.) throw () |
… | |
… | |
690 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
692 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
691 | } |
693 | } |
692 | EV_END_WATCHER (periodic, periodic) |
694 | EV_END_WATCHER (periodic, periodic) |
693 | #endif |
695 | #endif |
694 | |
696 | |
|
|
697 | #if EV_SIGNAL_ENABLE |
695 | EV_BEGIN_WATCHER (sig, signal) |
698 | EV_BEGIN_WATCHER (sig, signal) |
696 | void set (int signum) throw () |
699 | void set (int signum) throw () |
697 | { |
700 | { |
698 | int active = is_active (); |
701 | int active = is_active (); |
699 | if (active) stop (); |
702 | if (active) stop (); |
… | |
… | |
705 | { |
708 | { |
706 | set (signum); |
709 | set (signum); |
707 | start (); |
710 | start (); |
708 | } |
711 | } |
709 | EV_END_WATCHER (sig, signal) |
712 | EV_END_WATCHER (sig, signal) |
|
|
713 | #endif |
710 | |
714 | |
|
|
715 | #if EV_CHILD_ENABLE |
711 | EV_BEGIN_WATCHER (child, child) |
716 | EV_BEGIN_WATCHER (child, child) |
712 | void set (int pid) throw () |
717 | void set (int pid, int trace = 0) throw () |
713 | { |
718 | { |
714 | int active = is_active (); |
719 | int active = is_active (); |
715 | if (active) stop (); |
720 | if (active) stop (); |
716 | ev_child_set (static_cast<ev_child *>(this), pid); |
721 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
717 | if (active) start (); |
722 | if (active) start (); |
718 | } |
723 | } |
719 | |
724 | |
720 | void start (int pid) throw () |
725 | void start (int pid, int trace = 0) throw () |
721 | { |
726 | { |
722 | set (pid); |
727 | set (pid, trace); |
723 | start (); |
728 | start (); |
724 | } |
729 | } |
725 | EV_END_WATCHER (child, child) |
730 | EV_END_WATCHER (child, child) |
|
|
731 | #endif |
726 | |
732 | |
727 | #if EV_STAT_ENABLE |
733 | #if EV_STAT_ENABLE |
728 | EV_BEGIN_WATCHER (stat, stat) |
734 | EV_BEGIN_WATCHER (stat, stat) |
729 | void set (const char *path, ev_tstamp interval = 0.) throw () |
735 | void set (const char *path, ev_tstamp interval = 0.) throw () |
730 | { |
736 | { |
… | |
… | |
746 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
752 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
747 | } |
753 | } |
748 | EV_END_WATCHER (stat, stat) |
754 | EV_END_WATCHER (stat, stat) |
749 | #endif |
755 | #endif |
750 | |
756 | |
|
|
757 | #if EV_IDLE_ENABLE |
751 | EV_BEGIN_WATCHER (idle, idle) |
758 | EV_BEGIN_WATCHER (idle, idle) |
752 | void set () throw () { } |
759 | void set () throw () { } |
753 | EV_END_WATCHER (idle, idle) |
760 | EV_END_WATCHER (idle, idle) |
|
|
761 | #endif |
754 | |
762 | |
|
|
763 | #if EV_PREPARE_ENABLE |
755 | EV_BEGIN_WATCHER (prepare, prepare) |
764 | EV_BEGIN_WATCHER (prepare, prepare) |
756 | void set () throw () { } |
765 | void set () throw () { } |
757 | EV_END_WATCHER (prepare, prepare) |
766 | EV_END_WATCHER (prepare, prepare) |
|
|
767 | #endif |
758 | |
768 | |
|
|
769 | #if EV_CHECK_ENABLE |
759 | EV_BEGIN_WATCHER (check, check) |
770 | EV_BEGIN_WATCHER (check, check) |
760 | void set () throw () { } |
771 | void set () throw () { } |
761 | EV_END_WATCHER (check, check) |
772 | EV_END_WATCHER (check, check) |
|
|
773 | #endif |
762 | |
774 | |
763 | #if EV_EMBED_ENABLE |
775 | #if EV_EMBED_ENABLE |
764 | EV_BEGIN_WATCHER (embed, embed) |
776 | EV_BEGIN_WATCHER (embed, embed) |
|
|
777 | void set (struct ev_loop *embedded_loop) throw () |
|
|
778 | { |
|
|
779 | int active = is_active (); |
|
|
780 | if (active) stop (); |
|
|
781 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
782 | if (active) start (); |
|
|
783 | } |
|
|
784 | |
765 | void start (struct ev_loop *embedded_loop) throw () |
785 | void start (struct ev_loop *embedded_loop) throw () |
766 | { |
786 | { |
767 | stop (); |
787 | set (embedded_loop); |
768 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
769 | start (); |
788 | start (); |
770 | } |
789 | } |
771 | |
790 | |
772 | void sweep () |
791 | void sweep () |
773 | { |
792 | { |
… | |
… | |
778 | |
797 | |
779 | #if EV_FORK_ENABLE |
798 | #if EV_FORK_ENABLE |
780 | EV_BEGIN_WATCHER (fork, fork) |
799 | EV_BEGIN_WATCHER (fork, fork) |
781 | void set () throw () { } |
800 | void set () throw () { } |
782 | EV_END_WATCHER (fork, fork) |
801 | EV_END_WATCHER (fork, fork) |
|
|
802 | #endif |
|
|
803 | |
|
|
804 | #if EV_ASYNC_ENABLE |
|
|
805 | EV_BEGIN_WATCHER (async, async) |
|
|
806 | void send () throw () |
|
|
807 | { |
|
|
808 | ev_async_send (EV_A_ static_cast<ev_async *>(this)); |
|
|
809 | } |
|
|
810 | |
|
|
811 | bool async_pending () throw () |
|
|
812 | { |
|
|
813 | return ev_async_pending (static_cast<ev_async *>(this)); |
|
|
814 | } |
|
|
815 | EV_END_WATCHER (async, async) |
783 | #endif |
816 | #endif |
784 | |
817 | |
785 | #undef EV_PX |
818 | #undef EV_PX |
786 | #undef EV_PX_ |
819 | #undef EV_PX_ |
787 | #undef EV_CONSTRUCT |
820 | #undef EV_CONSTRUCT |
788 | #undef EV_BEGIN_WATCHER |
821 | #undef EV_BEGIN_WATCHER |
789 | #undef EV_END_WATCHER |
822 | #undef EV_END_WATCHER |
790 | |
|
|
791 | } |
823 | } |
792 | |
824 | |
793 | #undef EV_THROW |
|
|
794 | |
|
|
795 | #endif |
825 | #endif |
796 | |
826 | |