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_USE_STDEXCEPT |
|
|
50 | # define EV_USE_STDEXCEPT 1 |
|
|
51 | #endif |
|
|
52 | |
|
|
53 | #if EV_USE_STDEXCEPT |
|
|
54 | # include <stdexcept> |
|
|
55 | #endif |
|
|
56 | |
49 | namespace ev { |
57 | namespace ev { |
50 | |
58 | |
51 | typedef ev_tstamp tstamp; |
59 | typedef ev_tstamp tstamp; |
52 | |
60 | |
53 | enum { |
61 | enum { |
… | |
… | |
62 | STAT = EV_STAT, |
70 | STAT = EV_STAT, |
63 | IDLE = EV_IDLE, |
71 | IDLE = EV_IDLE, |
64 | CHECK = EV_CHECK, |
72 | CHECK = EV_CHECK, |
65 | PREPARE = EV_PREPARE, |
73 | PREPARE = EV_PREPARE, |
66 | FORK = EV_FORK, |
74 | FORK = EV_FORK, |
|
|
75 | ASYNC = EV_ASYNC, |
67 | EMBED = EV_EMBED, |
76 | EMBED = EV_EMBED, |
|
|
77 | # undef ERROR // some systems stupidly #define ERROR |
68 | ERROR = EV_ERROR, |
78 | ERROR = EV_ERROR, |
69 | }; |
79 | }; |
70 | |
80 | |
71 | enum |
81 | enum |
72 | { |
82 | { |
73 | AUTO = EVFLAG_AUTO, |
83 | AUTO = EVFLAG_AUTO, |
74 | NOENV = EVFLAG_NOENV, |
84 | NOENV = EVFLAG_NOENV, |
75 | FORKCHECK = EVFLAG_FORKCHECK, |
85 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
86 | |
76 | SELECT = EVBACKEND_SELECT, |
87 | SELECT = EVBACKEND_SELECT, |
77 | POLL = EVBACKEND_POLL, |
88 | POLL = EVBACKEND_POLL, |
78 | EPOLL = EVBACKEND_EPOLL, |
89 | EPOLL = EVBACKEND_EPOLL, |
79 | KQUEUE = EVBACKEND_KQUEUE, |
90 | KQUEUE = EVBACKEND_KQUEUE, |
80 | DEVPOLL = EVBACKEND_DEVPOLL, |
91 | DEVPOLL = EVBACKEND_DEVPOLL, |
81 | PORT = EVBACKEND_PORT |
92 | PORT = EVBACKEND_PORT |
82 | }; |
93 | }; |
83 | |
94 | |
84 | enum |
95 | enum |
85 | { |
96 | { |
86 | NONBLOCK = EVLOOP_NONBLOCK, |
97 | NONBLOCK = EVLOOP_NONBLOCK, |
87 | ONESHOT = EVLOOP_ONESHOT |
98 | ONESHOT = EVLOOP_ONESHOT |
88 | }; |
99 | }; |
89 | |
100 | |
90 | enum how_t |
101 | enum how_t |
91 | { |
102 | { |
92 | ONE = EVUNLOOP_ONE, |
103 | ONE = EVUNLOOP_ONE, |
93 | ALL = EVUNLOOP_ALL |
104 | ALL = EVUNLOOP_ALL |
|
|
105 | }; |
|
|
106 | |
|
|
107 | struct bad_loop |
|
|
108 | #if EV_USE_STDEXCEPT |
|
|
109 | : std::runtime_error |
|
|
110 | #endif |
|
|
111 | { |
|
|
112 | #if EV_USE_STDEXCEPT |
|
|
113 | bad_loop () |
|
|
114 | : std::runtime_error ("libev event loop cannot be initialized, bad value of LIBEV_FLAGS?") |
|
|
115 | { |
|
|
116 | } |
|
|
117 | #endif |
94 | }; |
118 | }; |
95 | |
119 | |
96 | #ifdef EV_AX |
120 | #ifdef EV_AX |
97 | # undef EV_AX |
121 | # undef EV_AX |
98 | #endif |
122 | #endif |
… | |
… | |
109 | # define EV_AX_ |
133 | # define EV_AX_ |
110 | #endif |
134 | #endif |
111 | |
135 | |
112 | struct loop_ref |
136 | struct loop_ref |
113 | { |
137 | { |
114 | |
|
|
115 | loop_ref (EV_P) |
138 | loop_ref (EV_P) throw () |
116 | #if EV_MULTIPLICITY |
139 | #if EV_MULTIPLICITY |
117 | : EV_AX (EV_A) |
140 | : EV_AX (EV_A) |
118 | #endif |
141 | #endif |
119 | { |
|
|
120 | } |
142 | { |
|
|
143 | } |
121 | |
144 | |
122 | bool operator== (const loop_ref &other) const |
145 | bool operator == (const loop_ref &other) const throw () |
123 | { |
146 | { |
124 | #if EV_MULTIPLICITY |
147 | #if EV_MULTIPLICITY |
125 | return this->EV_AX == other.EV_AX; |
148 | return EV_AX == other.EV_AX; |
126 | #else |
149 | #else |
127 | return true; |
150 | return true; |
128 | #endif |
151 | #endif |
129 | } |
152 | } |
130 | |
153 | |
131 | bool operator!= (const loop_ref &other) const |
154 | bool operator != (const loop_ref &other) const throw () |
132 | { |
155 | { |
133 | #if EV_MULTIPLICITY |
156 | #if EV_MULTIPLICITY |
134 | return ! (*this == other); |
157 | return ! (*this == other); |
135 | #else |
158 | #else |
136 | return false; |
159 | return false; |
137 | #endif |
160 | #endif |
138 | } |
161 | } |
139 | |
162 | |
140 | #if EV_MULTIPLICITY |
163 | #if EV_MULTIPLICITY |
141 | bool operator== (struct ev_loop *other) const |
164 | bool operator == (struct ev_loop *other) const throw () |
142 | { |
165 | { |
143 | return this->EV_AX == other; |
166 | return this->EV_AX == other; |
144 | } |
167 | } |
145 | |
168 | |
146 | bool operator!= (struct ev_loop *other) const |
169 | bool operator != (struct ev_loop *other) const throw () |
147 | { |
170 | { |
148 | return ! (*this == other); |
171 | return ! (*this == other); |
149 | } |
172 | } |
150 | |
173 | |
151 | bool operator== (const struct ev_loop *other) const |
174 | bool operator == (const struct ev_loop *other) const throw () |
152 | { |
175 | { |
153 | return this->EV_AX == other; |
176 | return this->EV_AX == other; |
154 | } |
177 | } |
155 | |
178 | |
156 | bool operator!= (const struct ev_loop *other) const |
179 | bool operator != (const struct ev_loop *other) const throw () |
157 | { |
180 | { |
158 | return (*this == other); |
181 | return (*this == other); |
159 | } |
182 | } |
160 | |
183 | |
161 | operator struct ev_loop * () const |
184 | operator struct ev_loop * () const throw () |
162 | { |
185 | { |
163 | return EV_AX; |
186 | return EV_AX; |
164 | } |
187 | } |
165 | |
188 | |
166 | operator const struct ev_loop * () const |
189 | operator const struct ev_loop * () const throw () |
167 | { |
190 | { |
168 | return EV_AX; |
191 | return EV_AX; |
169 | } |
192 | } |
170 | |
193 | |
171 | bool is_default () const |
194 | bool is_default () const throw () |
172 | { |
195 | { |
173 | return EV_AX == ev_default_loop (0); |
196 | return EV_AX == ev_default_loop (0); |
174 | } |
197 | } |
175 | #endif |
198 | #endif |
176 | |
199 | |
177 | void loop (int flags = 0) |
200 | void loop (int flags = 0) |
178 | { |
201 | { |
179 | ev_loop (EV_AX_ flags); |
202 | ev_loop (EV_AX_ flags); |
180 | } |
203 | } |
181 | |
204 | |
182 | void unloop (how_t how = ONE) |
205 | void unloop (how_t how = ONE) throw () |
183 | { |
206 | { |
184 | ev_unloop (EV_AX_ how); |
207 | ev_unloop (EV_AX_ how); |
185 | } |
208 | } |
186 | |
209 | |
187 | void post_fork () |
210 | void post_fork () throw () |
188 | { |
211 | { |
189 | #if EV_MULTIPLICITY |
212 | #if EV_MULTIPLICITY |
190 | ev_loop_fork (EV_AX); |
213 | ev_loop_fork (EV_AX); |
191 | #else |
214 | #else |
192 | ev_default_fork (); |
215 | ev_default_fork (); |
193 | #endif |
216 | #endif |
194 | } |
217 | } |
195 | |
218 | |
196 | unsigned int count () const |
219 | unsigned int count () const throw () |
197 | { |
220 | { |
198 | return ev_loop_count (EV_AX); |
221 | return ev_loop_count (EV_AX); |
199 | } |
222 | } |
200 | |
223 | |
201 | unsigned int backend () const |
224 | unsigned int backend () const throw () |
202 | { |
225 | { |
203 | return ev_backend (EV_AX); |
226 | return ev_backend (EV_AX); |
204 | } |
227 | } |
205 | |
228 | |
206 | tstamp now () const |
229 | tstamp now () const throw () |
207 | { |
230 | { |
208 | return ev_now (EV_AX); |
231 | return ev_now (EV_AX); |
209 | } |
232 | } |
210 | |
233 | |
211 | void ref () |
234 | void ref () throw () |
212 | { |
235 | { |
213 | ev_ref (EV_AX); |
236 | ev_ref (EV_AX); |
214 | } |
237 | } |
215 | |
238 | |
216 | void unref () |
239 | void unref () throw () |
217 | { |
240 | { |
218 | ev_unref (EV_AX); |
241 | ev_unref (EV_AX); |
219 | } |
242 | } |
220 | |
243 | |
221 | void set_io_collect_interval (tstamp interval) |
244 | void set_io_collect_interval (tstamp interval) throw () |
222 | { |
245 | { |
223 | ev_set_io_collect_interval (EV_AX_ interval); |
246 | ev_set_io_collect_interval (EV_AX_ interval); |
224 | } |
247 | } |
225 | |
248 | |
226 | void set_timeout_collect_interval (tstamp interval) |
249 | void set_timeout_collect_interval (tstamp interval) throw () |
227 | { |
250 | { |
228 | ev_set_timeout_collect_interval (EV_AX_ interval); |
251 | ev_set_timeout_collect_interval (EV_AX_ interval); |
229 | } |
252 | } |
230 | |
253 | |
231 | // function callback |
254 | // function callback |
232 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) |
255 | void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void* arg = 0) throw () |
233 | { |
256 | { |
234 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
257 | ev_once (EV_AX_ fd, events, timeout, cb, arg); |
235 | } |
258 | } |
236 | |
259 | |
237 | // method callback |
260 | // method callback |
238 | template<class K, void (K::*method)(int)> |
261 | template<class K, void (K::*method)(int)> |
239 | void once (int fd, int events, tstamp timeout, K *object) |
262 | void once (int fd, int events, tstamp timeout, K *object) throw () |
240 | { |
263 | { |
241 | once (fd, events, timeout, method_thunk<K, method>, object); |
264 | once (fd, events, timeout, method_thunk<K, method>, object); |
242 | } |
265 | } |
243 | |
266 | |
244 | template<class K, void (K::*method)(int)> |
267 | template<class K, void (K::*method)(int)> |
… | |
… | |
248 | (obj->*method) (revents); |
271 | (obj->*method) (revents); |
249 | } |
272 | } |
250 | |
273 | |
251 | // const method callback |
274 | // const method callback |
252 | template<class K, void (K::*method)(int) const> |
275 | template<class K, void (K::*method)(int) const> |
253 | void once (int fd, int events, tstamp timeout, const K *object) |
276 | void once (int fd, int events, tstamp timeout, const K *object) throw () |
254 | { |
277 | { |
255 | once (fd, events, timeout, const_method_thunk<K, method>, object); |
278 | once (fd, events, timeout, const_method_thunk<K, method>, object); |
256 | } |
279 | } |
257 | |
280 | |
258 | template<class K, void (K::*method)(int) const> |
281 | template<class K, void (K::*method)(int) const> |
… | |
… | |
262 | (obj->*method) (revents); |
285 | (obj->*method) (revents); |
263 | } |
286 | } |
264 | |
287 | |
265 | // simple method callback |
288 | // simple method callback |
266 | template<class K, void (K::*method)()> |
289 | template<class K, void (K::*method)()> |
267 | void once (int fd, int events, tstamp timeout, K *object) |
290 | void once (int fd, int events, tstamp timeout, K *object) throw () |
268 | { |
291 | { |
269 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
292 | once (fd, events, timeout, method_noargs_thunk<K, method>, object); |
270 | } |
293 | } |
271 | |
294 | |
272 | template<class K, void (K::*method)()> |
295 | template<class K, void (K::*method)()> |
… | |
… | |
276 | (obj->*method) (); |
299 | (obj->*method) (); |
277 | } |
300 | } |
278 | |
301 | |
279 | // simpler function callback |
302 | // simpler function callback |
280 | template<void (*cb)(int)> |
303 | template<void (*cb)(int)> |
281 | void once (int fd, int events, tstamp timeout) |
304 | void once (int fd, int events, tstamp timeout) throw () |
282 | { |
305 | { |
283 | once (fd, events, timeout, simpler_func_thunk<cb>); |
306 | once (fd, events, timeout, simpler_func_thunk<cb>); |
284 | } |
307 | } |
285 | |
308 | |
286 | template<void (*cb)(int)> |
309 | template<void (*cb)(int)> |
… | |
… | |
289 | (*cb) (revents); |
312 | (*cb) (revents); |
290 | } |
313 | } |
291 | |
314 | |
292 | // simplest function callback |
315 | // simplest function callback |
293 | template<void (*cb)()> |
316 | template<void (*cb)()> |
294 | void once (int fd, int events, tstamp timeout) |
317 | void once (int fd, int events, tstamp timeout) throw () |
295 | { |
318 | { |
296 | once (fd, events, timeout, simplest_func_thunk<cb>); |
319 | once (fd, events, timeout, simplest_func_thunk<cb>); |
297 | } |
320 | } |
298 | |
321 | |
299 | template<void (*cb)()> |
322 | template<void (*cb)()> |
300 | static void simplest_func_thunk (int revents, void* arg) |
323 | static void simplest_func_thunk (int revents, void* arg) |
301 | { |
324 | { |
302 | (*cb) (); |
325 | (*cb) (); |
303 | } |
326 | } |
304 | |
327 | |
305 | void feed_fd_event (int fd, int revents) |
328 | void feed_fd_event (int fd, int revents) throw () |
306 | { |
329 | { |
307 | ev_feed_fd_event (EV_AX_ fd, revents); |
330 | ev_feed_fd_event (EV_AX_ fd, revents); |
308 | } |
331 | } |
309 | |
332 | |
310 | void feed_signal_event (int signum) |
333 | void feed_signal_event (int signum) throw () |
311 | { |
334 | { |
312 | ev_feed_signal_event (EV_AX_ signum); |
335 | ev_feed_signal_event (EV_AX_ signum); |
313 | } |
336 | } |
314 | |
337 | |
315 | #if EV_MULTIPLICITY |
338 | #if EV_MULTIPLICITY |
… | |
… | |
317 | #endif |
340 | #endif |
318 | |
341 | |
319 | }; |
342 | }; |
320 | |
343 | |
321 | #if EV_MULTIPLICITY |
344 | #if EV_MULTIPLICITY |
322 | struct dynamic_loop: loop_ref |
345 | struct dynamic_loop : loop_ref |
323 | { |
346 | { |
324 | |
347 | |
325 | dynamic_loop (unsigned int flags = AUTO) |
348 | dynamic_loop (unsigned int flags = AUTO) throw (bad_loop) |
326 | : loop_ref (ev_loop_new (flags)) |
349 | : loop_ref (ev_loop_new (flags)) |
327 | { |
|
|
328 | } |
350 | { |
|
|
351 | if (!EV_AX) |
|
|
352 | throw bad_loop (); |
|
|
353 | } |
329 | |
354 | |
330 | ~dynamic_loop () |
355 | ~dynamic_loop () throw () |
331 | { |
356 | { |
332 | ev_loop_destroy (EV_AX); |
357 | ev_loop_destroy (EV_AX); |
333 | EV_AX = 0; |
358 | EV_AX = 0; |
334 | } |
359 | } |
335 | |
360 | |
… | |
… | |
340 | dynamic_loop & operator= (const dynamic_loop &); |
365 | dynamic_loop & operator= (const dynamic_loop &); |
341 | |
366 | |
342 | }; |
367 | }; |
343 | #endif |
368 | #endif |
344 | |
369 | |
345 | struct default_loop: loop_ref |
370 | struct default_loop : loop_ref |
346 | { |
371 | { |
347 | |
|
|
348 | default_loop (unsigned int flags = AUTO) |
372 | default_loop (unsigned int flags = AUTO) throw (bad_loop) |
349 | #if EV_MULTIPLICITY |
373 | #if EV_MULTIPLICITY |
350 | : loop_ref (ev_default_loop (flags)) |
374 | : loop_ref (ev_default_loop (flags)) |
351 | { |
375 | #endif |
352 | } |
376 | { |
|
|
377 | if ( |
|
|
378 | #if EV_MULTIPLICITY |
|
|
379 | !EV_AX |
353 | #else |
380 | #else |
354 | { |
|
|
355 | ev_default_loop (flags); |
381 | !ev_default_loop (flags) |
356 | } |
|
|
357 | #endif |
382 | #endif |
|
|
383 | ) |
|
|
384 | throw bad_loop (); |
|
|
385 | } |
358 | |
386 | |
359 | ~default_loop () |
387 | ~default_loop () throw () |
360 | { |
388 | { |
361 | ev_default_destroy (); |
389 | ev_default_destroy (); |
362 | #if EV_MULTIPLICITY |
|
|
363 | EV_AX = 0; |
|
|
364 | #endif |
|
|
365 | } |
390 | } |
366 | |
391 | |
367 | private: |
392 | private: |
368 | |
|
|
369 | default_loop (const default_loop &); |
393 | default_loop (const default_loop &); |
370 | |
|
|
371 | default_loop & operator= (const default_loop &); |
394 | default_loop &operator = (const default_loop &); |
372 | |
|
|
373 | }; |
395 | }; |
374 | |
396 | |
375 | inline loop_ref get_default_loop () |
397 | inline loop_ref get_default_loop () throw () |
376 | { |
398 | { |
377 | #if EV_MULTIPLICITY |
399 | #if EV_MULTIPLICITY |
378 | return ev_default_loop (0); |
400 | return ev_default_loop (0); |
379 | #else |
401 | #else |
380 | return loop_ref (); |
402 | return loop_ref (); |
… | |
… | |
398 | struct base : ev_watcher |
420 | struct base : ev_watcher |
399 | { |
421 | { |
400 | #if EV_MULTIPLICITY |
422 | #if EV_MULTIPLICITY |
401 | EV_PX; |
423 | EV_PX; |
402 | |
424 | |
403 | void set (EV_PX) |
425 | void set (EV_PX) throw () |
404 | { |
426 | { |
405 | this->EV_A = EV_A; |
427 | this->EV_A = EV_A; |
406 | } |
428 | } |
407 | #endif |
429 | #endif |
408 | |
430 | |
409 | base (EV_PX) |
431 | base (EV_PX) throw () |
410 | #if EV_MULTIPLICITY |
432 | #if EV_MULTIPLICITY |
411 | : EV_A (EV_A) |
433 | : EV_A (EV_A) |
412 | #endif |
434 | #endif |
413 | { |
435 | { |
414 | ev_init (this, 0); |
436 | ev_init (this, 0); |
415 | } |
437 | } |
416 | |
438 | |
417 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
439 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) throw () |
418 | { |
440 | { |
419 | this->data = data; |
441 | this->data = data; |
420 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
442 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
421 | } |
443 | } |
422 | |
444 | |
423 | // method callback |
445 | // method callback |
424 | template<class K, void (K::*method)(watcher &w, int)> |
446 | template<class K, void (K::*method)(watcher &w, int)> |
425 | void set (K *object) |
447 | void set (K *object) throw () |
426 | { |
448 | { |
427 | set_ (object, method_thunk<K, method>); |
449 | set_ (object, method_thunk<K, method>); |
428 | } |
450 | } |
429 | |
451 | |
430 | template<class K, void (K::*method)(watcher &w, int)> |
452 | template<class K, void (K::*method)(watcher &w, int)> |
… | |
… | |
434 | (obj->*method) (*static_cast<watcher *>(w), revents); |
456 | (obj->*method) (*static_cast<watcher *>(w), revents); |
435 | } |
457 | } |
436 | |
458 | |
437 | // const method callback |
459 | // const method callback |
438 | template<class K, void (K::*method)(watcher &w, int) const> |
460 | template<class K, void (K::*method)(watcher &w, int) const> |
439 | void set (const K *object) |
461 | void set (const K *object) throw () |
440 | { |
462 | { |
441 | set_ (object, const_method_thunk<K, method>); |
463 | set_ (object, const_method_thunk<K, method>); |
442 | } |
464 | } |
443 | |
465 | |
444 | template<class K, void (K::*method)(watcher &w, int) const> |
466 | template<class K, void (K::*method)(watcher &w, int) const> |
… | |
… | |
448 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
470 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
449 | } |
471 | } |
450 | |
472 | |
451 | // function callback |
473 | // function callback |
452 | template<void (*function)(watcher &w, int)> |
474 | template<void (*function)(watcher &w, int)> |
453 | void set (void *data = 0) |
475 | void set (void *data = 0) throw () |
454 | { |
476 | { |
455 | set_ (data, function_thunk<function>); |
477 | set_ (data, function_thunk<function>); |
456 | } |
478 | } |
457 | |
479 | |
458 | template<void (*function)(watcher &w, int)> |
480 | template<void (*function)(watcher &w, int)> |
… | |
… | |
461 | function (*static_cast<watcher *>(w), revents); |
483 | function (*static_cast<watcher *>(w), revents); |
462 | } |
484 | } |
463 | |
485 | |
464 | // simple callback |
486 | // simple callback |
465 | template<class K, void (K::*method)()> |
487 | template<class K, void (K::*method)()> |
466 | void set (K *object) |
488 | void set (K *object) throw () |
467 | { |
489 | { |
468 | set_ (object, method_noargs_thunk<K, method>); |
490 | set_ (object, method_noargs_thunk<K, method>); |
469 | } |
491 | } |
470 | |
492 | |
471 | template<class K, void (K::*method)()> |
493 | template<class K, void (K::*method)()> |
… | |
… | |
479 | { |
501 | { |
480 | return ev_cb (static_cast<ev_watcher *>(this)) |
502 | return ev_cb (static_cast<ev_watcher *>(this)) |
481 | (static_cast<ev_watcher *>(this), events); |
503 | (static_cast<ev_watcher *>(this), events); |
482 | } |
504 | } |
483 | |
505 | |
484 | bool is_active () const |
506 | bool is_active () const throw () |
485 | { |
507 | { |
486 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
508 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
487 | } |
509 | } |
488 | |
510 | |
489 | bool is_pending () const |
511 | bool is_pending () const throw () |
490 | { |
512 | { |
491 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
513 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
492 | } |
514 | } |
493 | |
515 | |
494 | void feed_event (int revents) |
516 | void feed_event (int revents) throw () |
495 | { |
517 | { |
496 | ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents); |
518 | ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents); |
497 | } |
519 | } |
498 | }; |
520 | }; |
499 | |
521 | |
500 | inline tstamp now () |
522 | inline tstamp now () throw () |
501 | { |
523 | { |
502 | return ev_time (); |
524 | return ev_time (); |
503 | } |
525 | } |
504 | |
526 | |
505 | inline void delay (tstamp interval) |
527 | inline void delay (tstamp interval) throw () |
506 | { |
528 | { |
507 | ev_sleep (interval); |
529 | ev_sleep (interval); |
508 | } |
530 | } |
509 | |
531 | |
510 | inline int version_major () |
532 | inline int version_major () throw () |
511 | { |
533 | { |
512 | return ev_version_major (); |
534 | return ev_version_major (); |
513 | } |
535 | } |
514 | |
536 | |
515 | inline int version_minor () |
537 | inline int version_minor () throw () |
516 | { |
538 | { |
517 | return ev_version_minor (); |
539 | return ev_version_minor (); |
518 | } |
540 | } |
519 | |
541 | |
520 | inline unsigned int supported_backends () |
542 | inline unsigned int supported_backends () throw () |
521 | { |
543 | { |
522 | return ev_supported_backends (); |
544 | return ev_supported_backends (); |
523 | } |
545 | } |
524 | |
546 | |
525 | inline unsigned int recommended_backends () |
547 | inline unsigned int recommended_backends () throw () |
526 | { |
548 | { |
527 | return ev_recommended_backends (); |
549 | return ev_recommended_backends (); |
528 | } |
550 | } |
529 | |
551 | |
530 | inline unsigned int embeddable_backends () |
552 | inline unsigned int embeddable_backends () throw () |
531 | { |
553 | { |
532 | return ev_embeddable_backends (); |
554 | return ev_embeddable_backends (); |
533 | } |
555 | } |
534 | |
556 | |
535 | inline void set_allocator (void *(*cb)(void *ptr, long size)) |
557 | inline void set_allocator (void *(*cb)(void *ptr, long size)) throw () |
536 | { |
558 | { |
537 | ev_set_allocator (cb); |
559 | ev_set_allocator (cb); |
538 | } |
560 | } |
539 | |
561 | |
540 | inline void set_syserr_cb (void (*cb)(const char *msg)) |
562 | inline void set_syserr_cb (void (*cb)(const char *msg)) throw () |
541 | { |
563 | { |
542 | ev_set_syserr_cb (cb); |
564 | ev_set_syserr_cb (cb); |
543 | } |
565 | } |
544 | |
566 | |
545 | #if EV_MULTIPLICITY |
567 | #if EV_MULTIPLICITY |
546 | #define EV_CONSTRUCT(cppstem,cstem) \ |
568 | #define EV_CONSTRUCT(cppstem,cstem) \ |
547 | (EV_PX = get_default_loop ()) \ |
569 | (EV_PX = get_default_loop ()) throw () \ |
548 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
570 | : base<ev_ ## cstem, cppstem> (EV_A) \ |
549 | { \ |
571 | { \ |
550 | } |
572 | } |
551 | #else |
573 | #else |
552 | #define EV_CONSTRUCT(cppstem,cstem) \ |
574 | #define EV_CONSTRUCT(cppstem,cstem) \ |
553 | () \ |
575 | () throw () \ |
554 | { \ |
576 | { \ |
555 | } |
577 | } |
556 | #endif |
578 | #endif |
557 | |
579 | |
558 | /* using a template here would require quite a bit more lines, |
580 | /* using a template here would require quite a bit more lines, |
559 | * so a macro solution was chosen */ |
581 | * so a macro solution was chosen */ |
560 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
582 | #define EV_BEGIN_WATCHER(cppstem,cstem) \ |
561 | \ |
583 | \ |
562 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
584 | struct cppstem : base<ev_ ## cstem, cppstem> \ |
563 | { \ |
585 | { \ |
564 | void start () \ |
586 | void start () throw () \ |
565 | { \ |
587 | { \ |
566 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
588 | ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
567 | } \ |
589 | } \ |
568 | \ |
590 | \ |
569 | void stop () \ |
591 | void stop () throw () \ |
570 | { \ |
592 | { \ |
571 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
593 | ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
572 | } \ |
594 | } \ |
573 | \ |
595 | \ |
574 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
596 | cppstem EV_CONSTRUCT(cppstem,cstem) \ |
575 | \ |
597 | \ |
576 | ~cppstem () \ |
598 | ~cppstem () throw () \ |
577 | { \ |
599 | { \ |
578 | stop (); \ |
600 | stop (); \ |
579 | } \ |
601 | } \ |
580 | \ |
602 | \ |
581 | using base<ev_ ## cstem, cppstem>::set; \ |
603 | using base<ev_ ## cstem, cppstem>::set; \ |
582 | \ |
604 | \ |
583 | private: \ |
605 | private: \ |
584 | \ |
606 | \ |
585 | cppstem (const cppstem &o); \ |
607 | cppstem (const cppstem &o); \ |
586 | \ |
608 | \ |
587 | cppstem & operator =(const cppstem &o); \ |
609 | cppstem &operator =(const cppstem &o); \ |
588 | \ |
610 | \ |
589 | public: |
611 | public: |
590 | |
612 | |
591 | #define EV_END_WATCHER(cppstem,cstem) \ |
613 | #define EV_END_WATCHER(cppstem,cstem) \ |
592 | }; |
614 | }; |
593 | |
615 | |
594 | EV_BEGIN_WATCHER (io, io) |
616 | EV_BEGIN_WATCHER (io, io) |
595 | void set (int fd, int events) |
617 | void set (int fd, int events) throw () |
596 | { |
618 | { |
597 | int active = is_active (); |
619 | int active = is_active (); |
598 | if (active) stop (); |
620 | if (active) stop (); |
599 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
621 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
600 | if (active) start (); |
622 | if (active) start (); |
601 | } |
623 | } |
602 | |
624 | |
603 | void set (int events) |
625 | void set (int events) throw () |
604 | { |
626 | { |
605 | int active = is_active (); |
627 | int active = is_active (); |
606 | if (active) stop (); |
628 | if (active) stop (); |
607 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
629 | ev_io_set (static_cast<ev_io *>(this), fd, events); |
608 | if (active) start (); |
630 | if (active) start (); |
609 | } |
631 | } |
610 | |
632 | |
611 | void start (int fd, int events) |
633 | void start (int fd, int events) throw () |
612 | { |
634 | { |
613 | set (fd, events); |
635 | set (fd, events); |
614 | start (); |
636 | start (); |
615 | } |
637 | } |
616 | EV_END_WATCHER (io, io) |
638 | EV_END_WATCHER (io, io) |
617 | |
639 | |
618 | EV_BEGIN_WATCHER (timer, timer) |
640 | EV_BEGIN_WATCHER (timer, timer) |
619 | void set (ev_tstamp after, ev_tstamp repeat = 0.) |
641 | void set (ev_tstamp after, ev_tstamp repeat = 0.) throw () |
620 | { |
642 | { |
621 | int active = is_active (); |
643 | int active = is_active (); |
622 | if (active) stop (); |
644 | if (active) stop (); |
623 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
645 | ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
624 | if (active) start (); |
646 | if (active) start (); |
625 | } |
647 | } |
626 | |
648 | |
627 | void start (ev_tstamp after, ev_tstamp repeat = 0.) |
649 | void start (ev_tstamp after, ev_tstamp repeat = 0.) throw () |
628 | { |
650 | { |
629 | set (after, repeat); |
651 | set (after, repeat); |
630 | start (); |
652 | start (); |
631 | } |
653 | } |
632 | |
654 | |
633 | void again () |
655 | void again () throw () |
634 | { |
656 | { |
635 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
657 | ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
636 | } |
658 | } |
637 | EV_END_WATCHER (timer, timer) |
659 | EV_END_WATCHER (timer, timer) |
638 | |
660 | |
639 | #if EV_PERIODIC_ENABLE |
661 | #if EV_PERIODIC_ENABLE |
640 | EV_BEGIN_WATCHER (periodic, periodic) |
662 | EV_BEGIN_WATCHER (periodic, periodic) |
641 | void set (ev_tstamp at, ev_tstamp interval = 0.) |
663 | void set (ev_tstamp at, ev_tstamp interval = 0.) throw () |
642 | { |
664 | { |
643 | int active = is_active (); |
665 | int active = is_active (); |
644 | if (active) stop (); |
666 | if (active) stop (); |
645 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
667 | ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
646 | if (active) start (); |
668 | if (active) start (); |
647 | } |
669 | } |
648 | |
670 | |
649 | void start (ev_tstamp at, ev_tstamp interval = 0.) |
671 | void start (ev_tstamp at, ev_tstamp interval = 0.) throw () |
650 | { |
672 | { |
651 | set (at, interval); |
673 | set (at, interval); |
652 | start (); |
674 | start (); |
653 | } |
675 | } |
654 | |
676 | |
655 | void again () |
677 | void again () throw () |
656 | { |
678 | { |
657 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
679 | ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
658 | } |
680 | } |
659 | EV_END_WATCHER (periodic, periodic) |
681 | EV_END_WATCHER (periodic, periodic) |
660 | #endif |
682 | #endif |
661 | |
683 | |
662 | EV_BEGIN_WATCHER (sig, signal) |
684 | EV_BEGIN_WATCHER (sig, signal) |
663 | void set (int signum) |
685 | void set (int signum) throw () |
664 | { |
686 | { |
665 | int active = is_active (); |
687 | int active = is_active (); |
666 | if (active) stop (); |
688 | if (active) stop (); |
667 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
689 | ev_signal_set (static_cast<ev_signal *>(this), signum); |
668 | if (active) start (); |
690 | if (active) start (); |
669 | } |
691 | } |
670 | |
692 | |
671 | void start (int signum) |
693 | void start (int signum) throw () |
672 | { |
694 | { |
673 | set (signum); |
695 | set (signum); |
674 | start (); |
696 | start (); |
675 | } |
697 | } |
676 | EV_END_WATCHER (sig, signal) |
698 | EV_END_WATCHER (sig, signal) |
677 | |
699 | |
678 | EV_BEGIN_WATCHER (child, child) |
700 | EV_BEGIN_WATCHER (child, child) |
679 | void set (int pid) |
701 | void set (int pid, int trace = 0) throw () |
680 | { |
702 | { |
681 | int active = is_active (); |
703 | int active = is_active (); |
682 | if (active) stop (); |
704 | if (active) stop (); |
683 | ev_child_set (static_cast<ev_child *>(this), pid); |
705 | ev_child_set (static_cast<ev_child *>(this), pid, trace); |
684 | if (active) start (); |
706 | if (active) start (); |
685 | } |
707 | } |
686 | |
708 | |
687 | void start (int pid) |
709 | void start (int pid, int trace = 0) throw () |
688 | { |
710 | { |
689 | set (pid); |
711 | set (pid, trace); |
690 | start (); |
712 | start (); |
691 | } |
713 | } |
692 | EV_END_WATCHER (child, child) |
714 | EV_END_WATCHER (child, child) |
693 | |
715 | |
694 | #if EV_STAT_ENABLE |
716 | #if EV_STAT_ENABLE |
695 | EV_BEGIN_WATCHER (stat, stat) |
717 | EV_BEGIN_WATCHER (stat, stat) |
696 | void set (const char *path, ev_tstamp interval = 0.) |
718 | void set (const char *path, ev_tstamp interval = 0.) throw () |
697 | { |
719 | { |
698 | int active = is_active (); |
720 | int active = is_active (); |
699 | if (active) stop (); |
721 | if (active) stop (); |
700 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
722 | ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
701 | if (active) start (); |
723 | if (active) start (); |
702 | } |
724 | } |
703 | |
725 | |
704 | void start (const char *path, ev_tstamp interval = 0.) |
726 | void start (const char *path, ev_tstamp interval = 0.) throw () |
705 | { |
727 | { |
706 | stop (); |
728 | stop (); |
707 | set (path, interval); |
729 | set (path, interval); |
708 | start (); |
730 | start (); |
709 | } |
731 | } |
710 | |
732 | |
711 | void update () |
733 | void update () throw () |
712 | { |
734 | { |
713 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
735 | ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
714 | } |
736 | } |
715 | EV_END_WATCHER (stat, stat) |
737 | EV_END_WATCHER (stat, stat) |
716 | #endif |
738 | #endif |
717 | |
739 | |
718 | EV_BEGIN_WATCHER (idle, idle) |
740 | EV_BEGIN_WATCHER (idle, idle) |
719 | void set () { } |
741 | void set () throw () { } |
720 | EV_END_WATCHER (idle, idle) |
742 | EV_END_WATCHER (idle, idle) |
721 | |
743 | |
722 | EV_BEGIN_WATCHER (prepare, prepare) |
744 | EV_BEGIN_WATCHER (prepare, prepare) |
723 | void set () { } |
745 | void set () throw () { } |
724 | EV_END_WATCHER (prepare, prepare) |
746 | EV_END_WATCHER (prepare, prepare) |
725 | |
747 | |
726 | EV_BEGIN_WATCHER (check, check) |
748 | EV_BEGIN_WATCHER (check, check) |
727 | void set () { } |
749 | void set () throw () { } |
728 | EV_END_WATCHER (check, check) |
750 | EV_END_WATCHER (check, check) |
729 | |
751 | |
730 | #if EV_EMBED_ENABLE |
752 | #if EV_EMBED_ENABLE |
731 | EV_BEGIN_WATCHER (embed, embed) |
753 | EV_BEGIN_WATCHER (embed, embed) |
732 | void start (struct ev_loop *embedded_loop) |
754 | void set (struct ev_loop *embedded_loop) throw () |
733 | { |
755 | { |
734 | stop (); |
756 | int active = is_active (); |
|
|
757 | if (active) stop (); |
735 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
758 | ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
|
|
759 | if (active) start (); |
|
|
760 | } |
|
|
761 | |
|
|
762 | void start (struct ev_loop *embedded_loop) throw () |
|
|
763 | { |
|
|
764 | set (embedded_loop); |
736 | start (); |
765 | start (); |
737 | } |
766 | } |
738 | |
767 | |
739 | void sweep () |
768 | void sweep () |
740 | { |
769 | { |
… | |
… | |
743 | EV_END_WATCHER (embed, embed) |
772 | EV_END_WATCHER (embed, embed) |
744 | #endif |
773 | #endif |
745 | |
774 | |
746 | #if EV_FORK_ENABLE |
775 | #if EV_FORK_ENABLE |
747 | EV_BEGIN_WATCHER (fork, fork) |
776 | EV_BEGIN_WATCHER (fork, fork) |
748 | void set () { } |
777 | void set () throw () { } |
749 | EV_END_WATCHER (fork, fork) |
778 | EV_END_WATCHER (fork, fork) |
|
|
779 | #endif |
|
|
780 | |
|
|
781 | #if EV_ASYNC_ENABLE |
|
|
782 | EV_BEGIN_WATCHER (async, async) |
|
|
783 | void set () throw () { } |
|
|
784 | |
|
|
785 | void send () throw () |
|
|
786 | { |
|
|
787 | ev_async_send (EV_A_ static_cast<ev_async *>(this)); |
|
|
788 | } |
|
|
789 | |
|
|
790 | bool async_pending () throw () |
|
|
791 | { |
|
|
792 | return ev_async_pending (static_cast<ev_async *>(this)); |
|
|
793 | } |
|
|
794 | EV_END_WATCHER (async, async) |
750 | #endif |
795 | #endif |
751 | |
796 | |
752 | #undef EV_PX |
797 | #undef EV_PX |
753 | #undef EV_PX_ |
798 | #undef EV_PX_ |
754 | #undef EV_CONSTRUCT |
799 | #undef EV_CONSTRUCT |
755 | #undef EV_BEGIN_WATCHER |
800 | #undef EV_BEGIN_WATCHER |
756 | #undef EV_END_WATCHER |
801 | #undef EV_END_WATCHER |
757 | |
|
|
758 | } |
802 | } |
759 | |
803 | |
760 | #endif |
804 | #endif |
761 | |
805 | |