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

Comparing libev/ev++.h (file contents):
Revision 1.1 by root, Sat Nov 10 21:19:30 2007 UTC vs.
Revision 1.25 by llucax, Fri Jan 18 18:13:40 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
4/* work in progress, don't use unless you know what you are doing */ 43#ifdef EV_H
44# include EV_H
45#else
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 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 enum
10 { 72 {
11 struct object { }; 73 AUTO = EVFLAG_AUTO,
12 74 NOENV = EVFLAG_NOENV,
13 void *obj; 75 FORKCHECK = EVFLAG_FORKCHECK,
14 void (object::*meth)(watcher &, int); 76 SELECT = EVBACKEND_SELECT,
15 77 POLL = EVBACKEND_POLL,
16 /* a proxy is a kind of recipe on how to call a specific class method */ 78 EPOLL = EVBACKEND_EPOLL,
17 struct proxy_base { 79 KQUEUE = EVBACKEND_KQUEUE,
18 virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int) const = 0; 80 DEVPOLL = EVBACKEND_DEVPOLL,
81 PORT = EVBACKEND_PORT
19 }; 82 };
20 template<class O1, class O2> 83
21 struct proxy : proxy_base { 84 enum
22 virtual void call (void *obj, void (object::*meth)(watcher &, int), watcher &w, int e) const 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)
23 { 103 {
24 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) 104 this->EV_A = EV_A;
25 (w, e);
26 } 105 }
106 #endif
107
108 base ()
109 {
110 ev_init (this, 0);
27 }; 111 }
28 112
29 proxy_base *prxy; 113 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
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 } 114 {
40 115 this->data = data;
41 void call (watcher *w, int e) const 116 ev_set_cb (static_cast<ev_watcher *>(this), cb);
42 { 117 }
43 return prxy->call (obj, meth, *w, e); 118
119 // method callback
120 template<class K, void (K::*method)(watcher &w, int)>
121 void set (K *object)
44 } 122 {
45 }; 123 set_ (object, method_thunk<K, method>);
124 }
46 125
47 #include "ev.h" 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 }
48 132
49 typedef ev_tstamp tstamp; 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
50 191
51 inline ev_tstamp now (EV_P) 192 inline ev_tstamp now (EV_P)
52 { 193 {
53 return ev_now (EV_A); 194 return ev_now (EV_A);
54 } 195 }
55 196
56 #if EV_MULTIPLICITY 197 #if EV_MULTIPLICITY
57
58 #define EV_CONSTRUCT(cppstem) \
59 EV_P; \ 198 #define EV_CONSTRUCT \
60 \
61 void set (EV_P) \ 199 (EV_P = EV_DEFAULT) \
62 { \ 200 { \
63 this->EV_A = EV_A; \
64 } \
65 \ 201 set (EV_A); \
66 template<class O1, class O2> \ 202 }
67 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
68 : callback<cppstem> (object, method), EV_A (EV_A)
69
70 #else 203 #else
71 204 #define EV_CONSTRUCT \
72 #define EV_CONSTRUCT(cppstem) \ 205 () \
73 template<class O1, class O2> \ 206 { \
74 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 207 }
75 : callback<cppstem> (object, method)
76
77 #endif 208 #endif
78 209
79 /* using a template here would require quite a bit more lines, 210 /* using a template here would require quite a bit more lines,
80 * so a macro solution was chosen */ 211 * so a macro solution was chosen */
81 #define EV_DECLARE_WATCHER(cppstem,cstem) \ 212 #define EV_BEGIN_WATCHER(cppstem,cstem) \
82 \ 213 \
83 extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \
84 \
85 struct cppstem : ev_ ## cstem, callback<cppstem> \ 214 struct cppstem : base<ev_ ## cstem, cppstem> \
86 { \ 215 { \
87 EV_CONSTRUCT (cppstem) \
88 { \
89 ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \
90 } \
91 \
92 bool is_active () const \
93 { \
94 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
95 } \
96 \
97 bool is_pending () const \
98 { \
99 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
100 } \
101 \
102 void start () \ 216 void start () \
103 { \ 217 { \
104 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 218 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
105 } \ 219 } \
106 \ 220 \
107 void stop () \ 221 void stop () \
108 { \ 222 { \
109 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 223 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
110 } \ 224 } \
111 \ 225 \
112 void operator ()(int events = EV_UNDEF) \ 226 cppstem EV_CONSTRUCT \
227 \
228 ~cppstem () \
113 { \ 229 { \
114 return call (this, events); \ 230 stop (); \
115 } \ 231 } \
116 \ 232 \
233 using base<ev_ ## cstem, cppstem>::set; \
234 \
117 private: \ 235 private: \
118 \ 236 \
119 cppstem (const cppstem &o) \ 237 cppstem (const cppstem &o); \
120 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ 238 \
121 { /* disabled */ } \ 239 cppstem & operator =(const cppstem &o); \
122 void operator =(const cppstem &o) { /* disabled */ } \
123 \ 240 \
124 public: 241 public:
125 242
243 #define EV_END_WATCHER(cppstem,cstem) \
244 };
245
126 EV_DECLARE_WATCHER (io, io) 246 EV_BEGIN_WATCHER (io, io)
127 void set (int fd, int events) 247 void set (int fd, int events)
128 { 248 {
129 int active = is_active (); 249 int active = is_active ();
130 if (active) stop (); 250 if (active) stop ();
131 ev_io_set (static_cast<ev_io *>(this), fd, events); 251 ev_io_set (static_cast<ev_io *>(this), fd, events);
143 void start (int fd, int events) 263 void start (int fd, int events)
144 { 264 {
145 set (fd, events); 265 set (fd, events);
146 start (); 266 start ();
147 } 267 }
148 }; 268 EV_END_WATCHER (io, io)
149 269
150 EV_DECLARE_WATCHER (timer, timer) 270 EV_BEGIN_WATCHER (timer, timer)
151 void set (ev_tstamp after, ev_tstamp repeat = 0.) 271 void set (ev_tstamp after, ev_tstamp repeat = 0.)
152 { 272 {
153 int active = is_active (); 273 int active = is_active ();
154 if (active) stop (); 274 if (active) stop ();
155 ev_timer_set (static_cast<ev_timer *>(this), after, repeat); 275 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
164 284
165 void again () 285 void again ()
166 { 286 {
167 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 287 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
168 } 288 }
169 }; 289 EV_END_WATCHER (timer, timer)
170 290
291 #if EV_PERIODIC_ENABLE
171 EV_DECLARE_WATCHER (periodic, periodic) 292 EV_BEGIN_WATCHER (periodic, periodic)
172 void set (ev_tstamp at, ev_tstamp interval = 0.) 293 void set (ev_tstamp at, ev_tstamp interval = 0.)
173 { 294 {
174 int active = is_active (); 295 int active = is_active ();
175 if (active) stop (); 296 if (active) stop ();
176 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); 297 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
185 306
186 void again () 307 void again ()
187 { 308 {
188 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 309 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
189 } 310 }
190 }; 311 EV_END_WATCHER (periodic, periodic)
312 #endif
191 313
192 EV_DECLARE_WATCHER (idle, idle)
193 };
194
195 EV_DECLARE_WATCHER (prepare, prepare)
196 };
197
198 EV_DECLARE_WATCHER (check, check)
199 };
200
201 EV_DECLARE_WATCHER (sig, signal) 314 EV_BEGIN_WATCHER (sig, signal)
202 void set (int signum) 315 void set (int signum)
203 { 316 {
204 int active = is_active (); 317 int active = is_active ();
205 if (active) stop (); 318 if (active) stop ();
206 ev_signal_set (static_cast<ev_signal *>(this), signum); 319 ev_signal_set (static_cast<ev_signal *>(this), signum);
210 void start (int signum) 323 void start (int signum)
211 { 324 {
212 set (signum); 325 set (signum);
213 start (); 326 start ();
214 } 327 }
215 }; 328 EV_END_WATCHER (sig, signal)
216 329
217 EV_DECLARE_WATCHER (child, child) 330 EV_BEGIN_WATCHER (child, child)
218 void set (int pid) 331 void set (int pid)
219 { 332 {
220 int active = is_active (); 333 int active = is_active ();
221 if (active) stop (); 334 if (active) stop ();
222 ev_child_set (static_cast<ev_child *>(this), pid); 335 ev_child_set (static_cast<ev_child *>(this), pid);
226 void start (int pid) 339 void start (int pid)
227 { 340 {
228 set (pid); 341 set (pid);
229 start (); 342 start ();
230 } 343 }
231 }; 344 EV_END_WATCHER (child, child)
345
346 #if EV_STAT_ENABLE
347 EV_BEGIN_WATCHER (stat, stat)
348 void set (const char *path, ev_tstamp interval = 0.)
349 {
350 int active = is_active ();
351 if (active) stop ();
352 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
353 if (active) start ();
354 }
355
356 void start (const char *path, ev_tstamp interval = 0.)
357 {
358 stop ();
359 set (path, interval);
360 start ();
361 }
362
363 void update ()
364 {
365 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
366 }
367 EV_END_WATCHER (stat, stat)
368 #endif
369
370 EV_BEGIN_WATCHER (idle, idle)
371 void set () { }
372 EV_END_WATCHER (idle, idle)
373
374 EV_BEGIN_WATCHER (prepare, prepare)
375 void set () { }
376 EV_END_WATCHER (prepare, prepare)
377
378 EV_BEGIN_WATCHER (check, check)
379 void set () { }
380 EV_END_WATCHER (check, check)
381
382 #if EV_EMBED_ENABLE
383 EV_BEGIN_WATCHER (embed, embed)
384 void start (struct ev_loop *embedded_loop)
385 {
386 stop ();
387 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
388 start ();
389 }
390
391 void sweep ()
392 {
393 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
394 }
395 EV_END_WATCHER (embed, embed)
396 #endif
397
398 #if EV_FORK_ENABLE
399 EV_BEGIN_WATCHER (fork, fork)
400 void set () { }
401 EV_END_WATCHER (fork, fork)
402 #endif
232 403
233 #undef EV_CONSTRUCT 404 #undef EV_CONSTRUCT
405 #undef EV_BEGIN_WATCHER
234 #undef EV_DECLARE_WATCHER 406 #undef EV_END_WATCHER
235} 407}
236 408
237#endif 409#endif
238 410

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines