1 |
root |
1.21 |
/* |
2 |
|
|
* libev simple C++ wrapper classes |
3 |
|
|
* |
4 |
|
|
* Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
5 |
|
|
* All rights reserved. |
6 |
|
|
* |
7 |
|
|
* Redistribution and use in source and binary forms, with or without modifica- |
8 |
|
|
* tion, are permitted provided that the following conditions are met: |
9 |
|
|
* |
10 |
|
|
* 1. Redistributions of source code must retain the above copyright notice, |
11 |
|
|
* this list of conditions and the following disclaimer. |
12 |
|
|
* |
13 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
|
* documentation and/or other materials provided with the distribution. |
16 |
|
|
* |
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
18 |
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 |
|
|
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 |
|
|
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
21 |
|
|
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
22 |
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 |
|
|
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 |
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
25 |
|
|
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 |
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
|
|
* |
28 |
|
|
* Alternatively, the contents of this file may be used under the terms of |
29 |
|
|
* the GNU General Public License ("GPL") version 2 or any later version, |
30 |
|
|
* in which case the provisions of the GPL are applicable instead of |
31 |
|
|
* the above. If you wish to allow the use of your version of this file |
32 |
|
|
* only under the terms of the GPL and not to allow others to use your |
33 |
|
|
* version of this file under the BSD license, indicate your decision |
34 |
|
|
* by deleting the provisions above and replace them with the notice |
35 |
|
|
* and other provisions required by the GPL. If you do not delete the |
36 |
|
|
* provisions above, a recipient may use your version of this file under |
37 |
|
|
* either the BSD or the GPL. |
38 |
|
|
*/ |
39 |
|
|
|
40 |
root |
1.1 |
#ifndef EVPP_H__ |
41 |
|
|
#define EVPP_H__ |
42 |
|
|
|
43 |
root |
1.20 |
#ifdef EV_H |
44 |
|
|
# include EV_H |
45 |
|
|
#else |
46 |
root |
1.22 |
# include "ev.h" |
47 |
root |
1.20 |
#endif |
48 |
root |
1.1 |
|
49 |
|
|
namespace ev { |
50 |
|
|
|
51 |
llucax |
1.24 |
typedef ev_tstamp tstamp; |
52 |
|
|
|
53 |
|
|
enum { |
54 |
|
|
UNDEF = EV_UNDEF, |
55 |
|
|
NONE = EV_NONE, |
56 |
|
|
READ = EV_READ, |
57 |
|
|
WRITE = EV_WRITE, |
58 |
|
|
TIMEOUT = EV_TIMEOUT, |
59 |
|
|
PERIODIC = EV_PERIODIC, |
60 |
|
|
SIGNAL = EV_SIGNAL, |
61 |
|
|
CHILD = EV_CHILD, |
62 |
|
|
STAT = EV_STAT, |
63 |
|
|
IDLE = EV_IDLE, |
64 |
|
|
CHECK = EV_CHECK, |
65 |
|
|
PREPARE = EV_PREPARE, |
66 |
|
|
FORK = EV_FORK, |
67 |
|
|
EMBED = EV_EMBED, |
68 |
|
|
ERROR = EV_ERROR, |
69 |
|
|
}; |
70 |
|
|
|
71 |
llucax |
1.25 |
enum |
72 |
|
|
{ |
73 |
|
|
AUTO = EVFLAG_AUTO, |
74 |
|
|
NOENV = EVFLAG_NOENV, |
75 |
|
|
FORKCHECK = EVFLAG_FORKCHECK, |
76 |
|
|
SELECT = EVBACKEND_SELECT, |
77 |
|
|
POLL = EVBACKEND_POLL, |
78 |
|
|
EPOLL = EVBACKEND_EPOLL, |
79 |
|
|
KQUEUE = EVBACKEND_KQUEUE, |
80 |
|
|
DEVPOLL = EVBACKEND_DEVPOLL, |
81 |
|
|
PORT = EVBACKEND_PORT |
82 |
|
|
}; |
83 |
|
|
|
84 |
|
|
enum |
85 |
|
|
{ |
86 |
|
|
NONBLOCK = EVLOOP_NONBLOCK, |
87 |
|
|
ONESHOT = EVLOOP_ONESHOT |
88 |
|
|
}; |
89 |
|
|
|
90 |
|
|
enum how_t |
91 |
|
|
{ |
92 |
|
|
ONE = EVUNLOOP_ONE, |
93 |
|
|
ALL = EVUNLOOP_ALL |
94 |
|
|
}; |
95 |
|
|
|
96 |
root |
1.13 |
template<class ev_watcher, class watcher> |
97 |
|
|
struct base : ev_watcher |
98 |
root |
1.1 |
{ |
99 |
root |
1.13 |
#if EV_MULTIPLICITY |
100 |
|
|
EV_P; |
101 |
root |
1.1 |
|
102 |
root |
1.13 |
void set (EV_P) |
103 |
|
|
{ |
104 |
|
|
this->EV_A = EV_A; |
105 |
|
|
} |
106 |
|
|
#endif |
107 |
root |
1.1 |
|
108 |
root |
1.13 |
base () |
109 |
|
|
{ |
110 |
|
|
ev_init (this, 0); |
111 |
|
|
} |
112 |
|
|
|
113 |
root |
1.18 |
void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
114 |
root |
1.13 |
{ |
115 |
root |
1.17 |
this->data = data; |
116 |
root |
1.18 |
ev_set_cb (static_cast<ev_watcher *>(this), cb); |
117 |
root |
1.13 |
} |
118 |
|
|
|
119 |
root |
1.19 |
// method callback |
120 |
root |
1.13 |
template<class K, void (K::*method)(watcher &w, int)> |
121 |
|
|
void set (K *object) |
122 |
|
|
{ |
123 |
|
|
set_ (object, method_thunk<K, method>); |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
template<class K, void (K::*method)(watcher &w, int)> |
127 |
root |
1.18 |
static void method_thunk (EV_P_ ev_watcher *w, int revents) |
128 |
root |
1.13 |
{ |
129 |
root |
1.17 |
K *obj = static_cast<K *>(w->data); |
130 |
root |
1.18 |
(obj->*method) (*static_cast<watcher *>(w), revents); |
131 |
root |
1.13 |
} |
132 |
|
|
|
133 |
root |
1.19 |
// const method callback |
134 |
root |
1.13 |
template<class K, void (K::*method)(watcher &w, int) const> |
135 |
|
|
void set (const K *object) |
136 |
|
|
{ |
137 |
|
|
set_ (object, const_method_thunk<K, method>); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
template<class K, void (K::*method)(watcher &w, int) const> |
141 |
root |
1.18 |
static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
142 |
root |
1.13 |
{ |
143 |
root |
1.17 |
K *obj = static_cast<K *>(w->data); |
144 |
root |
1.18 |
(static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
145 |
root |
1.13 |
} |
146 |
|
|
|
147 |
root |
1.19 |
// function callback |
148 |
root |
1.17 |
template<void (*function)(watcher &w, int)> |
149 |
|
|
void set (void *data = 0) |
150 |
root |
1.13 |
{ |
151 |
root |
1.16 |
set_ (data, function_thunk<function>); |
152 |
root |
1.13 |
} |
153 |
|
|
|
154 |
|
|
template<void (*function)(watcher &w, int)> |
155 |
root |
1.18 |
static void function_thunk (EV_P_ ev_watcher *w, int revents) |
156 |
root |
1.13 |
{ |
157 |
root |
1.18 |
function (*static_cast<watcher *>(w), revents); |
158 |
root |
1.13 |
} |
159 |
|
|
|
160 |
root |
1.19 |
// simple callback |
161 |
|
|
template<class K, void (K::*method)()> |
162 |
|
|
void set (K *object) |
163 |
|
|
{ |
164 |
|
|
set_ (object, method_noargs_thunk<K, method>); |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
template<class K, void (K::*method)()> |
168 |
|
|
static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
169 |
|
|
{ |
170 |
|
|
K *obj = static_cast<K *>(w->data); |
171 |
|
|
(obj->*method) (); |
172 |
|
|
} |
173 |
|
|
|
174 |
root |
1.13 |
void operator ()(int events = EV_UNDEF) |
175 |
|
|
{ |
176 |
root |
1.14 |
return ev_cb (static_cast<ev_watcher *>(this)) |
177 |
|
|
(static_cast<ev_watcher *>(this), events); |
178 |
root |
1.13 |
} |
179 |
|
|
|
180 |
|
|
bool is_active () const |
181 |
root |
1.1 |
{ |
182 |
root |
1.13 |
return ev_is_active (static_cast<const ev_watcher *>(this)); |
183 |
root |
1.1 |
} |
184 |
|
|
|
185 |
root |
1.13 |
bool is_pending () const |
186 |
root |
1.1 |
{ |
187 |
root |
1.13 |
return ev_is_pending (static_cast<const ev_watcher *>(this)); |
188 |
root |
1.1 |
} |
189 |
llucax |
1.27 |
|
190 |
|
|
void feed_event (int revents) |
191 |
|
|
{ |
192 |
|
|
ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents); |
193 |
|
|
} |
194 |
root |
1.1 |
}; |
195 |
|
|
|
196 |
llucax |
1.26 |
inline void delay (tstamp interval) |
197 |
|
|
{ |
198 |
|
|
ev_sleep (interval); |
199 |
|
|
} |
200 |
|
|
|
201 |
|
|
inline int version_major () |
202 |
|
|
{ |
203 |
|
|
return ev_version_major (); |
204 |
|
|
} |
205 |
|
|
|
206 |
|
|
inline int version_minor () |
207 |
|
|
{ |
208 |
|
|
return ev_version_minor (); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
inline unsigned int supported_backends () |
212 |
|
|
{ |
213 |
|
|
return ev_supported_backends (); |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
inline unsigned int recommended_backends () |
217 |
|
|
{ |
218 |
|
|
return ev_recommended_backends (); |
219 |
|
|
} |
220 |
|
|
|
221 |
|
|
inline unsigned int embeddable_backends () |
222 |
|
|
{ |
223 |
|
|
return ev_embeddable_backends (); |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
inline void set_allocator (void *(*cb)(void *ptr, long size)) |
227 |
|
|
{ |
228 |
|
|
ev_set_allocator (cb); |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
inline void set_syserr_cb (void (*cb)(const char *msg)) |
232 |
|
|
{ |
233 |
|
|
ev_set_syserr_cb (cb); |
234 |
|
|
} |
235 |
|
|
|
236 |
root |
1.1 |
|
237 |
|
|
inline ev_tstamp now (EV_P) |
238 |
|
|
{ |
239 |
|
|
return ev_now (EV_A); |
240 |
|
|
} |
241 |
|
|
|
242 |
|
|
#if EV_MULTIPLICITY |
243 |
root |
1.13 |
#define EV_CONSTRUCT \ |
244 |
|
|
(EV_P = EV_DEFAULT) \ |
245 |
root |
1.1 |
{ \ |
246 |
root |
1.13 |
set (EV_A); \ |
247 |
|
|
} |
248 |
root |
1.1 |
#else |
249 |
root |
1.13 |
#define EV_CONSTRUCT \ |
250 |
|
|
() \ |
251 |
|
|
{ \ |
252 |
|
|
} |
253 |
root |
1.1 |
#endif |
254 |
|
|
|
255 |
|
|
/* using a template here would require quite a bit more lines, |
256 |
|
|
* so a macro solution was chosen */ |
257 |
root |
1.4 |
#define EV_BEGIN_WATCHER(cppstem,cstem) \ |
258 |
root |
1.1 |
\ |
259 |
root |
1.13 |
struct cppstem : base<ev_ ## cstem, cppstem> \ |
260 |
root |
1.1 |
{ \ |
261 |
|
|
void start () \ |
262 |
|
|
{ \ |
263 |
|
|
ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
264 |
|
|
} \ |
265 |
|
|
\ |
266 |
|
|
void stop () \ |
267 |
|
|
{ \ |
268 |
|
|
ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ |
269 |
|
|
} \ |
270 |
|
|
\ |
271 |
root |
1.13 |
cppstem EV_CONSTRUCT \ |
272 |
root |
1.1 |
\ |
273 |
root |
1.3 |
~cppstem () \ |
274 |
|
|
{ \ |
275 |
|
|
stop (); \ |
276 |
|
|
} \ |
277 |
|
|
\ |
278 |
root |
1.13 |
using base<ev_ ## cstem, cppstem>::set; \ |
279 |
|
|
\ |
280 |
root |
1.1 |
private: \ |
281 |
|
|
\ |
282 |
llucax |
1.23 |
cppstem (const cppstem &o); \ |
283 |
root |
1.6 |
\ |
284 |
llucax |
1.23 |
cppstem & operator =(const cppstem &o); \ |
285 |
root |
1.1 |
\ |
286 |
|
|
public: |
287 |
|
|
|
288 |
root |
1.4 |
#define EV_END_WATCHER(cppstem,cstem) \ |
289 |
root |
1.6 |
}; |
290 |
root |
1.4 |
|
291 |
|
|
EV_BEGIN_WATCHER (io, io) |
292 |
root |
1.1 |
void set (int fd, int events) |
293 |
|
|
{ |
294 |
|
|
int active = is_active (); |
295 |
|
|
if (active) stop (); |
296 |
|
|
ev_io_set (static_cast<ev_io *>(this), fd, events); |
297 |
|
|
if (active) start (); |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
void set (int events) |
301 |
|
|
{ |
302 |
|
|
int active = is_active (); |
303 |
|
|
if (active) stop (); |
304 |
|
|
ev_io_set (static_cast<ev_io *>(this), fd, events); |
305 |
|
|
if (active) start (); |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
void start (int fd, int events) |
309 |
|
|
{ |
310 |
|
|
set (fd, events); |
311 |
|
|
start (); |
312 |
|
|
} |
313 |
root |
1.4 |
EV_END_WATCHER (io, io) |
314 |
root |
1.1 |
|
315 |
root |
1.4 |
EV_BEGIN_WATCHER (timer, timer) |
316 |
root |
1.1 |
void set (ev_tstamp after, ev_tstamp repeat = 0.) |
317 |
|
|
{ |
318 |
|
|
int active = is_active (); |
319 |
|
|
if (active) stop (); |
320 |
|
|
ev_timer_set (static_cast<ev_timer *>(this), after, repeat); |
321 |
|
|
if (active) start (); |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
void start (ev_tstamp after, ev_tstamp repeat = 0.) |
325 |
|
|
{ |
326 |
|
|
set (after, repeat); |
327 |
|
|
start (); |
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
void again () |
331 |
|
|
{ |
332 |
|
|
ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); |
333 |
|
|
} |
334 |
root |
1.4 |
EV_END_WATCHER (timer, timer) |
335 |
root |
1.1 |
|
336 |
root |
1.8 |
#if EV_PERIODIC_ENABLE |
337 |
root |
1.4 |
EV_BEGIN_WATCHER (periodic, periodic) |
338 |
root |
1.1 |
void set (ev_tstamp at, ev_tstamp interval = 0.) |
339 |
|
|
{ |
340 |
|
|
int active = is_active (); |
341 |
|
|
if (active) stop (); |
342 |
|
|
ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); |
343 |
|
|
if (active) start (); |
344 |
|
|
} |
345 |
|
|
|
346 |
|
|
void start (ev_tstamp at, ev_tstamp interval = 0.) |
347 |
|
|
{ |
348 |
|
|
set (at, interval); |
349 |
|
|
start (); |
350 |
|
|
} |
351 |
|
|
|
352 |
|
|
void again () |
353 |
|
|
{ |
354 |
|
|
ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); |
355 |
|
|
} |
356 |
root |
1.4 |
EV_END_WATCHER (periodic, periodic) |
357 |
root |
1.3 |
#endif |
358 |
root |
1.1 |
|
359 |
root |
1.4 |
EV_BEGIN_WATCHER (sig, signal) |
360 |
root |
1.1 |
void set (int signum) |
361 |
|
|
{ |
362 |
|
|
int active = is_active (); |
363 |
|
|
if (active) stop (); |
364 |
|
|
ev_signal_set (static_cast<ev_signal *>(this), signum); |
365 |
|
|
if (active) start (); |
366 |
|
|
} |
367 |
|
|
|
368 |
|
|
void start (int signum) |
369 |
|
|
{ |
370 |
|
|
set (signum); |
371 |
|
|
start (); |
372 |
|
|
} |
373 |
root |
1.4 |
EV_END_WATCHER (sig, signal) |
374 |
root |
1.1 |
|
375 |
root |
1.4 |
EV_BEGIN_WATCHER (child, child) |
376 |
root |
1.1 |
void set (int pid) |
377 |
|
|
{ |
378 |
|
|
int active = is_active (); |
379 |
|
|
if (active) stop (); |
380 |
|
|
ev_child_set (static_cast<ev_child *>(this), pid); |
381 |
|
|
if (active) start (); |
382 |
|
|
} |
383 |
|
|
|
384 |
|
|
void start (int pid) |
385 |
|
|
{ |
386 |
|
|
set (pid); |
387 |
|
|
start (); |
388 |
|
|
} |
389 |
root |
1.4 |
EV_END_WATCHER (child, child) |
390 |
root |
1.1 |
|
391 |
root |
1.8 |
#if EV_STAT_ENABLE |
392 |
|
|
EV_BEGIN_WATCHER (stat, stat) |
393 |
|
|
void set (const char *path, ev_tstamp interval = 0.) |
394 |
|
|
{ |
395 |
|
|
int active = is_active (); |
396 |
|
|
if (active) stop (); |
397 |
|
|
ev_stat_set (static_cast<ev_stat *>(this), path, interval); |
398 |
|
|
if (active) start (); |
399 |
|
|
} |
400 |
|
|
|
401 |
|
|
void start (const char *path, ev_tstamp interval = 0.) |
402 |
|
|
{ |
403 |
root |
1.12 |
stop (); |
404 |
root |
1.8 |
set (path, interval); |
405 |
|
|
start (); |
406 |
|
|
} |
407 |
|
|
|
408 |
|
|
void update () |
409 |
|
|
{ |
410 |
|
|
ev_stat_stat (EV_A_ static_cast<ev_stat *>(this)); |
411 |
|
|
} |
412 |
|
|
EV_END_WATCHER (stat, stat) |
413 |
|
|
#endif |
414 |
|
|
|
415 |
|
|
EV_BEGIN_WATCHER (idle, idle) |
416 |
|
|
void set () { } |
417 |
|
|
EV_END_WATCHER (idle, idle) |
418 |
root |
1.7 |
|
419 |
root |
1.8 |
EV_BEGIN_WATCHER (prepare, prepare) |
420 |
|
|
void set () { } |
421 |
|
|
EV_END_WATCHER (prepare, prepare) |
422 |
|
|
|
423 |
|
|
EV_BEGIN_WATCHER (check, check) |
424 |
|
|
void set () { } |
425 |
|
|
EV_END_WATCHER (check, check) |
426 |
|
|
|
427 |
|
|
#if EV_EMBED_ENABLE |
428 |
root |
1.7 |
EV_BEGIN_WATCHER (embed, embed) |
429 |
|
|
void start (struct ev_loop *embedded_loop) |
430 |
|
|
{ |
431 |
root |
1.12 |
stop (); |
432 |
|
|
ev_embed_set (static_cast<ev_embed *>(this), embedded_loop); |
433 |
root |
1.7 |
start (); |
434 |
|
|
} |
435 |
|
|
|
436 |
|
|
void sweep () |
437 |
|
|
{ |
438 |
|
|
ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); |
439 |
|
|
} |
440 |
|
|
EV_END_WATCHER (embed, embed) |
441 |
|
|
#endif |
442 |
|
|
|
443 |
root |
1.10 |
#if EV_FORK_ENABLE |
444 |
|
|
EV_BEGIN_WATCHER (fork, fork) |
445 |
|
|
void set () { } |
446 |
|
|
EV_END_WATCHER (fork, fork) |
447 |
|
|
#endif |
448 |
|
|
|
449 |
root |
1.1 |
#undef EV_CONSTRUCT |
450 |
root |
1.4 |
#undef EV_BEGIN_WATCHER |
451 |
root |
1.7 |
#undef EV_END_WATCHER |
452 |
root |
1.1 |
} |
453 |
|
|
|
454 |
|
|
#endif |
455 |
|
|
|