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.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 106
47 #include "ev.h" 107 enum {
108 UNDEF = EV_UNDEF,
109 NONE = EV_NONE,
110 READ = EV_READ,
111 WRITE = EV_WRITE,
112 TIMEOUT = EV_TIMEOUT,
113 PERIODIC = EV_PERIODIC,
114 SIGNAL = EV_SIGNAL,
115 CHILD = EV_CHILD,
116 STAT = EV_STAT,
117 IDLE = EV_IDLE,
118 CHECK = EV_CHECK,
119 PREPARE = EV_PREPARE,
120 FORK = EV_FORK,
121 EMBED = EV_EMBED,
122 ERROR = EV_ERROR,
123 };
48 124
49 typedef ev_tstamp tstamp; 125 typedef ev_tstamp tstamp;
50 126
51 inline ev_tstamp now (EV_P) 127 inline ev_tstamp now (EV_P)
52 { 128 {
53 return ev_now (EV_A); 129 return ev_now (EV_A);
54 } 130 }
55 131
56 #if EV_MULTIPLICITY 132 #if EV_MULTIPLICITY
57
58 #define EV_CONSTRUCT(cppstem) \
59 EV_P; \ 133 #define EV_CONSTRUCT \
60 \
61 void set (EV_P) \ 134 (EV_P = EV_DEFAULT) \
62 { \ 135 { \
63 this->EV_A = EV_A; \
64 } \
65 \ 136 set (EV_A); \
66 template<class O1, class O2> \ 137 }
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 138 #else
71 139 #define EV_CONSTRUCT \
72 #define EV_CONSTRUCT(cppstem) \ 140 () \
73 template<class O1, class O2> \ 141 { \
74 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 142 }
75 : callback<cppstem> (object, method)
76
77 #endif 143 #endif
78 144
79 /* using a template here would require quite a bit more lines, 145 /* using a template here would require quite a bit more lines,
80 * so a macro solution was chosen */ 146 * so a macro solution was chosen */
81 #define EV_DECLARE_WATCHER(cppstem,cstem) \ 147 #define EV_BEGIN_WATCHER(cppstem,cstem) \
82 \ 148 \
83 extern "C" void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \
84 \
85 struct cppstem : ev_ ## cstem, callback<cppstem> \ 149 struct cppstem : base<ev_ ## cstem, cppstem> \
86 { \ 150 { \
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 () \ 151 void start () \
103 { \ 152 { \
104 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 153 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
105 } \ 154 } \
106 \ 155 \
107 void stop () \ 156 void stop () \
108 { \ 157 { \
109 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 158 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
110 } \ 159 } \
111 \ 160 \
112 void operator ()(int events = EV_UNDEF) \ 161 cppstem EV_CONSTRUCT \
162 \
163 ~cppstem () \
113 { \ 164 { \
114 return call (this, events); \ 165 stop (); \
115 } \ 166 } \
116 \ 167 \
168 using base<ev_ ## cstem, cppstem>::set; \
169 \
117 private: \ 170 private: \
118 \ 171 \
119 cppstem (const cppstem &o) \ 172 cppstem (const cppstem &o) \
120 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
121 { /* disabled */ } \ 173 { /* disabled */ } \
174 \
122 void operator =(const cppstem &o) { /* disabled */ } \ 175 void operator =(const cppstem &o) { /* disabled */ } \
123 \ 176 \
124 public: 177 public:
125 178
179 #define EV_END_WATCHER(cppstem,cstem) \
180 };
181
126 EV_DECLARE_WATCHER (io, io) 182 EV_BEGIN_WATCHER (io, io)
127 void set (int fd, int events) 183 void set (int fd, int events)
128 { 184 {
129 int active = is_active (); 185 int active = is_active ();
130 if (active) stop (); 186 if (active) stop ();
131 ev_io_set (static_cast<ev_io *>(this), fd, events); 187 ev_io_set (static_cast<ev_io *>(this), fd, events);
143 void start (int fd, int events) 199 void start (int fd, int events)
144 { 200 {
145 set (fd, events); 201 set (fd, events);
146 start (); 202 start ();
147 } 203 }
148 }; 204 EV_END_WATCHER (io, io)
149 205
150 EV_DECLARE_WATCHER (timer, timer) 206 EV_BEGIN_WATCHER (timer, timer)
151 void set (ev_tstamp after, ev_tstamp repeat = 0.) 207 void set (ev_tstamp after, ev_tstamp repeat = 0.)
152 { 208 {
153 int active = is_active (); 209 int active = is_active ();
154 if (active) stop (); 210 if (active) stop ();
155 ev_timer_set (static_cast<ev_timer *>(this), after, repeat); 211 ev_timer_set (static_cast<ev_timer *>(this), after, repeat);
164 220
165 void again () 221 void again ()
166 { 222 {
167 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 223 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
168 } 224 }
169 }; 225 EV_END_WATCHER (timer, timer)
170 226
227 #if EV_PERIODIC_ENABLE
171 EV_DECLARE_WATCHER (periodic, periodic) 228 EV_BEGIN_WATCHER (periodic, periodic)
172 void set (ev_tstamp at, ev_tstamp interval = 0.) 229 void set (ev_tstamp at, ev_tstamp interval = 0.)
173 { 230 {
174 int active = is_active (); 231 int active = is_active ();
175 if (active) stop (); 232 if (active) stop ();
176 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0); 233 ev_periodic_set (static_cast<ev_periodic *>(this), at, interval, 0);
185 242
186 void again () 243 void again ()
187 { 244 {
188 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 245 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
189 } 246 }
190 }; 247 EV_END_WATCHER (periodic, periodic)
248 #endif
191 249
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) 250 EV_BEGIN_WATCHER (sig, signal)
202 void set (int signum) 251 void set (int signum)
203 { 252 {
204 int active = is_active (); 253 int active = is_active ();
205 if (active) stop (); 254 if (active) stop ();
206 ev_signal_set (static_cast<ev_signal *>(this), signum); 255 ev_signal_set (static_cast<ev_signal *>(this), signum);
210 void start (int signum) 259 void start (int signum)
211 { 260 {
212 set (signum); 261 set (signum);
213 start (); 262 start ();
214 } 263 }
215 }; 264 EV_END_WATCHER (sig, signal)
216 265
217 EV_DECLARE_WATCHER (child, child) 266 EV_BEGIN_WATCHER (child, child)
218 void set (int pid) 267 void set (int pid)
219 { 268 {
220 int active = is_active (); 269 int active = is_active ();
221 if (active) stop (); 270 if (active) stop ();
222 ev_child_set (static_cast<ev_child *>(this), pid); 271 ev_child_set (static_cast<ev_child *>(this), pid);
226 void start (int pid) 275 void start (int pid)
227 { 276 {
228 set (pid); 277 set (pid);
229 start (); 278 start ();
230 } 279 }
231 }; 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
232 339
233 #undef EV_CONSTRUCT 340 #undef EV_CONSTRUCT
341 #undef EV_BEGIN_WATCHER
234 #undef EV_DECLARE_WATCHER 342 #undef EV_END_WATCHER
235} 343}
236 344
237#endif 345#endif
238 346

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines