ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev++.h
(Generate patch)

Comparing libev/ev++.h (file contents):
Revision 1.7 by root, Sat Nov 24 09:48:37 2007 UTC vs.
Revision 1.27 by llucax, Fri Jan 18 18:14:23 2008 UTC

1/*
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
1#ifndef EVPP_H__ 40#ifndef EVPP_H__
2#define EVPP_H__ 41#define EVPP_H__
3 42
43#ifdef EV_H
44# include EV_H
45#else
4#include "ev.h" 46# include "ev.h"
47#endif
5 48
6namespace ev { 49namespace ev {
7 50
8 template<class watcher> 51 typedef ev_tstamp tstamp;
9 class callback
10 {
11 struct object { };
12
13 void *obj;
14 void (object::*meth)(watcher &, int);
15
16 /* a proxy is a kind of recipe on how to call a specific class method */
17 struct proxy_base {
18 virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int) const = 0;
19 };
20 template<class O1, class O2>
21 struct proxy : proxy_base {
22 virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int e) const
23 {
24 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth)))
25 (w, e);
26 }
27 };
28
29 proxy_base *prxy;
30
31 public:
32 template<class O1, class O2>
33 explicit callback (O1 *object, void (O2::*method)(watcher &, int))
34 {
35 static proxy<O1,O2> p;
36 obj = reinterpret_cast<void *>(object);
37 meth = reinterpret_cast<void (object::*)(watcher &, int)>(method);
38 prxy = &p;
39 }
40
41 void call (watcher *w, int e) const
42 {
43 return prxy->call (obj, meth, *w, e);
44 }
45 };
46 52
47 enum { 53 enum {
48 UNDEF = EV_UNDEF, 54 UNDEF = EV_UNDEF,
49 NONE = EV_NONE, 55 NONE = EV_NONE,
50 READ = EV_READ, 56 READ = EV_READ,
51 WRITE = EV_WRITE, 57 WRITE = EV_WRITE,
52 TIMEOUT = EV_TIMEOUT, 58 TIMEOUT = EV_TIMEOUT,
53 PERIODIC = EV_PERIODIC, 59 PERIODIC = EV_PERIODIC,
54 SIGNAL = EV_SIGNAL, 60 SIGNAL = EV_SIGNAL,
61 CHILD = EV_CHILD,
62 STAT = EV_STAT,
55 IDLE = EV_IDLE, 63 IDLE = EV_IDLE,
56 CHECK = EV_CHECK, 64 CHECK = EV_CHECK,
57 PREPARE = EV_PREPARE, 65 PREPARE = EV_PREPARE,
58 CHILD = EV_CHILD, 66 FORK = EV_FORK,
67 EMBED = EV_EMBED,
59 ERROR = EV_ERROR, 68 ERROR = EV_ERROR,
60 }; 69 };
61 70
62 typedef ev_tstamp tstamp; 71 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 template<class ev_watcher, class watcher>
97 struct base : ev_watcher
98 {
99 #if EV_MULTIPLICITY
100 EV_P;
101
102 void set (EV_P)
103 {
104 this->EV_A = EV_A;
105 }
106 #endif
107
108 base ()
109 {
110 ev_init (this, 0);
111 }
112
113 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
114 {
115 this->data = data;
116 ev_set_cb (static_cast<ev_watcher *>(this), cb);
117 }
118
119 // method callback
120 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 static void method_thunk (EV_P_ ev_watcher *w, int revents)
128 {
129 K *obj = static_cast<K *>(w->data);
130 (obj->*method) (*static_cast<watcher *>(w), revents);
131 }
132
133 // const method callback
134 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 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
142 {
143 K *obj = static_cast<K *>(w->data);
144 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
145 }
146
147 // function callback
148 template<void (*function)(watcher &w, int)>
149 void set (void *data = 0)
150 {
151 set_ (data, function_thunk<function>);
152 }
153
154 template<void (*function)(watcher &w, int)>
155 static void function_thunk (EV_P_ ev_watcher *w, int revents)
156 {
157 function (*static_cast<watcher *>(w), revents);
158 }
159
160 // 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 void operator ()(int events = EV_UNDEF)
175 {
176 return ev_cb (static_cast<ev_watcher *>(this))
177 (static_cast<ev_watcher *>(this), events);
178 }
179
180 bool is_active () const
181 {
182 return ev_is_active (static_cast<const ev_watcher *>(this));
183 }
184
185 bool is_pending () const
186 {
187 return ev_is_pending (static_cast<const ev_watcher *>(this));
188 }
189
190 void feed_event (int revents)
191 {
192 ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents);
193 }
194 };
195
196 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
63 236
64 inline ev_tstamp now (EV_P) 237 inline ev_tstamp now (EV_P)
65 { 238 {
66 return ev_now (EV_A); 239 return ev_now (EV_A);
67 } 240 }
68 241
69 #if EV_MULTIPLICITY 242 #if EV_MULTIPLICITY
70
71 #define EV_CONSTRUCT(cppstem) \
72 EV_P; \ 243 #define EV_CONSTRUCT \
73 \
74 void set (EV_P) \ 244 (EV_P = EV_DEFAULT) \
75 { \ 245 { \
76 this->EV_A = EV_A; \
77 } \
78 \ 246 set (EV_A); \
79 template<class O1, class O2> \ 247 }
80 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
81 : callback<cppstem> (object, method), EV_A (EV_A)
82
83 #else 248 #else
84 249 #define EV_CONSTRUCT \
85 #define EV_CONSTRUCT(cppstem) \ 250 () \
86 template<class O1, class O2> \ 251 { \
87 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 252 }
88 : callback<cppstem> (object, method)
89
90 #endif 253 #endif
91 254
92 /* using a template here would require quite a bit more lines, 255 /* using a template here would require quite a bit more lines,
93 * so a macro solution was chosen */ 256 * so a macro solution was chosen */
94 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 257 #define EV_BEGIN_WATCHER(cppstem,cstem) \
95 \ 258 \
96 struct cppstem : ev_ ## cstem, callback<cppstem> \ 259 struct cppstem : base<ev_ ## cstem, cppstem> \
97 { \ 260 { \
98 EV_CONSTRUCT (cppstem) \
99 { \
100 ev_init (static_cast<ev_ ## cstem *>(this), thunk); \
101 } \
102 \
103 bool is_active () const \
104 { \
105 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
106 } \
107 \
108 bool is_pending () const \
109 { \
110 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
111 } \
112 \
113 void start () \ 261 void start () \
114 { \ 262 { \
115 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 263 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
116 } \ 264 } \
117 \ 265 \
118 void stop () \ 266 void stop () \
119 { \ 267 { \
120 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 268 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
121 } \ 269 } \
122 \ 270 \
123 void operator ()(int events = EV_UNDEF) \
124 { \ 271 cppstem EV_CONSTRUCT \
125 return call (this, events); \
126 } \
127 \ 272 \
128 ~cppstem () \ 273 ~cppstem () \
129 { \ 274 { \
130 stop (); \ 275 stop (); \
131 } \ 276 } \
132 \ 277 \
278 using base<ev_ ## cstem, cppstem>::set; \
279 \
133 private: \ 280 private: \
134 \ 281 \
135 cppstem (const cppstem &o) \ 282 cppstem (const cppstem &o); \
136 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
137 { /* disabled */ } \
138 \
139 void operator =(const cppstem &o) { /* disabled */ } \
140 \
141 static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \
142 { \ 283 \
143 (*static_cast<cppstem *>(w))(revents); \ 284 cppstem & operator =(const cppstem &o); \
144 } \
145 \ 285 \
146 public: 286 public:
147 287
148 #define EV_END_WATCHER(cppstem,cstem) \ 288 #define EV_END_WATCHER(cppstem,cstem) \
149 }; 289 };
191 { 331 {
192 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 332 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
193 } 333 }
194 EV_END_WATCHER (timer, timer) 334 EV_END_WATCHER (timer, timer)
195 335
196 #if EV_PERIODICS 336 #if EV_PERIODIC_ENABLE
197 EV_BEGIN_WATCHER (periodic, periodic) 337 EV_BEGIN_WATCHER (periodic, periodic)
198 void set (ev_tstamp at, ev_tstamp interval = 0.) 338 void set (ev_tstamp at, ev_tstamp interval = 0.)
199 { 339 {
200 int active = is_active (); 340 int active = is_active ();
201 if (active) stop (); 341 if (active) stop ();
212 void again () 352 void again ()
213 { 353 {
214 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 354 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
215 } 355 }
216 EV_END_WATCHER (periodic, periodic) 356 EV_END_WATCHER (periodic, periodic)
357 #endif
358
359 EV_BEGIN_WATCHER (sig, signal)
360 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 EV_END_WATCHER (sig, signal)
374
375 EV_BEGIN_WATCHER (child, child)
376 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 EV_END_WATCHER (child, child)
390
391 #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 stop ();
404 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)
217 #endif 413 #endif
218 414
219 EV_BEGIN_WATCHER (idle, idle) 415 EV_BEGIN_WATCHER (idle, idle)
220 void set () { } 416 void set () { }
221 EV_END_WATCHER (idle, idle) 417 EV_END_WATCHER (idle, idle)
226 422
227 EV_BEGIN_WATCHER (check, check) 423 EV_BEGIN_WATCHER (check, check)
228 void set () { } 424 void set () { }
229 EV_END_WATCHER (check, check) 425 EV_END_WATCHER (check, check)
230 426
231 EV_BEGIN_WATCHER (sig, signal) 427 #if EV_EMBED_ENABLE
232 void set (int signum)
233 {
234 int active = is_active ();
235 if (active) stop ();
236 ev_signal_set (static_cast<ev_signal *>(this), signum);
237 if (active) start ();
238 }
239
240 void start (int signum)
241 {
242 set (signum);
243 start ();
244 }
245 EV_END_WATCHER (sig, signal)
246
247 EV_BEGIN_WATCHER (child, child)
248 void set (int pid)
249 {
250 int active = is_active ();
251 if (active) stop ();
252 ev_child_set (static_cast<ev_child *>(this), pid);
253 if (active) start ();
254 }
255
256 void start (int pid)
257 {
258 set (pid);
259 start ();
260 }
261 EV_END_WATCHER (child, child)
262
263 #if EV_MULTIPLICITY
264
265 EV_BEGIN_WATCHER (embed, embed) 428 EV_BEGIN_WATCHER (embed, embed)
266 void set (struct ev_loop *loop)
267 {
268 int active = is_active ();
269 if (active) stop ();
270 ev_embed_set (static_cast<ev_embed *>(this), loop);
271 if (active) start ();
272 }
273
274 void start (struct ev_loop *embedded_loop) 429 void start (struct ev_loop *embedded_loop)
275 { 430 {
276 set (embedded_loop); 431 stop ();
432 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
277 start (); 433 start ();
278 } 434 }
279 435
280 void sweep () 436 void sweep ()
281 { 437 {
282 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this)); 438 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
283 } 439 }
284 EV_END_WATCHER (embed, embed) 440 EV_END_WATCHER (embed, embed)
441 #endif
285 442
443 #if EV_FORK_ENABLE
444 EV_BEGIN_WATCHER (fork, fork)
445 void set () { }
446 EV_END_WATCHER (fork, fork)
286 #endif 447 #endif
287 448
288 #undef EV_CONSTRUCT 449 #undef EV_CONSTRUCT
289 #undef EV_BEGIN_WATCHER 450 #undef EV_BEGIN_WATCHER
290 #undef EV_END_WATCHER 451 #undef EV_END_WATCHER

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines