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

Comparing libev/ev++.h (file contents):
Revision 1.2 by root, Sun Nov 11 00:05:59 2007 UTC vs.
Revision 1.24 by llucax, Fri Jan 18 18:13:21 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
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
47 #include "ev.h"
48 52
49 enum { 53 enum {
50 UNDEF = EV_UNDEF, 54 UNDEF = EV_UNDEF,
51 NONE = EV_NONE, 55 NONE = EV_NONE,
52 READ = EV_READ, 56 READ = EV_READ,
53 WRITE = EV_WRITE, 57 WRITE = EV_WRITE,
54 TIMEOUT = EV_TIMEOUT, 58 TIMEOUT = EV_TIMEOUT,
55 PERIODIC = EV_PERIODIC, 59 PERIODIC = EV_PERIODIC,
56 SIGNAL = EV_SIGNAL, 60 SIGNAL = EV_SIGNAL,
61 CHILD = EV_CHILD,
62 STAT = EV_STAT,
57 IDLE = EV_IDLE, 63 IDLE = EV_IDLE,
58 CHECK = EV_CHECK, 64 CHECK = EV_CHECK,
59 PREPARE = EV_PREPARE, 65 PREPARE = EV_PREPARE,
60 CHILD = EV_CHILD, 66 FORK = EV_FORK,
67 EMBED = EV_EMBED,
61 ERROR = EV_ERROR, 68 ERROR = EV_ERROR,
62 }; 69 };
63 70
64 typedef ev_tstamp tstamp; 71 template<class ev_watcher, class watcher>
72 struct base : ev_watcher
73 {
74 #if EV_MULTIPLICITY
75 EV_P;
76
77 void set (EV_P)
78 {
79 this->EV_A = EV_A;
80 }
81 #endif
82
83 base ()
84 {
85 ev_init (this, 0);
86 }
87
88 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
89 {
90 this->data = data;
91 ev_set_cb (static_cast<ev_watcher *>(this), cb);
92 }
93
94 // method callback
95 template<class K, void (K::*method)(watcher &w, int)>
96 void set (K *object)
97 {
98 set_ (object, method_thunk<K, method>);
99 }
100
101 template<class K, void (K::*method)(watcher &w, int)>
102 static void method_thunk (EV_P_ ev_watcher *w, int revents)
103 {
104 K *obj = static_cast<K *>(w->data);
105 (obj->*method) (*static_cast<watcher *>(w), revents);
106 }
107
108 // const method callback
109 template<class K, void (K::*method)(watcher &w, int) const>
110 void set (const K *object)
111 {
112 set_ (object, const_method_thunk<K, method>);
113 }
114
115 template<class K, void (K::*method)(watcher &w, int) const>
116 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
117 {
118 K *obj = static_cast<K *>(w->data);
119 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
120 }
121
122 // function callback
123 template<void (*function)(watcher &w, int)>
124 void set (void *data = 0)
125 {
126 set_ (data, function_thunk<function>);
127 }
128
129 template<void (*function)(watcher &w, int)>
130 static void function_thunk (EV_P_ ev_watcher *w, int revents)
131 {
132 function (*static_cast<watcher *>(w), revents);
133 }
134
135 // simple callback
136 template<class K, void (K::*method)()>
137 void set (K *object)
138 {
139 set_ (object, method_noargs_thunk<K, method>);
140 }
141
142 template<class K, void (K::*method)()>
143 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
144 {
145 K *obj = static_cast<K *>(w->data);
146 (obj->*method) ();
147 }
148
149 void operator ()(int events = EV_UNDEF)
150 {
151 return ev_cb (static_cast<ev_watcher *>(this))
152 (static_cast<ev_watcher *>(this), events);
153 }
154
155 bool is_active () const
156 {
157 return ev_is_active (static_cast<const ev_watcher *>(this));
158 }
159
160 bool is_pending () const
161 {
162 return ev_is_pending (static_cast<const ev_watcher *>(this));
163 }
164 };
165
65 166
66 inline ev_tstamp now (EV_P) 167 inline ev_tstamp now (EV_P)
67 { 168 {
68 return ev_now (EV_A); 169 return ev_now (EV_A);
69 } 170 }
70 171
71 #if EV_MULTIPLICITY 172 #if EV_MULTIPLICITY
72
73 #define EV_CONSTRUCT(cppstem) \
74 EV_P; \ 173 #define EV_CONSTRUCT \
75 \
76 void set (EV_P) \ 174 (EV_P = EV_DEFAULT) \
77 { \ 175 { \
78 this->EV_A = EV_A; \
79 } \
80 \ 176 set (EV_A); \
81 template<class O1, class O2> \ 177 }
82 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
83 : callback<cppstem> (object, method), EV_A (EV_A)
84
85 #else 178 #else
86 179 #define EV_CONSTRUCT \
87 #define EV_CONSTRUCT(cppstem) \ 180 () \
88 template<class O1, class O2> \ 181 { \
89 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 182 }
90 : callback<cppstem> (object, method)
91
92 #endif 183 #endif
93 184
94 /* using a template here would require quite a bit more lines, 185 /* using a template here would require quite a bit more lines,
95 * so a macro solution was chosen */ 186 * so a macro solution was chosen */
96 #define EV_DECLARE_WATCHER(cppstem,cstem) \ 187 #define EV_BEGIN_WATCHER(cppstem,cstem) \
97 \ 188 \
98 extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \
99 \
100 struct cppstem : ev_ ## cstem, callback<cppstem> \ 189 struct cppstem : base<ev_ ## cstem, cppstem> \
101 { \ 190 { \
102 EV_CONSTRUCT (cppstem) \
103 { \
104 ev_init (static_cast<ev_ ## cstem *>(this), cb_ ## cppstem); \
105 } \
106 \
107 bool is_active () const \
108 { \
109 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
110 } \
111 \
112 bool is_pending () const \
113 { \
114 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
115 } \
116 \
117 void start () \ 191 void start () \
118 { \ 192 { \
119 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 193 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
120 } \ 194 } \
121 \ 195 \
122 void stop () \ 196 void stop () \
123 { \ 197 { \
124 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 198 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
125 } \ 199 } \
126 \ 200 \
127 void operator ()(int events = EV_UNDEF) \ 201 cppstem EV_CONSTRUCT \
202 \
203 ~cppstem () \
128 { \ 204 { \
129 return call (this, events); \ 205 stop (); \
130 } \ 206 } \
131 \ 207 \
208 using base<ev_ ## cstem, cppstem>::set; \
209 \
132 private: \ 210 private: \
133 \ 211 \
134 cppstem (const cppstem &o) \ 212 cppstem (const cppstem &o); \
135 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \ 213 \
136 { /* disabled */ } \ 214 cppstem & operator =(const cppstem &o); \
137 void operator =(const cppstem &o) { /* disabled */ } \
138 \ 215 \
139 public: 216 public:
140 217
218 #define EV_END_WATCHER(cppstem,cstem) \
219 };
220
141 EV_DECLARE_WATCHER (io, io) 221 EV_BEGIN_WATCHER (io, io)
142 void set (int fd, int events) 222 void set (int fd, int events)
143 { 223 {
144 int active = is_active (); 224 int active = is_active ();
145 if (active) stop (); 225 if (active) stop ();
146 ev_io_set (static_cast<ev_io *>(this), fd, events); 226 ev_io_set (static_cast<ev_io *>(this), fd, events);
158 void start (int fd, int events) 238 void start (int fd, int events)
159 { 239 {
160 set (fd, events); 240 set (fd, events);
161 start (); 241 start ();
162 } 242 }
163 }; 243 EV_END_WATCHER (io, io)
164 244
165 EV_DECLARE_WATCHER (timer, timer) 245 EV_BEGIN_WATCHER (timer, timer)
166 void set (ev_tstamp after, ev_tstamp repeat = 0.) 246 void set (ev_tstamp after, ev_tstamp repeat = 0.)
167 { 247 {
168 int active = is_active (); 248 int active = is_active ();
169 if (active) stop (); 249 if (active) stop ();
170 ev_timer_set (static_cast<ev_timer *>(this), after, repeat); 250 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
179 259
180 void again () 260 void again ()
181 { 261 {
182 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 262 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
183 } 263 }
184 }; 264 EV_END_WATCHER (timer, timer)
185 265
266 #if EV_PERIODIC_ENABLE
186 EV_DECLARE_WATCHER (periodic, periodic) 267 EV_BEGIN_WATCHER (periodic, periodic)
187 void set (ev_tstamp at, ev_tstamp interval = 0.) 268 void set (ev_tstamp at, ev_tstamp interval = 0.)
188 { 269 {
189 int active = is_active (); 270 int active = is_active ();
190 if (active) stop (); 271 if (active) stop ();
191 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); 272 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
200 281
201 void again () 282 void again ()
202 { 283 {
203 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 284 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
204 } 285 }
205 }; 286 EV_END_WATCHER (periodic, periodic)
287 #endif
206 288
207 EV_DECLARE_WATCHER (idle, idle)
208 };
209
210 EV_DECLARE_WATCHER (prepare, prepare)
211 };
212
213 EV_DECLARE_WATCHER (check, check)
214 };
215
216 EV_DECLARE_WATCHER (sig, signal) 289 EV_BEGIN_WATCHER (sig, signal)
217 void set (int signum) 290 void set (int signum)
218 { 291 {
219 int active = is_active (); 292 int active = is_active ();
220 if (active) stop (); 293 if (active) stop ();
221 ev_signal_set (static_cast<ev_signal *>(this), signum); 294 ev_signal_set (static_cast<ev_signal *>(this), signum);
225 void start (int signum) 298 void start (int signum)
226 { 299 {
227 set (signum); 300 set (signum);
228 start (); 301 start ();
229 } 302 }
230 }; 303 EV_END_WATCHER (sig, signal)
231 304
232 EV_DECLARE_WATCHER (child, child) 305 EV_BEGIN_WATCHER (child, child)
233 void set (int pid) 306 void set (int pid)
234 { 307 {
235 int active = is_active (); 308 int active = is_active ();
236 if (active) stop (); 309 if (active) stop ();
237 ev_child_set (static_cast<ev_child *>(this), pid); 310 ev_child_set (static_cast<ev_child *>(this), pid);
241 void start (int pid) 314 void start (int pid)
242 { 315 {
243 set (pid); 316 set (pid);
244 start (); 317 start ();
245 } 318 }
246 }; 319 EV_END_WATCHER (child, child)
320
321 #if EV_STAT_ENABLE
322 EV_BEGIN_WATCHER (stat, stat)
323 void set (const char *path, ev_tstamp interval = 0.)
324 {
325 int active = is_active ();
326 if (active) stop ();
327 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
328 if (active) start ();
329 }
330
331 void start (const char *path, ev_tstamp interval = 0.)
332 {
333 stop ();
334 set (path, interval);
335 start ();
336 }
337
338 void update ()
339 {
340 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
341 }
342 EV_END_WATCHER (stat, stat)
343 #endif
344
345 EV_BEGIN_WATCHER (idle, idle)
346 void set () { }
347 EV_END_WATCHER (idle, idle)
348
349 EV_BEGIN_WATCHER (prepare, prepare)
350 void set () { }
351 EV_END_WATCHER (prepare, prepare)
352
353 EV_BEGIN_WATCHER (check, check)
354 void set () { }
355 EV_END_WATCHER (check, check)
356
357 #if EV_EMBED_ENABLE
358 EV_BEGIN_WATCHER (embed, embed)
359 void start (struct ev_loop *embedded_loop)
360 {
361 stop ();
362 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
363 start ();
364 }
365
366 void sweep ()
367 {
368 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
369 }
370 EV_END_WATCHER (embed, embed)
371 #endif
372
373 #if EV_FORK_ENABLE
374 EV_BEGIN_WATCHER (fork, fork)
375 void set () { }
376 EV_END_WATCHER (fork, fork)
377 #endif
247 378
248 #undef EV_CONSTRUCT 379 #undef EV_CONSTRUCT
380 #undef EV_BEGIN_WATCHER
249 #undef EV_DECLARE_WATCHER 381 #undef EV_END_WATCHER
250} 382}
251 383
252#endif 384#endif
253 385

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines