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

Comparing libev/ev++.h (file contents):
Revision 1.3 by root, Sun Nov 11 01:07:35 2007 UTC vs.
Revision 1.20 by root, Thu Dec 20 08:17:57 2007 UTC

1#ifndef EVPP_H__ 1#ifndef EVPP_H__
2#define EVPP_H__ 2#define EVPP_H__
3 3
4/* work in progress, don't use unless you know what you are doing */ 4#ifdef EV_H
5# include EV_H
6#else
7# include <ev.h>
8#endif
5 9
6namespace ev { 10namespace ev {
7 11
8 template<class watcher> 12 template<class ev_watcher, class watcher>
9 class callback 13 struct base : ev_watcher
10 { 14 {
11 struct object { }; 15 #if EV_MULTIPLICITY
16 EV_P;
12 17
13 void *obj; 18 void set (EV_P)
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 { 19 {
24 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(watcher &, int)>(meth))) 20 this->EV_A = EV_A;
25 (w, e);
26 } 21 }
22 #endif
23
24 base ()
25 {
26 ev_init (this, 0);
27 }; 27 }
28 28
29 proxy_base *prxy; 29 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 } 30 {
40 31 this->data = data;
41 void call (watcher *w, int e) const 32 ev_set_cb (static_cast<ev_watcher *>(this), cb);
42 { 33 }
43 return prxy->call (obj, meth, *w, e); 34
35 // method callback
36 template<class K, void (K::*method)(watcher &w, int)>
37 void set (K *object)
38 {
39 set_ (object, method_thunk<K, method>);
40 }
41
42 template<class K, void (K::*method)(watcher &w, int)>
43 static void method_thunk (EV_P_ ev_watcher *w, int revents)
44 {
45 K *obj = static_cast<K *>(w->data);
46 (obj->*method) (*static_cast<watcher *>(w), revents);
47 }
48
49 // const method callback
50 template<class K, void (K::*method)(watcher &w, int) const>
51 void set (const K *object)
52 {
53 set_ (object, const_method_thunk<K, method>);
54 }
55
56 template<class K, void (K::*method)(watcher &w, int) const>
57 static void const_method_thunk (EV_P_ ev_watcher *w, int revents)
58 {
59 K *obj = static_cast<K *>(w->data);
60 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents);
61 }
62
63 // function callback
64 template<void (*function)(watcher &w, int)>
65 void set (void *data = 0)
66 {
67 set_ (data, function_thunk<function>);
68 }
69
70 template<void (*function)(watcher &w, int)>
71 static void function_thunk (EV_P_ ev_watcher *w, int revents)
72 {
73 function (*static_cast<watcher *>(w), revents);
74 }
75
76 // simple callback
77 template<class K, void (K::*method)()>
78 void set (K *object)
79 {
80 set_ (object, method_noargs_thunk<K, method>);
81 }
82
83 template<class K, void (K::*method)()>
84 static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents)
85 {
86 K *obj = static_cast<K *>(w->data);
87 (obj->*method) ();
88 }
89
90 void operator ()(int events = EV_UNDEF)
91 {
92 return ev_cb (static_cast<ev_watcher *>(this))
93 (static_cast<ev_watcher *>(this), events);
94 }
95
96 bool is_active () const
97 {
98 return ev_is_active (static_cast<const ev_watcher *>(this));
99 }
100
101 bool is_pending () const
102 {
103 return ev_is_pending (static_cast<const ev_watcher *>(this));
44 } 104 }
45 }; 105 };
46
47 #include "ev.h"
48 106
49 enum { 107 enum {
50 UNDEF = EV_UNDEF, 108 UNDEF = EV_UNDEF,
51 NONE = EV_NONE, 109 NONE = EV_NONE,
52 READ = EV_READ, 110 READ = EV_READ,
53 WRITE = EV_WRITE, 111 WRITE = EV_WRITE,
54 TIMEOUT = EV_TIMEOUT, 112 TIMEOUT = EV_TIMEOUT,
55 PERIODIC = EV_PERIODIC, 113 PERIODIC = EV_PERIODIC,
56 SIGNAL = EV_SIGNAL, 114 SIGNAL = EV_SIGNAL,
115 CHILD = EV_CHILD,
116 STAT = EV_STAT,
57 IDLE = EV_IDLE, 117 IDLE = EV_IDLE,
58 CHECK = EV_CHECK, 118 CHECK = EV_CHECK,
59 PREPARE = EV_PREPARE, 119 PREPARE = EV_PREPARE,
60 CHILD = EV_CHILD, 120 FORK = EV_FORK,
121 EMBED = EV_EMBED,
61 ERROR = EV_ERROR, 122 ERROR = EV_ERROR,
62 }; 123 };
63 124
64 typedef ev_tstamp tstamp; 125 typedef ev_tstamp tstamp;
65 126
67 { 128 {
68 return ev_now (EV_A); 129 return ev_now (EV_A);
69 } 130 }
70 131
71 #if EV_MULTIPLICITY 132 #if EV_MULTIPLICITY
72
73 #define EV_CONSTRUCT(cppstem) \
74 EV_P; \ 133 #define EV_CONSTRUCT \
75 \
76 void set (EV_P) \ 134 (EV_P = EV_DEFAULT) \
77 { \ 135 { \
78 this->EV_A = EV_A; \
79 } \
80 \ 136 set (EV_A); \
81 template<class O1, class O2> \ 137 }
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 138 #else
86 139 #define EV_CONSTRUCT \
87 #define EV_CONSTRUCT(cppstem) \ 140 () \
88 template<class O1, class O2> \ 141 { \
89 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 142 }
90 : callback<cppstem> (object, method)
91
92 #endif 143 #endif
93 144
94 /* using a template here would require quite a bit more lines, 145 /* using a template here would require quite a bit more lines,
95 * so a macro solution was chosen */ 146 * so a macro solution was chosen */
96 #define EV_DECLARE_WATCHER(cppstem,cstem) \ 147 #define EV_BEGIN_WATCHER(cppstem,cstem) \
97 \ 148 \
98 extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \
99 \
100 struct cppstem : ev_ ## cstem, callback<cppstem> \ 149 struct cppstem : base<ev_ ## cstem, cppstem> \
101 { \ 150 { \
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 () \ 151 void start () \
118 { \ 152 { \
119 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 153 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
120 } \ 154 } \
121 \ 155 \
122 void stop () \ 156 void stop () \
123 { \ 157 { \
124 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 158 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
125 } \ 159 } \
126 \ 160 \
127 void operator ()(int events = EV_UNDEF) \
128 { \ 161 cppstem EV_CONSTRUCT \
129 return call (this, events); \
130 } \
131 \ 162 \
132 ~cppstem () \ 163 ~cppstem () \
133 { \ 164 { \
134 stop (); \ 165 stop (); \
135 } \ 166 } \
136 \ 167 \
168 using base<ev_ ## cstem, cppstem>::set; \
169 \
137 private: \ 170 private: \
138 \ 171 \
139 cppstem (const cppstem &o) \ 172 cppstem (const cppstem &o) \
140 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
141 { /* disabled */ } \ 173 { /* disabled */ } \
174 \
142 void operator =(const cppstem &o) { /* disabled */ } \ 175 void operator =(const cppstem &o) { /* disabled */ } \
143 \ 176 \
144 public: 177 public:
145 178
179 #define EV_END_WATCHER(cppstem,cstem) \
180 };
181
146 EV_DECLARE_WATCHER (io, io) 182 EV_BEGIN_WATCHER (io, io)
147 void set (int fd, int events) 183 void set (int fd, int events)
148 { 184 {
149 int active = is_active (); 185 int active = is_active ();
150 if (active) stop (); 186 if (active) stop ();
151 ev_io_set (static_cast<ev_io *>(this), fd, events); 187 ev_io_set (static_cast<ev_io *>(this), fd, events);
163 void start (int fd, int events) 199 void start (int fd, int events)
164 { 200 {
165 set (fd, events); 201 set (fd, events);
166 start (); 202 start ();
167 } 203 }
168 }; 204 EV_END_WATCHER (io, io)
169 205
170 EV_DECLARE_WATCHER (timer, timer) 206 EV_BEGIN_WATCHER (timer, timer)
171 void set (ev_tstamp after, ev_tstamp repeat = 0.) 207 void set (ev_tstamp after, ev_tstamp repeat = 0.)
172 { 208 {
173 int active = is_active (); 209 int active = is_active ();
174 if (active) stop (); 210 if (active) stop ();
175 ev_timer_set (static_cast<ev_timer *>(this), after, repeat); 211 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
184 220
185 void again () 221 void again ()
186 { 222 {
187 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 223 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
188 } 224 }
189 }; 225 EV_END_WATCHER (timer, timer)
190 226
191 #if EV_PERIODICS 227 #if EV_PERIODIC_ENABLE
192 EV_DECLARE_WATCHER (periodic, periodic) 228 EV_BEGIN_WATCHER (periodic, periodic)
193 void set (ev_tstamp at, ev_tstamp interval = 0.) 229 void set (ev_tstamp at, ev_tstamp interval = 0.)
194 { 230 {
195 int active = is_active (); 231 int active = is_active ();
196 if (active) stop (); 232 if (active) stop ();
197 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); 233 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
206 242
207 void again () 243 void again ()
208 { 244 {
209 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 245 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
210 } 246 }
211 }; 247 EV_END_WATCHER (periodic, periodic)
212 #endif 248 #endif
213 249
214 EV_DECLARE_WATCHER (idle, idle)
215 };
216
217 EV_DECLARE_WATCHER (prepare, prepare)
218 };
219
220 EV_DECLARE_WATCHER (check, check)
221 };
222
223 EV_DECLARE_WATCHER (sig, signal) 250 EV_BEGIN_WATCHER (sig, signal)
224 void set (int signum) 251 void set (int signum)
225 { 252 {
226 int active = is_active (); 253 int active = is_active ();
227 if (active) stop (); 254 if (active) stop ();
228 ev_signal_set (static_cast<ev_signal *>(this), signum); 255 ev_signal_set (static_cast<ev_signal *>(this), signum);
232 void start (int signum) 259 void start (int signum)
233 { 260 {
234 set (signum); 261 set (signum);
235 start (); 262 start ();
236 } 263 }
237 }; 264 EV_END_WATCHER (sig, signal)
238 265
239 EV_DECLARE_WATCHER (child, child) 266 EV_BEGIN_WATCHER (child, child)
240 void set (int pid) 267 void set (int pid)
241 { 268 {
242 int active = is_active (); 269 int active = is_active ();
243 if (active) stop (); 270 if (active) stop ();
244 ev_child_set (static_cast<ev_child *>(this), pid); 271 ev_child_set (static_cast<ev_child *>(this), pid);
248 void start (int pid) 275 void start (int pid)
249 { 276 {
250 set (pid); 277 set (pid);
251 start (); 278 start ();
252 } 279 }
253 }; 280 EV_END_WATCHER (child, child)
281
282 #if EV_STAT_ENABLE
283 EV_BEGIN_WATCHER (stat, stat)
284 void set (const char *path, ev_tstamp interval = 0.)
285 {
286 int active = is_active ();
287 if (active) stop ();
288 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
289 if (active) start ();
290 }
291
292 void start (const char *path, ev_tstamp interval = 0.)
293 {
294 stop ();
295 set (path, interval);
296 start ();
297 }
298
299 void update ()
300 {
301 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
302 }
303 EV_END_WATCHER (stat, stat)
304 #endif
305
306 EV_BEGIN_WATCHER (idle, idle)
307 void set () { }
308 EV_END_WATCHER (idle, idle)
309
310 EV_BEGIN_WATCHER (prepare, prepare)
311 void set () { }
312 EV_END_WATCHER (prepare, prepare)
313
314 EV_BEGIN_WATCHER (check, check)
315 void set () { }
316 EV_END_WATCHER (check, check)
317
318 #if EV_EMBED_ENABLE
319 EV_BEGIN_WATCHER (embed, embed)
320 void start (struct ev_loop *embedded_loop)
321 {
322 stop ();
323 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
324 start ();
325 }
326
327 void sweep ()
328 {
329 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
330 }
331 EV_END_WATCHER (embed, embed)
332 #endif
333
334 #if EV_FORK_ENABLE
335 EV_BEGIN_WATCHER (fork, fork)
336 void set () { }
337 EV_END_WATCHER (fork, fork)
338 #endif
254 339
255 #undef EV_CONSTRUCT 340 #undef EV_CONSTRUCT
341 #undef EV_BEGIN_WATCHER
256 #undef EV_DECLARE_WATCHER 342 #undef EV_END_WATCHER
257} 343}
258 344
259#endif 345#endif
260 346

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines