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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines