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