jpayne@69
|
1 /*
|
jpayne@69
|
2 * libev simple C++ wrapper classes
|
jpayne@69
|
3 *
|
jpayne@69
|
4 * Copyright (c) 2007,2008,2010,2018,2020 Marc Alexander Lehmann <libev@schmorp.de>
|
jpayne@69
|
5 * All rights reserved.
|
jpayne@69
|
6 *
|
jpayne@69
|
7 * Redistribution and use in source and binary forms, with or without modifica-
|
jpayne@69
|
8 * tion, are permitted provided that the following conditions are met:
|
jpayne@69
|
9 *
|
jpayne@69
|
10 * 1. Redistributions of source code must retain the above copyright notice,
|
jpayne@69
|
11 * this list of conditions and the following disclaimer.
|
jpayne@69
|
12 *
|
jpayne@69
|
13 * 2. Redistributions in binary form must reproduce the above copyright
|
jpayne@69
|
14 * notice, this list of conditions and the following disclaimer in the
|
jpayne@69
|
15 * documentation and/or other materials provided with the distribution.
|
jpayne@69
|
16 *
|
jpayne@69
|
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
jpayne@69
|
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
|
jpayne@69
|
19 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
jpayne@69
|
20 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
|
jpayne@69
|
21 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
jpayne@69
|
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
jpayne@69
|
23 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
jpayne@69
|
24 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
|
jpayne@69
|
25 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
jpayne@69
|
26 * OF THE POSSIBILITY OF SUCH DAMAGE.
|
jpayne@69
|
27 *
|
jpayne@69
|
28 * Alternatively, the contents of this file may be used under the terms of
|
jpayne@69
|
29 * the GNU General Public License ("GPL") version 2 or any later version,
|
jpayne@69
|
30 * in which case the provisions of the GPL are applicable instead of
|
jpayne@69
|
31 * the above. If you wish to allow the use of your version of this file
|
jpayne@69
|
32 * only under the terms of the GPL and not to allow others to use your
|
jpayne@69
|
33 * version of this file under the BSD license, indicate your decision
|
jpayne@69
|
34 * by deleting the provisions above and replace them with the notice
|
jpayne@69
|
35 * and other provisions required by the GPL. If you do not delete the
|
jpayne@69
|
36 * provisions above, a recipient may use your version of this file under
|
jpayne@69
|
37 * either the BSD or the GPL.
|
jpayne@69
|
38 */
|
jpayne@69
|
39
|
jpayne@69
|
40 #ifndef EVPP_H__
|
jpayne@69
|
41 #define EVPP_H__
|
jpayne@69
|
42
|
jpayne@69
|
43 #ifdef EV_H
|
jpayne@69
|
44 # include EV_H
|
jpayne@69
|
45 #else
|
jpayne@69
|
46 # include "ev.h"
|
jpayne@69
|
47 #endif
|
jpayne@69
|
48
|
jpayne@69
|
49 #ifndef EV_USE_STDEXCEPT
|
jpayne@69
|
50 # define EV_USE_STDEXCEPT 1
|
jpayne@69
|
51 #endif
|
jpayne@69
|
52
|
jpayne@69
|
53 #if EV_USE_STDEXCEPT
|
jpayne@69
|
54 # include <stdexcept>
|
jpayne@69
|
55 #endif
|
jpayne@69
|
56
|
jpayne@69
|
57 namespace ev {
|
jpayne@69
|
58
|
jpayne@69
|
59 typedef ev_tstamp tstamp;
|
jpayne@69
|
60
|
jpayne@69
|
61 enum {
|
jpayne@69
|
62 UNDEF = EV_UNDEF,
|
jpayne@69
|
63 NONE = EV_NONE,
|
jpayne@69
|
64 READ = EV_READ,
|
jpayne@69
|
65 WRITE = EV_WRITE,
|
jpayne@69
|
66 #if EV_COMPAT3
|
jpayne@69
|
67 TIMEOUT = EV_TIMEOUT,
|
jpayne@69
|
68 #endif
|
jpayne@69
|
69 TIMER = EV_TIMER,
|
jpayne@69
|
70 PERIODIC = EV_PERIODIC,
|
jpayne@69
|
71 SIGNAL = EV_SIGNAL,
|
jpayne@69
|
72 CHILD = EV_CHILD,
|
jpayne@69
|
73 STAT = EV_STAT,
|
jpayne@69
|
74 IDLE = EV_IDLE,
|
jpayne@69
|
75 CHECK = EV_CHECK,
|
jpayne@69
|
76 PREPARE = EV_PREPARE,
|
jpayne@69
|
77 FORK = EV_FORK,
|
jpayne@69
|
78 ASYNC = EV_ASYNC,
|
jpayne@69
|
79 EMBED = EV_EMBED,
|
jpayne@69
|
80 # undef ERROR // some systems stupidly #define ERROR
|
jpayne@69
|
81 ERROR = EV_ERROR
|
jpayne@69
|
82 };
|
jpayne@69
|
83
|
jpayne@69
|
84 enum
|
jpayne@69
|
85 {
|
jpayne@69
|
86 AUTO = EVFLAG_AUTO,
|
jpayne@69
|
87 NOENV = EVFLAG_NOENV,
|
jpayne@69
|
88 FORKCHECK = EVFLAG_FORKCHECK,
|
jpayne@69
|
89
|
jpayne@69
|
90 SELECT = EVBACKEND_SELECT,
|
jpayne@69
|
91 POLL = EVBACKEND_POLL,
|
jpayne@69
|
92 EPOLL = EVBACKEND_EPOLL,
|
jpayne@69
|
93 KQUEUE = EVBACKEND_KQUEUE,
|
jpayne@69
|
94 DEVPOLL = EVBACKEND_DEVPOLL,
|
jpayne@69
|
95 PORT = EVBACKEND_PORT
|
jpayne@69
|
96 };
|
jpayne@69
|
97
|
jpayne@69
|
98 enum
|
jpayne@69
|
99 {
|
jpayne@69
|
100 #if EV_COMPAT3
|
jpayne@69
|
101 NONBLOCK = EVLOOP_NONBLOCK,
|
jpayne@69
|
102 ONESHOT = EVLOOP_ONESHOT,
|
jpayne@69
|
103 #endif
|
jpayne@69
|
104 NOWAIT = EVRUN_NOWAIT,
|
jpayne@69
|
105 ONCE = EVRUN_ONCE
|
jpayne@69
|
106 };
|
jpayne@69
|
107
|
jpayne@69
|
108 enum how_t
|
jpayne@69
|
109 {
|
jpayne@69
|
110 ONE = EVBREAK_ONE,
|
jpayne@69
|
111 ALL = EVBREAK_ALL
|
jpayne@69
|
112 };
|
jpayne@69
|
113
|
jpayne@69
|
114 struct bad_loop
|
jpayne@69
|
115 #if EV_USE_STDEXCEPT
|
jpayne@69
|
116 : std::exception
|
jpayne@69
|
117 #endif
|
jpayne@69
|
118 {
|
jpayne@69
|
119 #if EV_USE_STDEXCEPT
|
jpayne@69
|
120 const char *what () const EV_NOEXCEPT
|
jpayne@69
|
121 {
|
jpayne@69
|
122 return "libev event loop cannot be initialized, bad value of LIBEV_FLAGS?";
|
jpayne@69
|
123 }
|
jpayne@69
|
124 #endif
|
jpayne@69
|
125 };
|
jpayne@69
|
126
|
jpayne@69
|
127 #ifdef EV_AX
|
jpayne@69
|
128 # undef EV_AX
|
jpayne@69
|
129 #endif
|
jpayne@69
|
130
|
jpayne@69
|
131 #ifdef EV_AX_
|
jpayne@69
|
132 # undef EV_AX_
|
jpayne@69
|
133 #endif
|
jpayne@69
|
134
|
jpayne@69
|
135 #if EV_MULTIPLICITY
|
jpayne@69
|
136 # define EV_AX raw_loop
|
jpayne@69
|
137 # define EV_AX_ raw_loop,
|
jpayne@69
|
138 #else
|
jpayne@69
|
139 # define EV_AX
|
jpayne@69
|
140 # define EV_AX_
|
jpayne@69
|
141 #endif
|
jpayne@69
|
142
|
jpayne@69
|
143 struct loop_ref
|
jpayne@69
|
144 {
|
jpayne@69
|
145 loop_ref (EV_P) EV_NOEXCEPT
|
jpayne@69
|
146 #if EV_MULTIPLICITY
|
jpayne@69
|
147 : EV_AX (EV_A)
|
jpayne@69
|
148 #endif
|
jpayne@69
|
149 {
|
jpayne@69
|
150 }
|
jpayne@69
|
151
|
jpayne@69
|
152 bool operator == (const loop_ref &other) const EV_NOEXCEPT
|
jpayne@69
|
153 {
|
jpayne@69
|
154 #if EV_MULTIPLICITY
|
jpayne@69
|
155 return EV_AX == other.EV_AX;
|
jpayne@69
|
156 #else
|
jpayne@69
|
157 return true;
|
jpayne@69
|
158 #endif
|
jpayne@69
|
159 }
|
jpayne@69
|
160
|
jpayne@69
|
161 bool operator != (const loop_ref &other) const EV_NOEXCEPT
|
jpayne@69
|
162 {
|
jpayne@69
|
163 #if EV_MULTIPLICITY
|
jpayne@69
|
164 return ! (*this == other);
|
jpayne@69
|
165 #else
|
jpayne@69
|
166 return false;
|
jpayne@69
|
167 #endif
|
jpayne@69
|
168 }
|
jpayne@69
|
169
|
jpayne@69
|
170 #if EV_MULTIPLICITY
|
jpayne@69
|
171 bool operator == (const EV_P) const EV_NOEXCEPT
|
jpayne@69
|
172 {
|
jpayne@69
|
173 return this->EV_AX == EV_A;
|
jpayne@69
|
174 }
|
jpayne@69
|
175
|
jpayne@69
|
176 bool operator != (const EV_P) const EV_NOEXCEPT
|
jpayne@69
|
177 {
|
jpayne@69
|
178 return ! (*this == EV_A);
|
jpayne@69
|
179 }
|
jpayne@69
|
180
|
jpayne@69
|
181 operator struct ev_loop * () const EV_NOEXCEPT
|
jpayne@69
|
182 {
|
jpayne@69
|
183 return EV_AX;
|
jpayne@69
|
184 }
|
jpayne@69
|
185
|
jpayne@69
|
186 operator const struct ev_loop * () const EV_NOEXCEPT
|
jpayne@69
|
187 {
|
jpayne@69
|
188 return EV_AX;
|
jpayne@69
|
189 }
|
jpayne@69
|
190
|
jpayne@69
|
191 bool is_default () const EV_NOEXCEPT
|
jpayne@69
|
192 {
|
jpayne@69
|
193 return EV_AX == ev_default_loop (0);
|
jpayne@69
|
194 }
|
jpayne@69
|
195 #endif
|
jpayne@69
|
196
|
jpayne@69
|
197 #if EV_COMPAT3
|
jpayne@69
|
198 void loop (int flags = 0)
|
jpayne@69
|
199 {
|
jpayne@69
|
200 ev_run (EV_AX_ flags);
|
jpayne@69
|
201 }
|
jpayne@69
|
202
|
jpayne@69
|
203 void unloop (how_t how = ONE) EV_NOEXCEPT
|
jpayne@69
|
204 {
|
jpayne@69
|
205 ev_break (EV_AX_ how);
|
jpayne@69
|
206 }
|
jpayne@69
|
207 #endif
|
jpayne@69
|
208
|
jpayne@69
|
209 void run (int flags = 0)
|
jpayne@69
|
210 {
|
jpayne@69
|
211 ev_run (EV_AX_ flags);
|
jpayne@69
|
212 }
|
jpayne@69
|
213
|
jpayne@69
|
214 void break_loop (how_t how = ONE) EV_NOEXCEPT
|
jpayne@69
|
215 {
|
jpayne@69
|
216 ev_break (EV_AX_ how);
|
jpayne@69
|
217 }
|
jpayne@69
|
218
|
jpayne@69
|
219 void post_fork () EV_NOEXCEPT
|
jpayne@69
|
220 {
|
jpayne@69
|
221 ev_loop_fork (EV_AX);
|
jpayne@69
|
222 }
|
jpayne@69
|
223
|
jpayne@69
|
224 unsigned int backend () const EV_NOEXCEPT
|
jpayne@69
|
225 {
|
jpayne@69
|
226 return ev_backend (EV_AX);
|
jpayne@69
|
227 }
|
jpayne@69
|
228
|
jpayne@69
|
229 tstamp now () const EV_NOEXCEPT
|
jpayne@69
|
230 {
|
jpayne@69
|
231 return ev_now (EV_AX);
|
jpayne@69
|
232 }
|
jpayne@69
|
233
|
jpayne@69
|
234 void ref () EV_NOEXCEPT
|
jpayne@69
|
235 {
|
jpayne@69
|
236 ev_ref (EV_AX);
|
jpayne@69
|
237 }
|
jpayne@69
|
238
|
jpayne@69
|
239 void unref () EV_NOEXCEPT
|
jpayne@69
|
240 {
|
jpayne@69
|
241 ev_unref (EV_AX);
|
jpayne@69
|
242 }
|
jpayne@69
|
243
|
jpayne@69
|
244 #if EV_FEATURE_API
|
jpayne@69
|
245 unsigned int iteration () const EV_NOEXCEPT
|
jpayne@69
|
246 {
|
jpayne@69
|
247 return ev_iteration (EV_AX);
|
jpayne@69
|
248 }
|
jpayne@69
|
249
|
jpayne@69
|
250 unsigned int depth () const EV_NOEXCEPT
|
jpayne@69
|
251 {
|
jpayne@69
|
252 return ev_depth (EV_AX);
|
jpayne@69
|
253 }
|
jpayne@69
|
254
|
jpayne@69
|
255 void set_io_collect_interval (tstamp interval) EV_NOEXCEPT
|
jpayne@69
|
256 {
|
jpayne@69
|
257 ev_set_io_collect_interval (EV_AX_ interval);
|
jpayne@69
|
258 }
|
jpayne@69
|
259
|
jpayne@69
|
260 void set_timeout_collect_interval (tstamp interval) EV_NOEXCEPT
|
jpayne@69
|
261 {
|
jpayne@69
|
262 ev_set_timeout_collect_interval (EV_AX_ interval);
|
jpayne@69
|
263 }
|
jpayne@69
|
264 #endif
|
jpayne@69
|
265
|
jpayne@69
|
266 // function callback
|
jpayne@69
|
267 void once (int fd, int events, tstamp timeout, void (*cb)(int, void *), void *arg = 0) EV_NOEXCEPT
|
jpayne@69
|
268 {
|
jpayne@69
|
269 ev_once (EV_AX_ fd, events, timeout, cb, arg);
|
jpayne@69
|
270 }
|
jpayne@69
|
271
|
jpayne@69
|
272 // method callback
|
jpayne@69
|
273 template<class K, void (K::*method)(int)>
|
jpayne@69
|
274 void once (int fd, int events, tstamp timeout, K *object) EV_NOEXCEPT
|
jpayne@69
|
275 {
|
jpayne@69
|
276 once (fd, events, timeout, method_thunk<K, method>, object);
|
jpayne@69
|
277 }
|
jpayne@69
|
278
|
jpayne@69
|
279 // default method == operator ()
|
jpayne@69
|
280 template<class K>
|
jpayne@69
|
281 void once (int fd, int events, tstamp timeout, K *object) EV_NOEXCEPT
|
jpayne@69
|
282 {
|
jpayne@69
|
283 once (fd, events, timeout, method_thunk<K, &K::operator ()>, object);
|
jpayne@69
|
284 }
|
jpayne@69
|
285
|
jpayne@69
|
286 template<class K, void (K::*method)(int)>
|
jpayne@69
|
287 static void method_thunk (int revents, void *arg)
|
jpayne@69
|
288 {
|
jpayne@69
|
289 (static_cast<K *>(arg)->*method)
|
jpayne@69
|
290 (revents);
|
jpayne@69
|
291 }
|
jpayne@69
|
292
|
jpayne@69
|
293 // no-argument method callback
|
jpayne@69
|
294 template<class K, void (K::*method)()>
|
jpayne@69
|
295 void once (int fd, int events, tstamp timeout, K *object) EV_NOEXCEPT
|
jpayne@69
|
296 {
|
jpayne@69
|
297 once (fd, events, timeout, method_noargs_thunk<K, method>, object);
|
jpayne@69
|
298 }
|
jpayne@69
|
299
|
jpayne@69
|
300 template<class K, void (K::*method)()>
|
jpayne@69
|
301 static void method_noargs_thunk (int revents, void *arg)
|
jpayne@69
|
302 {
|
jpayne@69
|
303 (static_cast<K *>(arg)->*method)
|
jpayne@69
|
304 ();
|
jpayne@69
|
305 }
|
jpayne@69
|
306
|
jpayne@69
|
307 // simpler function callback
|
jpayne@69
|
308 template<void (*cb)(int)>
|
jpayne@69
|
309 void once (int fd, int events, tstamp timeout) EV_NOEXCEPT
|
jpayne@69
|
310 {
|
jpayne@69
|
311 once (fd, events, timeout, simpler_func_thunk<cb>);
|
jpayne@69
|
312 }
|
jpayne@69
|
313
|
jpayne@69
|
314 template<void (*cb)(int)>
|
jpayne@69
|
315 static void simpler_func_thunk (int revents, void *arg)
|
jpayne@69
|
316 {
|
jpayne@69
|
317 (*cb)
|
jpayne@69
|
318 (revents);
|
jpayne@69
|
319 }
|
jpayne@69
|
320
|
jpayne@69
|
321 // simplest function callback
|
jpayne@69
|
322 template<void (*cb)()>
|
jpayne@69
|
323 void once (int fd, int events, tstamp timeout) EV_NOEXCEPT
|
jpayne@69
|
324 {
|
jpayne@69
|
325 once (fd, events, timeout, simplest_func_thunk<cb>);
|
jpayne@69
|
326 }
|
jpayne@69
|
327
|
jpayne@69
|
328 template<void (*cb)()>
|
jpayne@69
|
329 static void simplest_func_thunk (int revents, void *arg)
|
jpayne@69
|
330 {
|
jpayne@69
|
331 (*cb)
|
jpayne@69
|
332 ();
|
jpayne@69
|
333 }
|
jpayne@69
|
334
|
jpayne@69
|
335 void feed_fd_event (int fd, int revents) EV_NOEXCEPT
|
jpayne@69
|
336 {
|
jpayne@69
|
337 ev_feed_fd_event (EV_AX_ fd, revents);
|
jpayne@69
|
338 }
|
jpayne@69
|
339
|
jpayne@69
|
340 void feed_signal_event (int signum) EV_NOEXCEPT
|
jpayne@69
|
341 {
|
jpayne@69
|
342 ev_feed_signal_event (EV_AX_ signum);
|
jpayne@69
|
343 }
|
jpayne@69
|
344
|
jpayne@69
|
345 #if EV_MULTIPLICITY
|
jpayne@69
|
346 struct ev_loop* EV_AX;
|
jpayne@69
|
347 #endif
|
jpayne@69
|
348
|
jpayne@69
|
349 };
|
jpayne@69
|
350
|
jpayne@69
|
351 #if EV_MULTIPLICITY
|
jpayne@69
|
352 struct dynamic_loop : loop_ref
|
jpayne@69
|
353 {
|
jpayne@69
|
354
|
jpayne@69
|
355 dynamic_loop (unsigned int flags = AUTO)
|
jpayne@69
|
356 : loop_ref (ev_loop_new (flags))
|
jpayne@69
|
357 {
|
jpayne@69
|
358 if (!EV_AX)
|
jpayne@69
|
359 throw bad_loop ();
|
jpayne@69
|
360 }
|
jpayne@69
|
361
|
jpayne@69
|
362 ~dynamic_loop () EV_NOEXCEPT
|
jpayne@69
|
363 {
|
jpayne@69
|
364 ev_loop_destroy (EV_AX);
|
jpayne@69
|
365 EV_AX = 0;
|
jpayne@69
|
366 }
|
jpayne@69
|
367
|
jpayne@69
|
368 private:
|
jpayne@69
|
369
|
jpayne@69
|
370 dynamic_loop (const dynamic_loop &);
|
jpayne@69
|
371
|
jpayne@69
|
372 dynamic_loop & operator= (const dynamic_loop &);
|
jpayne@69
|
373
|
jpayne@69
|
374 };
|
jpayne@69
|
375 #endif
|
jpayne@69
|
376
|
jpayne@69
|
377 struct default_loop : loop_ref
|
jpayne@69
|
378 {
|
jpayne@69
|
379 default_loop (unsigned int flags = AUTO)
|
jpayne@69
|
380 #if EV_MULTIPLICITY
|
jpayne@69
|
381 : loop_ref (ev_default_loop (flags))
|
jpayne@69
|
382 #endif
|
jpayne@69
|
383 {
|
jpayne@69
|
384 if (
|
jpayne@69
|
385 #if EV_MULTIPLICITY
|
jpayne@69
|
386 !EV_AX
|
jpayne@69
|
387 #else
|
jpayne@69
|
388 !ev_default_loop (flags)
|
jpayne@69
|
389 #endif
|
jpayne@69
|
390 )
|
jpayne@69
|
391 throw bad_loop ();
|
jpayne@69
|
392 }
|
jpayne@69
|
393
|
jpayne@69
|
394 private:
|
jpayne@69
|
395 default_loop (const default_loop &);
|
jpayne@69
|
396 default_loop &operator = (const default_loop &);
|
jpayne@69
|
397 };
|
jpayne@69
|
398
|
jpayne@69
|
399 inline loop_ref get_default_loop () EV_NOEXCEPT
|
jpayne@69
|
400 {
|
jpayne@69
|
401 #if EV_MULTIPLICITY
|
jpayne@69
|
402 return ev_default_loop (0);
|
jpayne@69
|
403 #else
|
jpayne@69
|
404 return loop_ref ();
|
jpayne@69
|
405 #endif
|
jpayne@69
|
406 }
|
jpayne@69
|
407
|
jpayne@69
|
408 #undef EV_AX
|
jpayne@69
|
409 #undef EV_AX_
|
jpayne@69
|
410
|
jpayne@69
|
411 #undef EV_PX
|
jpayne@69
|
412 #undef EV_PX_
|
jpayne@69
|
413 #if EV_MULTIPLICITY
|
jpayne@69
|
414 # define EV_PX loop_ref EV_A
|
jpayne@69
|
415 # define EV_PX_ loop_ref EV_A_
|
jpayne@69
|
416 #else
|
jpayne@69
|
417 # define EV_PX
|
jpayne@69
|
418 # define EV_PX_
|
jpayne@69
|
419 #endif
|
jpayne@69
|
420
|
jpayne@69
|
421 template<class ev_watcher, class watcher>
|
jpayne@69
|
422 struct base : ev_watcher
|
jpayne@69
|
423 {
|
jpayne@69
|
424 // scoped pause/unpause of a watcher
|
jpayne@69
|
425 struct freeze_guard
|
jpayne@69
|
426 {
|
jpayne@69
|
427 watcher &w;
|
jpayne@69
|
428 bool active;
|
jpayne@69
|
429
|
jpayne@69
|
430 freeze_guard (watcher *self) EV_NOEXCEPT
|
jpayne@69
|
431 : w (*self), active (w.is_active ())
|
jpayne@69
|
432 {
|
jpayne@69
|
433 if (active) w.stop ();
|
jpayne@69
|
434 }
|
jpayne@69
|
435
|
jpayne@69
|
436 ~freeze_guard ()
|
jpayne@69
|
437 {
|
jpayne@69
|
438 if (active) w.start ();
|
jpayne@69
|
439 }
|
jpayne@69
|
440 };
|
jpayne@69
|
441
|
jpayne@69
|
442 #if EV_MULTIPLICITY
|
jpayne@69
|
443 EV_PX;
|
jpayne@69
|
444
|
jpayne@69
|
445 // loop set
|
jpayne@69
|
446 void set (EV_P) EV_NOEXCEPT
|
jpayne@69
|
447 {
|
jpayne@69
|
448 this->EV_A = EV_A;
|
jpayne@69
|
449 }
|
jpayne@69
|
450 #endif
|
jpayne@69
|
451
|
jpayne@69
|
452 base (EV_PX) EV_NOEXCEPT
|
jpayne@69
|
453 #if EV_MULTIPLICITY
|
jpayne@69
|
454 : EV_A (EV_A)
|
jpayne@69
|
455 #endif
|
jpayne@69
|
456 {
|
jpayne@69
|
457 ev_init (this, 0);
|
jpayne@69
|
458 }
|
jpayne@69
|
459
|
jpayne@69
|
460 void set_ (const void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) EV_NOEXCEPT
|
jpayne@69
|
461 {
|
jpayne@69
|
462 this->data = (void *)data;
|
jpayne@69
|
463 ev_set_cb (static_cast<ev_watcher *>(this), cb);
|
jpayne@69
|
464 }
|
jpayne@69
|
465
|
jpayne@69
|
466 // function callback
|
jpayne@69
|
467 template<void (*function)(watcher &w, int)>
|
jpayne@69
|
468 void set (void *data = 0) EV_NOEXCEPT
|
jpayne@69
|
469 {
|
jpayne@69
|
470 set_ (data, function_thunk<function>);
|
jpayne@69
|
471 }
|
jpayne@69
|
472
|
jpayne@69
|
473 template<void (*function)(watcher &w, int)>
|
jpayne@69
|
474 static void function_thunk (EV_P_ ev_watcher *w, int revents)
|
jpayne@69
|
475 {
|
jpayne@69
|
476 function
|
jpayne@69
|
477 (*static_cast<watcher *>(w), revents);
|
jpayne@69
|
478 }
|
jpayne@69
|
479
|
jpayne@69
|
480 // method callback
|
jpayne@69
|
481 template<class K, void (K::*method)(watcher &w, int)>
|
jpayne@69
|
482 void set (K *object) EV_NOEXCEPT
|
jpayne@69
|
483 {
|
jpayne@69
|
484 set_ (object, method_thunk<K, method>);
|
jpayne@69
|
485 }
|
jpayne@69
|
486
|
jpayne@69
|
487 // default method == operator ()
|
jpayne@69
|
488 template<class K>
|
jpayne@69
|
489 void set (K *object) EV_NOEXCEPT
|
jpayne@69
|
490 {
|
jpayne@69
|
491 set_ (object, method_thunk<K, &K::operator ()>);
|
jpayne@69
|
492 }
|
jpayne@69
|
493
|
jpayne@69
|
494 template<class K, void (K::*method)(watcher &w, int)>
|
jpayne@69
|
495 static void method_thunk (EV_P_ ev_watcher *w, int revents)
|
jpayne@69
|
496 {
|
jpayne@69
|
497 (static_cast<K *>(w->data)->*method)
|
jpayne@69
|
498 (*static_cast<watcher *>(w), revents);
|
jpayne@69
|
499 }
|
jpayne@69
|
500
|
jpayne@69
|
501 // no-argument callback
|
jpayne@69
|
502 template<class K, void (K::*method)()>
|
jpayne@69
|
503 void set (K *object) EV_NOEXCEPT
|
jpayne@69
|
504 {
|
jpayne@69
|
505 set_ (object, method_noargs_thunk<K, method>);
|
jpayne@69
|
506 }
|
jpayne@69
|
507
|
jpayne@69
|
508 template<class K, void (K::*method)()>
|
jpayne@69
|
509 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
|
jpayne@69
|
510 {
|
jpayne@69
|
511 (static_cast<K *>(w->data)->*method)
|
jpayne@69
|
512 ();
|
jpayne@69
|
513 }
|
jpayne@69
|
514
|
jpayne@69
|
515 void operator ()(int events = EV_UNDEF)
|
jpayne@69
|
516 {
|
jpayne@69
|
517 return
|
jpayne@69
|
518 ev_cb (static_cast<ev_watcher *>(this))
|
jpayne@69
|
519 (static_cast<ev_watcher *>(this), events);
|
jpayne@69
|
520 }
|
jpayne@69
|
521
|
jpayne@69
|
522 bool is_active () const EV_NOEXCEPT
|
jpayne@69
|
523 {
|
jpayne@69
|
524 return ev_is_active (static_cast<const ev_watcher *>(this));
|
jpayne@69
|
525 }
|
jpayne@69
|
526
|
jpayne@69
|
527 bool is_pending () const EV_NOEXCEPT
|
jpayne@69
|
528 {
|
jpayne@69
|
529 return ev_is_pending (static_cast<const ev_watcher *>(this));
|
jpayne@69
|
530 }
|
jpayne@69
|
531
|
jpayne@69
|
532 void feed_event (int revents) EV_NOEXCEPT
|
jpayne@69
|
533 {
|
jpayne@69
|
534 ev_feed_event (EV_A_ static_cast<ev_watcher *>(this), revents);
|
jpayne@69
|
535 }
|
jpayne@69
|
536 };
|
jpayne@69
|
537
|
jpayne@69
|
538 inline tstamp now (EV_P) EV_NOEXCEPT
|
jpayne@69
|
539 {
|
jpayne@69
|
540 return ev_now (EV_A);
|
jpayne@69
|
541 }
|
jpayne@69
|
542
|
jpayne@69
|
543 inline void delay (tstamp interval) EV_NOEXCEPT
|
jpayne@69
|
544 {
|
jpayne@69
|
545 ev_sleep (interval);
|
jpayne@69
|
546 }
|
jpayne@69
|
547
|
jpayne@69
|
548 inline int version_major () EV_NOEXCEPT
|
jpayne@69
|
549 {
|
jpayne@69
|
550 return ev_version_major ();
|
jpayne@69
|
551 }
|
jpayne@69
|
552
|
jpayne@69
|
553 inline int version_minor () EV_NOEXCEPT
|
jpayne@69
|
554 {
|
jpayne@69
|
555 return ev_version_minor ();
|
jpayne@69
|
556 }
|
jpayne@69
|
557
|
jpayne@69
|
558 inline unsigned int supported_backends () EV_NOEXCEPT
|
jpayne@69
|
559 {
|
jpayne@69
|
560 return ev_supported_backends ();
|
jpayne@69
|
561 }
|
jpayne@69
|
562
|
jpayne@69
|
563 inline unsigned int recommended_backends () EV_NOEXCEPT
|
jpayne@69
|
564 {
|
jpayne@69
|
565 return ev_recommended_backends ();
|
jpayne@69
|
566 }
|
jpayne@69
|
567
|
jpayne@69
|
568 inline unsigned int embeddable_backends () EV_NOEXCEPT
|
jpayne@69
|
569 {
|
jpayne@69
|
570 return ev_embeddable_backends ();
|
jpayne@69
|
571 }
|
jpayne@69
|
572
|
jpayne@69
|
573 inline void set_allocator (void *(*cb)(void *ptr, long size) EV_NOEXCEPT) EV_NOEXCEPT
|
jpayne@69
|
574 {
|
jpayne@69
|
575 ev_set_allocator (cb);
|
jpayne@69
|
576 }
|
jpayne@69
|
577
|
jpayne@69
|
578 inline void set_syserr_cb (void (*cb)(const char *msg) EV_NOEXCEPT) EV_NOEXCEPT
|
jpayne@69
|
579 {
|
jpayne@69
|
580 ev_set_syserr_cb (cb);
|
jpayne@69
|
581 }
|
jpayne@69
|
582
|
jpayne@69
|
583 #if EV_MULTIPLICITY
|
jpayne@69
|
584 #define EV_CONSTRUCT(cppstem,cstem) \
|
jpayne@69
|
585 (EV_PX = get_default_loop ()) EV_NOEXCEPT \
|
jpayne@69
|
586 : base<ev_ ## cstem, cppstem> (EV_A) \
|
jpayne@69
|
587 { \
|
jpayne@69
|
588 }
|
jpayne@69
|
589 #else
|
jpayne@69
|
590 #define EV_CONSTRUCT(cppstem,cstem) \
|
jpayne@69
|
591 () EV_NOEXCEPT \
|
jpayne@69
|
592 { \
|
jpayne@69
|
593 }
|
jpayne@69
|
594 #endif
|
jpayne@69
|
595
|
jpayne@69
|
596 /* using a template here would require quite a few more lines,
|
jpayne@69
|
597 * so a macro solution was chosen */
|
jpayne@69
|
598 #define EV_BEGIN_WATCHER(cppstem,cstem) \
|
jpayne@69
|
599 \
|
jpayne@69
|
600 struct cppstem : base<ev_ ## cstem, cppstem> \
|
jpayne@69
|
601 { \
|
jpayne@69
|
602 void start () EV_NOEXCEPT \
|
jpayne@69
|
603 { \
|
jpayne@69
|
604 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
|
jpayne@69
|
605 } \
|
jpayne@69
|
606 \
|
jpayne@69
|
607 void stop () EV_NOEXCEPT \
|
jpayne@69
|
608 { \
|
jpayne@69
|
609 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
|
jpayne@69
|
610 } \
|
jpayne@69
|
611 \
|
jpayne@69
|
612 cppstem EV_CONSTRUCT(cppstem,cstem) \
|
jpayne@69
|
613 \
|
jpayne@69
|
614 ~cppstem () EV_NOEXCEPT \
|
jpayne@69
|
615 { \
|
jpayne@69
|
616 stop (); \
|
jpayne@69
|
617 } \
|
jpayne@69
|
618 \
|
jpayne@69
|
619 using base<ev_ ## cstem, cppstem>::set; \
|
jpayne@69
|
620 \
|
jpayne@69
|
621 private: \
|
jpayne@69
|
622 \
|
jpayne@69
|
623 cppstem (const cppstem &o); \
|
jpayne@69
|
624 \
|
jpayne@69
|
625 cppstem &operator =(const cppstem &o); \
|
jpayne@69
|
626 \
|
jpayne@69
|
627 public:
|
jpayne@69
|
628
|
jpayne@69
|
629 #define EV_END_WATCHER(cppstem,cstem) \
|
jpayne@69
|
630 };
|
jpayne@69
|
631
|
jpayne@69
|
632 EV_BEGIN_WATCHER (io, io)
|
jpayne@69
|
633 void set (int fd, int events) EV_NOEXCEPT
|
jpayne@69
|
634 {
|
jpayne@69
|
635 freeze_guard freeze (this);
|
jpayne@69
|
636 ev_io_set (static_cast<ev_io *>(this), fd, events);
|
jpayne@69
|
637 }
|
jpayne@69
|
638
|
jpayne@69
|
639 void set (int events) EV_NOEXCEPT
|
jpayne@69
|
640 {
|
jpayne@69
|
641 freeze_guard freeze (this);
|
jpayne@69
|
642 ev_io_modify (static_cast<ev_io *>(this), events);
|
jpayne@69
|
643 }
|
jpayne@69
|
644
|
jpayne@69
|
645 void start (int fd, int events) EV_NOEXCEPT
|
jpayne@69
|
646 {
|
jpayne@69
|
647 set (fd, events);
|
jpayne@69
|
648 start ();
|
jpayne@69
|
649 }
|
jpayne@69
|
650 EV_END_WATCHER (io, io)
|
jpayne@69
|
651
|
jpayne@69
|
652 EV_BEGIN_WATCHER (timer, timer)
|
jpayne@69
|
653 void set (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT
|
jpayne@69
|
654 {
|
jpayne@69
|
655 freeze_guard freeze (this);
|
jpayne@69
|
656 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
|
jpayne@69
|
657 }
|
jpayne@69
|
658
|
jpayne@69
|
659 void start (ev_tstamp after, ev_tstamp repeat = 0.) EV_NOEXCEPT
|
jpayne@69
|
660 {
|
jpayne@69
|
661 set (after, repeat);
|
jpayne@69
|
662 start ();
|
jpayne@69
|
663 }
|
jpayne@69
|
664
|
jpayne@69
|
665 void again () EV_NOEXCEPT
|
jpayne@69
|
666 {
|
jpayne@69
|
667 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
|
jpayne@69
|
668 }
|
jpayne@69
|
669
|
jpayne@69
|
670 ev_tstamp remaining ()
|
jpayne@69
|
671 {
|
jpayne@69
|
672 return ev_timer_remaining (EV_A_ static_cast<ev_timer *>(this));
|
jpayne@69
|
673 }
|
jpayne@69
|
674 EV_END_WATCHER (timer, timer)
|
jpayne@69
|
675
|
jpayne@69
|
676 #if EV_PERIODIC_ENABLE
|
jpayne@69
|
677 EV_BEGIN_WATCHER (periodic, periodic)
|
jpayne@69
|
678 void set (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT
|
jpayne@69
|
679 {
|
jpayne@69
|
680 freeze_guard freeze (this);
|
jpayne@69
|
681 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
|
jpayne@69
|
682 }
|
jpayne@69
|
683
|
jpayne@69
|
684 void start (ev_tstamp at, ev_tstamp interval = 0.) EV_NOEXCEPT
|
jpayne@69
|
685 {
|
jpayne@69
|
686 set (at, interval);
|
jpayne@69
|
687 start ();
|
jpayne@69
|
688 }
|
jpayne@69
|
689
|
jpayne@69
|
690 void again () EV_NOEXCEPT
|
jpayne@69
|
691 {
|
jpayne@69
|
692 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
|
jpayne@69
|
693 }
|
jpayne@69
|
694 EV_END_WATCHER (periodic, periodic)
|
jpayne@69
|
695 #endif
|
jpayne@69
|
696
|
jpayne@69
|
697 #if EV_SIGNAL_ENABLE
|
jpayne@69
|
698 EV_BEGIN_WATCHER (sig, signal)
|
jpayne@69
|
699 void set (int signum) EV_NOEXCEPT
|
jpayne@69
|
700 {
|
jpayne@69
|
701 freeze_guard freeze (this);
|
jpayne@69
|
702 ev_signal_set (static_cast<ev_signal *>(this), signum);
|
jpayne@69
|
703 }
|
jpayne@69
|
704
|
jpayne@69
|
705 void start (int signum) EV_NOEXCEPT
|
jpayne@69
|
706 {
|
jpayne@69
|
707 set (signum);
|
jpayne@69
|
708 start ();
|
jpayne@69
|
709 }
|
jpayne@69
|
710 EV_END_WATCHER (sig, signal)
|
jpayne@69
|
711 #endif
|
jpayne@69
|
712
|
jpayne@69
|
713 #if EV_CHILD_ENABLE
|
jpayne@69
|
714 EV_BEGIN_WATCHER (child, child)
|
jpayne@69
|
715 void set (int pid, int trace = 0) EV_NOEXCEPT
|
jpayne@69
|
716 {
|
jpayne@69
|
717 freeze_guard freeze (this);
|
jpayne@69
|
718 ev_child_set (static_cast<ev_child *>(this), pid, trace);
|
jpayne@69
|
719 }
|
jpayne@69
|
720
|
jpayne@69
|
721 void start (int pid, int trace = 0) EV_NOEXCEPT
|
jpayne@69
|
722 {
|
jpayne@69
|
723 set (pid, trace);
|
jpayne@69
|
724 start ();
|
jpayne@69
|
725 }
|
jpayne@69
|
726 EV_END_WATCHER (child, child)
|
jpayne@69
|
727 #endif
|
jpayne@69
|
728
|
jpayne@69
|
729 #if EV_STAT_ENABLE
|
jpayne@69
|
730 EV_BEGIN_WATCHER (stat, stat)
|
jpayne@69
|
731 void set (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT
|
jpayne@69
|
732 {
|
jpayne@69
|
733 freeze_guard freeze (this);
|
jpayne@69
|
734 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
|
jpayne@69
|
735 }
|
jpayne@69
|
736
|
jpayne@69
|
737 void start (const char *path, ev_tstamp interval = 0.) EV_NOEXCEPT
|
jpayne@69
|
738 {
|
jpayne@69
|
739 stop ();
|
jpayne@69
|
740 set (path, interval);
|
jpayne@69
|
741 start ();
|
jpayne@69
|
742 }
|
jpayne@69
|
743
|
jpayne@69
|
744 void update () EV_NOEXCEPT
|
jpayne@69
|
745 {
|
jpayne@69
|
746 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
|
jpayne@69
|
747 }
|
jpayne@69
|
748 EV_END_WATCHER (stat, stat)
|
jpayne@69
|
749 #endif
|
jpayne@69
|
750
|
jpayne@69
|
751 #if EV_IDLE_ENABLE
|
jpayne@69
|
752 EV_BEGIN_WATCHER (idle, idle)
|
jpayne@69
|
753 void set () EV_NOEXCEPT { }
|
jpayne@69
|
754 EV_END_WATCHER (idle, idle)
|
jpayne@69
|
755 #endif
|
jpayne@69
|
756
|
jpayne@69
|
757 #if EV_PREPARE_ENABLE
|
jpayne@69
|
758 EV_BEGIN_WATCHER (prepare, prepare)
|
jpayne@69
|
759 void set () EV_NOEXCEPT { }
|
jpayne@69
|
760 EV_END_WATCHER (prepare, prepare)
|
jpayne@69
|
761 #endif
|
jpayne@69
|
762
|
jpayne@69
|
763 #if EV_CHECK_ENABLE
|
jpayne@69
|
764 EV_BEGIN_WATCHER (check, check)
|
jpayne@69
|
765 void set () EV_NOEXCEPT { }
|
jpayne@69
|
766 EV_END_WATCHER (check, check)
|
jpayne@69
|
767 #endif
|
jpayne@69
|
768
|
jpayne@69
|
769 #if EV_EMBED_ENABLE
|
jpayne@69
|
770 EV_BEGIN_WATCHER (embed, embed)
|
jpayne@69
|
771 void set_embed (struct ev_loop *embedded_loop) EV_NOEXCEPT
|
jpayne@69
|
772 {
|
jpayne@69
|
773 freeze_guard freeze (this);
|
jpayne@69
|
774 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
|
jpayne@69
|
775 }
|
jpayne@69
|
776
|
jpayne@69
|
777 void start (struct ev_loop *embedded_loop) EV_NOEXCEPT
|
jpayne@69
|
778 {
|
jpayne@69
|
779 set (embedded_loop);
|
jpayne@69
|
780 start ();
|
jpayne@69
|
781 }
|
jpayne@69
|
782
|
jpayne@69
|
783 void sweep ()
|
jpayne@69
|
784 {
|
jpayne@69
|
785 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
|
jpayne@69
|
786 }
|
jpayne@69
|
787 EV_END_WATCHER (embed, embed)
|
jpayne@69
|
788 #endif
|
jpayne@69
|
789
|
jpayne@69
|
790 #if EV_FORK_ENABLE
|
jpayne@69
|
791 EV_BEGIN_WATCHER (fork, fork)
|
jpayne@69
|
792 void set () EV_NOEXCEPT { }
|
jpayne@69
|
793 EV_END_WATCHER (fork, fork)
|
jpayne@69
|
794 #endif
|
jpayne@69
|
795
|
jpayne@69
|
796 #if EV_ASYNC_ENABLE
|
jpayne@69
|
797 EV_BEGIN_WATCHER (async, async)
|
jpayne@69
|
798 void send () EV_NOEXCEPT
|
jpayne@69
|
799 {
|
jpayne@69
|
800 ev_async_send (EV_A_ static_cast<ev_async *>(this));
|
jpayne@69
|
801 }
|
jpayne@69
|
802
|
jpayne@69
|
803 bool async_pending () EV_NOEXCEPT
|
jpayne@69
|
804 {
|
jpayne@69
|
805 return ev_async_pending (static_cast<ev_async *>(this));
|
jpayne@69
|
806 }
|
jpayne@69
|
807 EV_END_WATCHER (async, async)
|
jpayne@69
|
808 #endif
|
jpayne@69
|
809
|
jpayne@69
|
810 #undef EV_PX
|
jpayne@69
|
811 #undef EV_PX_
|
jpayne@69
|
812 #undef EV_CONSTRUCT
|
jpayne@69
|
813 #undef EV_BEGIN_WATCHER
|
jpayne@69
|
814 #undef EV_END_WATCHER
|
jpayne@69
|
815 }
|
jpayne@69
|
816
|
jpayne@69
|
817 #endif
|
jpayne@69
|
818
|