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 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, |
70 | TIMEOUT = EV_TIMEOUT, |
67 | TIMEOUT = EV_TIMEOUT, |
… | |
… | |
74 | STAT = EV_STAT, |
71 | STAT = EV_STAT, |
75 | IDLE = EV_IDLE, |
72 | IDLE = EV_IDLE, |
76 | CHECK = EV_CHECK, |
73 | CHECK = EV_CHECK, |
77 | PREPARE = EV_PREPARE, |
74 | PREPARE = EV_PREPARE, |
78 | FORK = EV_FORK, |
75 | FORK = EV_FORK, |
|
|
76 | ASYNC = EV_ASYNC, |
79 | EMBED = EV_EMBED, |
77 | EMBED = EV_EMBED, |
|
|
78 | # undef ERROR // some systems stupidly #define ERROR |
80 | ERROR = EV_ERROR, |
79 | ERROR = EV_ERROR, |
81 | }; |
80 | }; |
82 | |
81 | |
83 | enum |
82 | enum |
84 | { |
83 | { |
85 | AUTO = EVFLAG_AUTO, |
84 | AUTO = EVFLAG_AUTO, |
86 | NOENV = EVFLAG_NOENV, |
85 | NOENV = EVFLAG_NOENV, |
87 | FORKCHECK = EVFLAG_FORKCHECK, |
86 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
87 | |
88 | SELECT = EVBACKEND_SELECT, |
88 | SELECT = EVBACKEND_SELECT, |
89 | POLL = EVBACKEND_POLL, |
89 | POLL = EVBACKEND_POLL, |
90 | EPOLL = EVBACKEND_EPOLL, |
90 | EPOLL = EVBACKEND_EPOLL, |
91 | KQUEUE = EVBACKEND_KQUEUE, |
91 | KQUEUE = EVBACKEND_KQUEUE, |
92 | DEVPOLL = EVBACKEND_DEVPOLL, |
92 | DEVPOLL = EVBACKEND_DEVPOLL, |
93 | PORT = EVBACKEND_PORT |
93 | PORT = EVBACKEND_PORT |
94 | }; |
94 | }; |
95 | |
95 | |
96 | enum |
96 | enum |
97 | { |
97 | { |
98 | NONBLOCK = EVLOOP_NONBLOCK, |
98 | NONBLOCK = EVLOOP_NONBLOCK, |
99 | ONESHOT = EVLOOP_ONESHOT |
99 | ONESHOT = EVLOOP_ONESHOT |
100 | }; |
100 | }; |
101 | |
101 | |
102 | enum how_t |
102 | enum how_t |
103 | { |
103 | { |
104 | ONE = EVUNLOOP_ONE, |
104 | ONE = EVUNLOOP_ONE, |
105 | ALL = EVUNLOOP_ALL |
105 | ALL = EVUNLOOP_ALL |
106 | }; |
106 | }; |
107 | |
107 | |
108 | #if EV_CXX_EXCEPTIONS |
108 | struct bad_loop |
109 | struct bad_loop : std::runtime_error |
109 | #if EV_USE_STDEXCEPT |
|
|
110 | : std::runtime_error |
|
|
111 | #endif |
110 | { |
112 | { |
|
|
113 | #if EV_USE_STDEXCEPT |
111 | bad_loop () |
114 | bad_loop () |
112 | : std::runtime_error ("loop can't be initialized") |
115 | : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") |
113 | { |
|
|
114 | } |
116 | { |
115 | }; |
117 | } |
116 | #endif |
118 | #endif |
|
|
119 | }; |
117 | |
120 | |
118 | #ifdef EV_AX |
121 | #ifdef EV_AX |
119 | # undef EV_AX |
122 | # undef EV_AX |
120 | #endif |
123 | #endif |
121 | |
124 | |
… | |
… | |
131 | # define EV_AX_ |
134 | # define EV_AX_ |
132 | #endif |
135 | #endif |
133 | |
136 | |
134 | struct loop_ref |
137 | struct loop_ref |
135 | { |
138 | { |
136 | |
|
|
137 | loop_ref (EV_P) |
139 | loop_ref (EV_P) throw () |
138 | #if EV_MULTIPLICITY |
140 | #if EV_MULTIPLICITY |
139 | EV_THROW (bad_loop) : EV_AX (EV_A) |
141 | : EV_AX (EV_A) |
140 | #endif |
142 | #endif |
141 | { |
|
|
142 | #if EV_MULTIPLICIY && EV_CXX_EXCEPTIONS |
|
|
143 | if (!EV_A) |
|
|
144 | throw bad_loop (); |
|
|
145 | #endif |
|
|
146 | } |
143 | { |
|
|
144 | } |
147 | |
145 | |
148 | bool operator== (const loop_ref &other) const throw () |
146 | bool operator == (const loop_ref &other) const throw () |
149 | { |
147 | { |
150 | #if EV_MULTIPLICITY |
148 | #if EV_MULTIPLICITY |
151 | return this->EV_AX == other.EV_AX; |
149 | return EV_AX == other.EV_AX; |
152 | #else |
150 | #else |
153 | return true; |
151 | return true; |
154 | #endif |
152 | #endif |
155 | } |
153 | } |
156 | |
154 | |
157 | bool operator!= (const loop_ref &other) const throw () |
155 | bool operator != (const loop_ref &other) const throw () |
158 | { |
156 | { |
159 | #if EV_MULTIPLICITY |
157 | #if EV_MULTIPLICITY |
160 | return ! (*this == other); |
158 | return ! (*this == other); |
161 | #else |
159 | #else |
162 | return false; |
160 | return false; |
163 | #endif |
161 | #endif |
164 | } |
162 | } |
165 | |
163 | |
166 | #if EV_MULTIPLICITY |
164 | #if EV_MULTIPLICITY |
167 | bool operator== (struct ev_loop *other) const throw () |
165 | bool operator == (const EV_P) const throw () |
168 | { |
166 | { |
169 | return this->EV_AX == other; |
167 | return this->EV_AX == EV_A; |
170 | } |
|
|
171 | |
|
|
172 | bool operator!= (struct ev_loop *other) const throw () |
|
|
173 | { |
168 | } |
174 | return ! (*this == other); |
169 | |
|
|
170 | bool operator != (const EV_P) const throw () |
175 | } |
171 | { |
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); |
172 | return (*this == EV_A); |
185 | } |
173 | } |
186 | |
174 | |
187 | operator struct ev_loop * () const throw () |
175 | operator struct ev_loop * () const throw () |
188 | { |
176 | { |
189 | return EV_AX; |
177 | return EV_AX; |
… | |
… | |
253 | { |
241 | { |
254 | ev_set_timeout_collect_interval (EV_AX_ interval); |
242 | ev_set_timeout_collect_interval (EV_AX_ interval); |
255 | } |
243 | } |
256 | |
244 | |
257 | // function callback |
245 | // function callback |
258 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw () |
246 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) throw () |
259 | { |
247 | { |
260 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
248 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
261 | } |
249 | } |
262 | |
250 | |
263 | // method callback |
251 | // method callback |
… | |
… | |
265 | void once (int fd, int events, tstamp timeout, K *object) throw () |
253 | void once (int fd, int events, tstamp timeout, K *object) throw () |
266 | { |
254 | { |
267 | once (fd, events, timeout, method_thunk<K, method>, object); |
255 | once (fd, events, timeout, method_thunk<K, method>, object); |
268 | } |
256 | } |
269 | |
257 | |
|
|
258 | // default method == operator () |
|
|
259 | template<class K> |
|
|
260 | void once (int fd, int events, tstamp timeout, K *object) throw () |
|
|
261 | { |
|
|
262 | once (fd, events, timeout, method_thunk<K, &K::operator ()>, object); |
|
|
263 | } |
|
|
264 | |
270 | template<class K, void (K::*method)(int)> |
265 | template<class K, void (K::*method)(int)> |
271 | static void method_thunk (int revents, void* arg) |
266 | static void method_thunk (int revents, void *arg) |
272 | { |
|
|
273 | K *obj = static_cast<K *>(arg); |
|
|
274 | (obj->*method) (revents); |
|
|
275 | } |
267 | { |
|
|
268 | static_cast<K *>(arg)->*method |
|
|
269 | (revents); |
|
|
270 | } |
276 | |
271 | |
277 | // const method callback |
272 | // 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)()> |
273 | template<class K, void (K::*method)()> |
293 | void once (int fd, int events, tstamp timeout, K *object) throw () |
274 | void once (int fd, int events, tstamp timeout, K *object) throw () |
294 | { |
275 | { |
295 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
276 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
296 | } |
277 | } |
297 | |
278 | |
298 | template<class K, void (K::*method)()> |
279 | template<class K, void (K::*method)()> |
299 | static void method_noargs_thunk (int revents, void* arg) |
280 | static void method_noargs_thunk (int revents, void *arg) |
300 | { |
281 | { |
301 | K *obj = static_cast<K *>(arg); |
282 | static_cast<K *>(arg)->*method |
302 | (obj->*method) (); |
283 | (); |
303 | } |
284 | } |
304 | |
285 | |
305 | // simpler function callback |
286 | // simpler function callback |
306 | template<void (*cb)(int)> |
287 | template<void (*cb)(int)> |
307 | void once (int fd, int events, tstamp timeout) throw () |
288 | void once (int fd, int events, tstamp timeout) throw () |
308 | { |
289 | { |
309 | once (fd, events, timeout, simpler_func_thunk<cb>); |
290 | once (fd, events, timeout, simpler_func_thunk<cb>); |
310 | } |
291 | } |
311 | |
292 | |
312 | template<void (*cb)(int)> |
293 | template<void (*cb)(int)> |
313 | static void simpler_func_thunk (int revents, void* arg) |
294 | static void simpler_func_thunk (int revents, void *arg) |
314 | { |
295 | { |
|
|
296 | (*cb) |
315 | (*cb) (revents); |
297 | (revents); |
316 | } |
298 | } |
317 | |
299 | |
318 | // simplest function callback |
300 | // simplest function callback |
319 | template<void (*cb)()> |
301 | template<void (*cb)()> |
320 | void once (int fd, int events, tstamp timeout) throw () |
302 | void once (int fd, int events, tstamp timeout) throw () |
321 | { |
303 | { |
322 | once (fd, events, timeout, simplest_func_thunk<cb>); |
304 | once (fd, events, timeout, simplest_func_thunk<cb>); |
323 | } |
305 | } |
324 | |
306 | |
325 | template<void (*cb)()> |
307 | template<void (*cb)()> |
326 | static void simplest_func_thunk (int revents, void* arg) |
308 | static void simplest_func_thunk (int revents, void *arg) |
327 | { |
309 | { |
328 | (*cb) (); |
310 | (*cb) |
|
|
311 | (); |
329 | } |
312 | } |
330 | |
313 | |
331 | void feed_fd_event (int fd, int revents) throw () |
314 | void feed_fd_event (int fd, int revents) throw () |
332 | { |
315 | { |
333 | ev_feed_fd_event (EV_AX_ fd, revents); |
316 | ev_feed_fd_event (EV_AX_ fd, revents); |
… | |
… | |
343 | #endif |
326 | #endif |
344 | |
327 | |
345 | }; |
328 | }; |
346 | |
329 | |
347 | #if EV_MULTIPLICITY |
330 | #if EV_MULTIPLICITY |
348 | struct dynamic_loop: loop_ref |
331 | struct dynamic_loop : loop_ref |
349 | { |
332 | { |
350 | |
333 | |
351 | dynamic_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
334 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
352 | : loop_ref (ev_loop_new (flags)) |
335 | : loop_ref (ev_loop_new (flags)) |
353 | { |
336 | { |
|
|
337 | if (!EV_AX) |
|
|
338 | throw bad_loop (); |
354 | } |
339 | } |
355 | |
340 | |
356 | ~dynamic_loop () throw () |
341 | ~dynamic_loop () throw () |
357 | { |
342 | { |
358 | ev_loop_destroy (EV_AX); |
343 | ev_loop_destroy (EV_AX); |
… | |
… | |
366 | dynamic_loop & operator= (const dynamic_loop &); |
351 | dynamic_loop & operator= (const dynamic_loop &); |
367 | |
352 | |
368 | }; |
353 | }; |
369 | #endif |
354 | #endif |
370 | |
355 | |
371 | struct default_loop: loop_ref |
356 | struct default_loop : loop_ref |
372 | { |
357 | { |
373 | |
|
|
374 | default_loop (unsigned int flags = AUTO) EV_THROW (bad_loop) |
358 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
375 | #if EV_MULTIPLICITY |
359 | #if EV_MULTIPLICITY |
376 | : loop_ref (ev_default_loop (flags)) |
360 | : loop_ref (ev_default_loop (flags)) |
377 | { |
361 | #endif |
378 | } |
362 | { |
|
|
363 | if ( |
|
|
364 | #if EV_MULTIPLICITY |
|
|
365 | !EV_AX |
379 | #else |
366 | #else |
380 | { |
|
|
381 | #if EV_CXX_EXCEPTIONS |
|
|
382 | int r = |
|
|
383 | #endif |
|
|
384 | ev_default_loop (flags); |
367 | !ev_default_loop (flags) |
385 | #if EV_CXX_EXCEPTIONS |
368 | #endif |
386 | if (!r) |
369 | ) |
387 | throw bad_loop (); |
370 | throw bad_loop (); |
388 | #endif |
|
|
389 | } |
371 | } |
390 | #endif |
|
|
391 | |
372 | |
392 | ~default_loop () throw () |
373 | ~default_loop () throw () |
393 | { |
374 | { |
394 | ev_default_destroy (); |
375 | ev_default_destroy (); |
395 | #if EV_MULTIPLICITY |
|
|
396 | EV_AX = 0; |
|
|
397 | #endif |
|
|
398 | } |
376 | } |
399 | |
377 | |
400 | private: |
378 | private: |
401 | |
|
|
402 | default_loop (const default_loop &); |
379 | default_loop (const default_loop &); |
403 | |
|
|
404 | default_loop & operator= (const default_loop &); |
380 | default_loop &operator = (const default_loop &); |
405 | |
|
|
406 | }; |
381 | }; |
407 | |
382 | |
408 | inline loop_ref get_default_loop () throw () |
383 | inline loop_ref get_default_loop () throw () |
409 | { |
384 | { |
410 | #if EV_MULTIPLICITY |
385 | #if EV_MULTIPLICITY |
… | |
… | |
431 | struct base : ev_watcher |
406 | struct base : ev_watcher |
432 | { |
407 | { |
433 | #if EV_MULTIPLICITY |
408 | #if EV_MULTIPLICITY |
434 | EV_PX; |
409 | EV_PX; |
435 | |
410 | |
436 | void set (EV_PX) throw () |
411 | void set (EV_P) throw () |
437 | { |
412 | { |
438 | this->EV_A = EV_A; |
413 | this->EV_A = EV_A; |
439 | } |
414 | } |
440 | #endif |
415 | #endif |
441 | |
416 | |
… | |
… | |
445 | #endif |
420 | #endif |
446 | { |
421 | { |
447 | ev_init (this, 0); |
422 | ev_init (this, 0); |
448 | } |
423 | } |
449 | |
424 | |
450 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
425 | void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
451 | { |
426 | { |
452 | this->data = data; |
427 | this->data = (void *)data; |
453 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
428 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
429 | } |
|
|
430 | |
|
|
431 | // function callback |
|
|
432 | template<void (*function)(watcher &w, int)> |
|
|
433 | void set (void *data = 0) throw () |
|
|
434 | { |
|
|
435 | set_ (data, function_thunk<function>); |
|
|
436 | } |
|
|
437 | |
|
|
438 | template<void (*function)(watcher &w, int)> |
|
|
439 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
440 | { |
|
|
441 | function |
|
|
442 | (*static_cast<watcher *>(w), revents); |
454 | } |
443 | } |
455 | |
444 | |
456 | // method callback |
445 | // method callback |
457 | template<class K, void (K::*method)(watcher &w, int)> |
446 | template<class K, void (K::*method)(watcher &w, int)> |
458 | void set (K *object) throw () |
447 | void set (K *object) throw () |
459 | { |
448 | { |
460 | set_ (object, method_thunk<K, method>); |
449 | set_ (object, method_thunk<K, method>); |
461 | } |
450 | } |
462 | |
451 | |
|
|
452 | // default method == operator () |
|
|
453 | template<class K> |
|
|
454 | void set (K *object) throw () |
|
|
455 | { |
|
|
456 | set_ (object, method_thunk<K, &K::operator ()>); |
|
|
457 | } |
|
|
458 | |
463 | template<class K, void (K::*method)(watcher &w, int)> |
459 | template<class K, void (K::*method)(watcher &w, int)> |
464 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
460 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
465 | { |
461 | { |
466 | K *obj = static_cast<K *>(w->data); |
462 | (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); |
463 | (*static_cast<watcher *>(w), revents); |
495 | } |
464 | } |
496 | |
465 | |
497 | // simple callback |
466 | // no-argument callback |
498 | template<class K, void (K::*method)()> |
467 | template<class K, void (K::*method)()> |
499 | void set (K *object) throw () |
468 | void set (K *object) throw () |
500 | { |
469 | { |
501 | set_ (object, method_noargs_thunk<K, method>); |
470 | set_ (object, method_noargs_thunk<K, method>); |
502 | } |
471 | } |
503 | |
472 | |
504 | template<class K, void (K::*method)()> |
473 | template<class K, void (K::*method)()> |
505 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
474 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
506 | { |
475 | { |
507 | K *obj = static_cast<K *>(w->data); |
476 | static_cast<K *>(w->data)->*method |
508 | (obj->*method) (); |
477 | (); |
509 | } |
478 | } |
510 | |
479 | |
511 | void operator ()(int events = EV_UNDEF) |
480 | void operator ()(int events = EV_UNDEF) |
512 | { |
481 | { |
|
|
482 | return |
513 | return ev_cb (static_cast<ev_watcher *>(this)) |
483 | ev_cb (static_cast<ev_watcher *>(this)) |
514 | (static_cast<ev_watcher *>(this), events); |
484 | (static_cast<ev_watcher *>(this), events); |
515 | } |
485 | } |
516 | |
486 | |
517 | bool is_active () const throw () |
487 | bool is_active () const throw () |
518 | { |
488 | { |
519 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
489 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
… | |
… | |
575 | ev_set_syserr_cb (cb); |
545 | ev_set_syserr_cb (cb); |
576 | } |
546 | } |
577 | |
547 | |
578 | #if EV_MULTIPLICITY |
548 | #if EV_MULTIPLICITY |
579 | #define EV_CONSTRUCT(cppstem,cstem) \ |
549 | #define EV_CONSTRUCT(cppstem,cstem) \ |
580 | (EV_PX = get_default_loop ()) throw () \ |
550 | (EV_PX = get_default_loop ()) throw () \ |
581 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
551 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
582 | { \ |
552 | { \ |
583 | } |
553 | } |
584 | #else |
554 | #else |
585 | #define EV_CONSTRUCT(cppstem,cstem) \ |
555 | #define EV_CONSTRUCT(cppstem,cstem) \ |
586 | () throw () \ |
556 | () throw () \ |
587 | { \ |
557 | { \ |
588 | } |
558 | } |
589 | #endif |
559 | #endif |
590 | |
560 | |
591 | /* using a template here would require quite a bit more lines, |
561 | /* using a template here would require quite a bit more lines, |
592 | * so a macro solution was chosen */ |
562 | * so a macro solution was chosen */ |
593 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
563 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
594 | \ |
564 | \ |
595 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
565 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
596 | { \ |
566 | { \ |
597 | void start () throw () \ |
567 | void start () throw () \ |
598 | { \ |
568 | { \ |
599 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
569 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
600 | } \ |
570 | } \ |
601 | \ |
571 | \ |
602 | void stop () throw () \ |
572 | void stop () throw () \ |
603 | { \ |
573 | { \ |
604 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
574 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
605 | } \ |
575 | } \ |
606 | \ |
576 | \ |
607 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
577 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
608 | \ |
578 | \ |
609 | ~cppstem () throw () \ |
579 | ~cppstem () throw () \ |
610 | { \ |
580 | { \ |
611 | stop (); \ |
581 | stop (); \ |
612 | } \ |
582 | } \ |
613 | \ |
583 | \ |
614 | using base<ev_ ## cstem, cppstem>::set; \ |
584 | using base<ev_ ## cstem, cppstem>::set; \ |
615 | \ |
585 | \ |
616 | private: \ |
586 | private: \ |
617 | \ |
587 | \ |
618 | cppstem (const cppstem &o); \ |
588 | cppstem (const cppstem &o); \ |
619 | \ |
589 | \ |
620 | cppstem & operator =(const cppstem &o); \ |
590 | cppstem &operator =(const cppstem &o); \ |
621 | \ |
591 | \ |
622 | public: |
592 | public: |
623 | |
593 | |
624 | #define EV_END_WATCHER(cppstem,cstem) \ |
594 | #define EV_END_WATCHER(cppstem,cstem) \ |
625 | }; |
595 | }; |
… | |
… | |
707 | start (); |
677 | start (); |
708 | } |
678 | } |
709 | EV_END_WATCHER (sig, signal) |
679 | EV_END_WATCHER (sig, signal) |
710 | |
680 | |
711 | EV_BEGIN_WATCHER (child, child) |
681 | EV_BEGIN_WATCHER (child, child) |
712 | void set (int pid) throw () |
682 | void set (int pid, int trace = 0) throw () |
713 | { |
683 | { |
714 | int active = is_active (); |
684 | int active = is_active (); |
715 | if (active) stop (); |
685 | if (active) stop (); |
716 | ev_child_set (static_cast<ev_child *>(this), pid); |
686 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
717 | if (active) start (); |
687 | if (active) start (); |
718 | } |
688 | } |
719 | |
689 | |
720 | void start (int pid) throw () |
690 | void start (int pid, int trace = 0) throw () |
721 | { |
691 | { |
722 | set (pid); |
692 | set (pid, trace); |
723 | start (); |
693 | start (); |
724 | } |
694 | } |
725 | EV_END_WATCHER (child, child) |
695 | EV_END_WATCHER (child, child) |
726 | |
696 | |
727 | #if EV_STAT_ENABLE |
697 | #if EV_STAT_ENABLE |
… | |
… | |
760 | void set () throw () { } |
730 | void set () throw () { } |
761 | EV_END_WATCHER (check, check) |
731 | EV_END_WATCHER (check, check) |
762 | |
732 | |
763 | #if EV_EMBED_ENABLE |
733 | #if EV_EMBED_ENABLE |
764 | EV_BEGIN_WATCHER (embed, embed) |
734 | EV_BEGIN_WATCHER (embed, embed) |
|
|
735 | void set (struct ev_loop *embedded_loop) throw () |
|
|
736 | { |
|
|
737 | int active = is_active (); |
|
|
738 | if (active) stop (); |
|
|
739 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
740 | if (active) start (); |
|
|
741 | } |
|
|
742 | |
765 | void start (struct ev_loop *embedded_loop) throw () |
743 | void start (struct ev_loop *embedded_loop) throw () |
766 | { |
744 | { |
767 | stop (); |
745 | set (embedded_loop); |
768 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
769 | start (); |
746 | start (); |
770 | } |
747 | } |
771 | |
748 | |
772 | void sweep () |
749 | void sweep () |
773 | { |
750 | { |
… | |
… | |
780 | EV_BEGIN_WATCHER (fork, fork) |
757 | EV_BEGIN_WATCHER (fork, fork) |
781 | void set () throw () { } |
758 | void set () throw () { } |
782 | EV_END_WATCHER (fork, fork) |
759 | EV_END_WATCHER (fork, fork) |
783 | #endif |
760 | #endif |
784 | |
761 | |
|
|
762 | #if EV_ASYNC_ENABLE |
|
|
763 | EV_BEGIN_WATCHER (async, async) |
|
|
764 | void set () throw () { } |
|
|
765 | |
|
|
766 | void send () throw () |
|
|
767 | { |
|
|
768 | ev_async_send (EV_A_ static_cast<ev_async *>(this)); |
|
|
769 | } |
|
|
770 | |
|
|
771 | bool async_pending () throw () |
|
|
772 | { |
|
|
773 | return ev_async_pending (static_cast<ev_async *>(this)); |
|
|
774 | } |
|
|
775 | EV_END_WATCHER (async, async) |
|
|
776 | #endif |
|
|
777 | |
785 | #undef EV_PX |
778 | #undef EV_PX |
786 | #undef EV_PX_ |
779 | #undef EV_PX_ |
787 | #undef EV_CONSTRUCT |
780 | #undef EV_CONSTRUCT |
788 | #undef EV_BEGIN_WATCHER |
781 | #undef EV_BEGIN_WATCHER |
789 | #undef EV_END_WATCHER |
782 | #undef EV_END_WATCHER |
790 | |
|
|
791 | } |
783 | } |
792 | |
784 | |
793 | #undef EV_THROW |
|
|
794 | |
|
|
795 | #endif |
785 | #endif |
796 | |
786 | |