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 | * |
… | |
… | |
56 | |
56 | |
57 | namespace ev { |
57 | namespace ev { |
58 | |
58 | |
59 | typedef ev_tstamp tstamp; |
59 | typedef ev_tstamp tstamp; |
60 | |
60 | |
61 | enum { |
61 | enum |
|
|
62 | { |
62 | UNDEF = EV_UNDEF, |
63 | UNDEF = EV_UNDEF, |
63 | NONE = EV_NONE, |
64 | NONE = EV_NONE, |
64 | READ = EV_READ, |
65 | READ = EV_READ, |
65 | WRITE = EV_WRITE, |
66 | WRITE = EV_WRITE, |
66 | TIMEOUT = EV_TIMEOUT, |
67 | TIMEOUT = EV_TIMEOUT, |
… | |
… | |
72 | CHECK = EV_CHECK, |
73 | CHECK = EV_CHECK, |
73 | PREPARE = EV_PREPARE, |
74 | PREPARE = EV_PREPARE, |
74 | FORK = EV_FORK, |
75 | FORK = EV_FORK, |
75 | ASYNC = EV_ASYNC, |
76 | ASYNC = EV_ASYNC, |
76 | EMBED = EV_EMBED, |
77 | EMBED = EV_EMBED, |
|
|
78 | # undef ERROR // some systems stupidly #define ERROR |
77 | ERROR = EV_ERROR, |
79 | ERROR = EV_ERROR, |
78 | }; |
80 | }; |
79 | |
81 | |
80 | enum |
82 | enum |
81 | { |
83 | { |
82 | AUTO = EVFLAG_AUTO, |
84 | AUTO = EVFLAG_AUTO, |
83 | NOENV = EVFLAG_NOENV, |
85 | NOENV = EVFLAG_NOENV, |
84 | FORKCHECK = EVFLAG_FORKCHECK, |
86 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
87 | |
85 | SELECT = EVBACKEND_SELECT, |
88 | SELECT = EVBACKEND_SELECT, |
86 | POLL = EVBACKEND_POLL, |
89 | POLL = EVBACKEND_POLL, |
87 | EPOLL = EVBACKEND_EPOLL, |
90 | EPOLL = EVBACKEND_EPOLL, |
88 | KQUEUE = EVBACKEND_KQUEUE, |
91 | KQUEUE = EVBACKEND_KQUEUE, |
89 | DEVPOLL = EVBACKEND_DEVPOLL, |
92 | DEVPOLL = EVBACKEND_DEVPOLL, |
90 | PORT = EVBACKEND_PORT |
93 | PORT = EVBACKEND_PORT |
91 | }; |
94 | }; |
92 | |
95 | |
93 | enum |
96 | enum |
94 | { |
97 | { |
95 | NONBLOCK = EVLOOP_NONBLOCK, |
98 | NONBLOCK = EVLOOP_NONBLOCK, |
96 | ONESHOT = EVLOOP_ONESHOT |
99 | ONESHOT = EVLOOP_ONESHOT |
97 | }; |
100 | }; |
98 | |
101 | |
99 | enum how_t |
102 | enum how_t |
100 | { |
103 | { |
101 | ONE = EVUNLOOP_ONE, |
104 | ONE = EVUNLOOP_ONE, |
… | |
… | |
157 | return false; |
160 | return false; |
158 | #endif |
161 | #endif |
159 | } |
162 | } |
160 | |
163 | |
161 | #if EV_MULTIPLICITY |
164 | #if EV_MULTIPLICITY |
162 | bool operator == (struct ev_loop *other) const throw () |
165 | bool operator == (const EV_P) const throw () |
163 | { |
166 | { |
164 | return this->EV_AX == other; |
167 | return this->EV_AX == EV_A; |
165 | } |
|
|
166 | |
|
|
167 | bool operator != (struct ev_loop *other) const throw () |
|
|
168 | { |
168 | } |
169 | return ! (*this == other); |
169 | |
|
|
170 | bool operator != (const EV_P) const throw () |
170 | } |
171 | { |
171 | |
|
|
172 | bool operator == (const struct ev_loop *other) const throw () |
|
|
173 | { |
|
|
174 | return this->EV_AX == other; |
|
|
175 | } |
|
|
176 | |
|
|
177 | bool operator != (const struct ev_loop *other) const throw () |
|
|
178 | { |
|
|
179 | return (*this == other); |
172 | return (*this == EV_A); |
180 | } |
173 | } |
181 | |
174 | |
182 | operator struct ev_loop * () const throw () |
175 | operator struct ev_loop * () const throw () |
183 | { |
176 | { |
184 | return EV_AX; |
177 | return EV_AX; |
… | |
… | |
248 | { |
241 | { |
249 | ev_set_timeout_collect_interval (EV_AX_ interval); |
242 | ev_set_timeout_collect_interval (EV_AX_ interval); |
250 | } |
243 | } |
251 | |
244 | |
252 | // function callback |
245 | // function callback |
253 | 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 () |
254 | { |
247 | { |
255 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
248 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
256 | } |
249 | } |
257 | |
250 | |
258 | // method callback |
251 | // method callback |
… | |
… | |
260 | void once (int fd, int events, tstamp timeout, K *object) throw () |
253 | void once (int fd, int events, tstamp timeout, K *object) throw () |
261 | { |
254 | { |
262 | once (fd, events, timeout, method_thunk<K, method>, object); |
255 | once (fd, events, timeout, method_thunk<K, method>, object); |
263 | } |
256 | } |
264 | |
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 | |
265 | template<class K, void (K::*method)(int)> |
265 | template<class K, void (K::*method)(int)> |
266 | static void method_thunk (int revents, void* arg) |
266 | static void method_thunk (int revents, void *arg) |
267 | { |
|
|
268 | K *obj = static_cast<K *>(arg); |
|
|
269 | (obj->*method) (revents); |
|
|
270 | } |
267 | { |
|
|
268 | static_cast<K *>(arg)->*method |
|
|
269 | (revents); |
|
|
270 | } |
271 | |
271 | |
272 | // const method callback |
272 | // no-argument method callback |
273 | template<class K, void (K::*method)(int) const> |
|
|
274 | void once (int fd, int events, tstamp timeout, const K *object) throw () |
|
|
275 | { |
|
|
276 | once (fd, events, timeout, const_method_thunk<K, method>, object); |
|
|
277 | } |
|
|
278 | |
|
|
279 | template<class K, void (K::*method)(int) const> |
|
|
280 | static void const_method_thunk (int revents, void* arg) |
|
|
281 | { |
|
|
282 | K *obj = static_cast<K *>(arg); |
|
|
283 | (obj->*method) (revents); |
|
|
284 | } |
|
|
285 | |
|
|
286 | // simple method callback |
|
|
287 | template<class K, void (K::*method)()> |
273 | template<class K, void (K::*method)()> |
288 | void once (int fd, int events, tstamp timeout, K *object) throw () |
274 | void once (int fd, int events, tstamp timeout, K *object) throw () |
289 | { |
275 | { |
290 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
276 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
291 | } |
277 | } |
292 | |
278 | |
293 | template<class K, void (K::*method)()> |
279 | template<class K, void (K::*method)()> |
294 | static void method_noargs_thunk (int revents, void* arg) |
280 | static void method_noargs_thunk (int revents, void *arg) |
295 | { |
281 | { |
296 | K *obj = static_cast<K *>(arg); |
282 | static_cast<K *>(arg)->*method |
297 | (obj->*method) (); |
283 | (); |
298 | } |
284 | } |
299 | |
285 | |
300 | // simpler function callback |
286 | // simpler function callback |
301 | template<void (*cb)(int)> |
287 | template<void (*cb)(int)> |
302 | void once (int fd, int events, tstamp timeout) throw () |
288 | void once (int fd, int events, tstamp timeout) throw () |
303 | { |
289 | { |
304 | once (fd, events, timeout, simpler_func_thunk<cb>); |
290 | once (fd, events, timeout, simpler_func_thunk<cb>); |
305 | } |
291 | } |
306 | |
292 | |
307 | template<void (*cb)(int)> |
293 | template<void (*cb)(int)> |
308 | static void simpler_func_thunk (int revents, void* arg) |
294 | static void simpler_func_thunk (int revents, void *arg) |
309 | { |
295 | { |
|
|
296 | (*cb) |
310 | (*cb) (revents); |
297 | (revents); |
311 | } |
298 | } |
312 | |
299 | |
313 | // simplest function callback |
300 | // simplest function callback |
314 | template<void (*cb)()> |
301 | template<void (*cb)()> |
315 | void once (int fd, int events, tstamp timeout) throw () |
302 | void once (int fd, int events, tstamp timeout) throw () |
316 | { |
303 | { |
317 | once (fd, events, timeout, simplest_func_thunk<cb>); |
304 | once (fd, events, timeout, simplest_func_thunk<cb>); |
318 | } |
305 | } |
319 | |
306 | |
320 | template<void (*cb)()> |
307 | template<void (*cb)()> |
321 | static void simplest_func_thunk (int revents, void* arg) |
308 | static void simplest_func_thunk (int revents, void *arg) |
322 | { |
309 | { |
323 | (*cb) (); |
310 | (*cb) |
|
|
311 | (); |
324 | } |
312 | } |
325 | |
313 | |
326 | void feed_fd_event (int fd, int revents) throw () |
314 | void feed_fd_event (int fd, int revents) throw () |
327 | { |
315 | { |
328 | ev_feed_fd_event (EV_AX_ fd, revents); |
316 | ev_feed_fd_event (EV_AX_ fd, revents); |
… | |
… | |
418 | struct base : ev_watcher |
406 | struct base : ev_watcher |
419 | { |
407 | { |
420 | #if EV_MULTIPLICITY |
408 | #if EV_MULTIPLICITY |
421 | EV_PX; |
409 | EV_PX; |
422 | |
410 | |
423 | void set (EV_PX) throw () |
411 | void set (EV_P) throw () |
424 | { |
412 | { |
425 | this->EV_A = EV_A; |
413 | this->EV_A = EV_A; |
426 | } |
414 | } |
427 | #endif |
415 | #endif |
428 | |
416 | |
… | |
… | |
432 | #endif |
420 | #endif |
433 | { |
421 | { |
434 | ev_init (this, 0); |
422 | ev_init (this, 0); |
435 | } |
423 | } |
436 | |
424 | |
437 | 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 () |
438 | { |
426 | { |
439 | this->data = data; |
427 | this->data = (void *)data; |
440 | 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); |
441 | } |
443 | } |
442 | |
444 | |
443 | // method callback |
445 | // method callback |
444 | template<class K, void (K::*method)(watcher &w, int)> |
446 | template<class K, void (K::*method)(watcher &w, int)> |
445 | void set (K *object) throw () |
447 | void set (K *object) throw () |
446 | { |
448 | { |
447 | set_ (object, method_thunk<K, method>); |
449 | set_ (object, method_thunk<K, method>); |
448 | } |
450 | } |
449 | |
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 | |
450 | template<class K, void (K::*method)(watcher &w, int)> |
459 | template<class K, void (K::*method)(watcher &w, int)> |
451 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
460 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
452 | { |
461 | { |
453 | K *obj = static_cast<K *>(w->data); |
462 | (static_cast<K *>(w->data)->*method) |
454 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
455 | } |
|
|
456 | |
|
|
457 | // const method callback |
|
|
458 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
459 | void set (const K *object) throw () |
|
|
460 | { |
|
|
461 | set_ (object, const_method_thunk<K, method>); |
|
|
462 | } |
|
|
463 | |
|
|
464 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
465 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
466 | { |
|
|
467 | K *obj = static_cast<K *>(w->data); |
|
|
468 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
469 | } |
|
|
470 | |
|
|
471 | // function callback |
|
|
472 | template<void (*function)(watcher &w, int)> |
|
|
473 | void set (void *data = 0) throw () |
|
|
474 | { |
|
|
475 | set_ (data, function_thunk<function>); |
|
|
476 | } |
|
|
477 | |
|
|
478 | template<void (*function)(watcher &w, int)> |
|
|
479 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
480 | { |
|
|
481 | function (*static_cast<watcher *>(w), revents); |
463 | (*static_cast<watcher *>(w), revents); |
482 | } |
464 | } |
483 | |
465 | |
484 | // simple callback |
466 | // no-argument callback |
485 | template<class K, void (K::*method)()> |
467 | template<class K, void (K::*method)()> |
486 | void set (K *object) throw () |
468 | void set (K *object) throw () |
487 | { |
469 | { |
488 | set_ (object, method_noargs_thunk<K, method>); |
470 | set_ (object, method_noargs_thunk<K, method>); |
489 | } |
471 | } |
490 | |
472 | |
491 | template<class K, void (K::*method)()> |
473 | template<class K, void (K::*method)()> |
492 | 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) |
493 | { |
475 | { |
494 | K *obj = static_cast<K *>(w->data); |
476 | static_cast<K *>(w->data)->*method |
495 | (obj->*method) (); |
477 | (); |
496 | } |
478 | } |
497 | |
479 | |
498 | void operator ()(int events = EV_UNDEF) |
480 | void operator ()(int events = EV_UNDEF) |
499 | { |
481 | { |
|
|
482 | return |
500 | return ev_cb (static_cast<ev_watcher *>(this)) |
483 | ev_cb (static_cast<ev_watcher *>(this)) |
501 | (static_cast<ev_watcher *>(this), events); |
484 | (static_cast<ev_watcher *>(this), events); |
502 | } |
485 | } |
503 | |
486 | |
504 | bool is_active () const throw () |
487 | bool is_active () const throw () |
505 | { |
488 | { |
506 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
489 | return ev_is_active (static_cast<const ev_watcher *>(this)); |