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

Comparing libev/ev++.h (file contents):
Revision 1.4 by root, Sun Nov 11 16:58:25 2007 UTC vs.
Revision 1.13 by root, Tue Dec 4 16:23:29 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 *object, void (*cb)(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 } 26 {
40 27 this->data = object;
41 void call (watcher *w, int e) const 28 ev_set_cb (static_cast<ev_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_watcher *w, int revents)
39 {
40 watcher *self = static_cast<watcher *>(w);
41 K *obj = static_cast<K *>(self->data);
42 (obj->*method) (*self, revents);
43 }
44
45 template<class K, void (K::*method)(watcher &w, int) const>
46 void set (const K *object)
47 {
48 set_ (object, const_method_thunk<K, method>);
49 }
50
51 template<class K, void (K::*method)(watcher &w, int) const>
52 static void const_method_thunk (ev_watcher *w, int revents)
53 {
54 watcher *self = static_cast<watcher *>(w);
55 K *obj = static_cast<K *>(self->data);
56 (obj->*method) (*self, revents);
57 }
58
59 template<void (*function)(watcher &w, int)>
60 void set ()
61 {
62 set_ (0, function_thunk<function>);
63 }
64
65 template<void (*function)(watcher &w, int)>
66 static void function_thunk (ev_watcher *w, int revents)
67 {
68 watcher *self = static_cast<watcher *>(w);
69 function (*self, revents);
70 }
71
72 void operator ()(int events = EV_UNDEF)
73 {
74 return e_cb (static_cast<ev_watcher *>(this)) (events);
75 }
76
77 bool is_active () const
78 {
79 return ev_is_active (static_cast<const ev_watcher *>(this));
80 }
81
82 bool is_pending () const
83 {
84 return ev_is_pending (static_cast<const ev_watcher *>(this));
44 } 85 }
45 }; 86 };
46
47 #include "ev.h"
48 87
49 enum { 88 enum {
50 UNDEF = EV_UNDEF, 89 UNDEF = EV_UNDEF,
51 NONE = EV_NONE, 90 NONE = EV_NONE,
52 READ = EV_READ, 91 READ = EV_READ,
53 WRITE = EV_WRITE, 92 WRITE = EV_WRITE,
54 TIMEOUT = EV_TIMEOUT, 93 TIMEOUT = EV_TIMEOUT,
55 PERIODIC = EV_PERIODIC, 94 PERIODIC = EV_PERIODIC,
56 SIGNAL = EV_SIGNAL, 95 SIGNAL = EV_SIGNAL,
96 CHILD = EV_CHILD,
97 STAT = EV_STAT,
57 IDLE = EV_IDLE, 98 IDLE = EV_IDLE,
58 CHECK = EV_CHECK, 99 CHECK = EV_CHECK,
59 PREPARE = EV_PREPARE, 100 PREPARE = EV_PREPARE,
60 CHILD = EV_CHILD, 101 FORK = EV_FORK,
102 EMBED = EV_EMBED,
61 ERROR = EV_ERROR, 103 ERROR = EV_ERROR,
62 }; 104 };
63 105
64 typedef ev_tstamp tstamp; 106 typedef ev_tstamp tstamp;
65 107
67 { 109 {
68 return ev_now (EV_A); 110 return ev_now (EV_A);
69 } 111 }
70 112
71 #if EV_MULTIPLICITY 113 #if EV_MULTIPLICITY
72
73 #define EV_CONSTRUCT(cppstem) \
74 EV_P; \ 114 #define EV_CONSTRUCT \
75 \
76 void set (EV_P) \ 115 (EV_P = EV_DEFAULT) \
77 { \ 116 { \
78 this->EV_A = EV_A; \
79 } \
80 \ 117 set (EV_A); \
81 template<class O1, class O2> \ 118 }
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 119 #else
86 120 #define EV_CONSTRUCT \
87 #define EV_CONSTRUCT(cppstem) \ 121 () \
88 template<class O1, class O2> \ 122 { \
89 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 123 }
90 : callback<cppstem> (object, method)
91
92 #endif 124 #endif
93 125
94 /* using a template here would require quite a bit more lines, 126 /* using a template here would require quite a bit more lines,
95 * so a macro solution was chosen */ 127 * so a macro solution was chosen */
96 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 128 #define EV_BEGIN_WATCHER(cppstem,cstem) \
97 \ 129 \
98 static void cb_ ## cppstem (struct ev_ ## cstem *w, int revents); \
99 \
100 struct cppstem : ev_ ## cstem, callback<cppstem> \ 130 struct cppstem : base<ev_ ## cstem, cppstem> \
101 { \ 131 { \
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 () \ 132 void start () \
118 { \ 133 { \
119 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 134 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
120 } \ 135 } \
121 \ 136 \
122 void stop () \ 137 void stop () \
123 { \ 138 { \
124 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 139 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
125 } \ 140 } \
126 \ 141 \
127 void operator ()(int events = EV_UNDEF) \
128 { \ 142 cppstem EV_CONSTRUCT \
129 return call (this, events); \
130 } \
131 \ 143 \
132 ~cppstem () \ 144 ~cppstem () \
133 { \ 145 { \
134 stop (); \ 146 stop (); \
135 } \ 147 } \
136 \ 148 \
149 using base<ev_ ## cstem, cppstem>::set; \
150 \
137 private: \ 151 private: \
138 \ 152 \
139 cppstem (const cppstem &o) \ 153 cppstem (const cppstem &o) \
140 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
141 { /* disabled */ } \ 154 { /* disabled */ } \
155 \
142 void operator =(const cppstem &o) { /* disabled */ } \ 156 void operator =(const cppstem &o) { /* disabled */ } \
143 \ 157 \
144 public: 158 public:
145 159
146 #define EV_END_WATCHER(cppstem,cstem) \ 160 #define EV_END_WATCHER(cppstem,cstem) \
147 }; \
148 \
149 static void cb_ ## cppstem (struct ev_ ## cstem *w, int revents) \
150 { \
151 (*static_cast<cppstem *>(w))(revents); \
152 } 161 };
153 162
154 EV_BEGIN_WATCHER (io, io) 163 EV_BEGIN_WATCHER (io, io)
155 void set (int fd, int events) 164 void set (int fd, int events)
156 { 165 {
157 int active = is_active (); 166 int active = is_active ();
194 { 203 {
195 ev_timer_again (EV_A_ static_cast<ev_timer *>(this)); 204 ev_timer_again (EV_A_ static_cast<ev_timer *>(this));
196 } 205 }
197 EV_END_WATCHER (timer, timer) 206 EV_END_WATCHER (timer, timer)
198 207
199 #if EV_PERIODICS 208 #if EV_PERIODIC_ENABLE
200 EV_BEGIN_WATCHER (periodic, periodic) 209 EV_BEGIN_WATCHER (periodic, periodic)
201 void set (ev_tstamp at, ev_tstamp interval = 0.) 210 void set (ev_tstamp at, ev_tstamp interval = 0.)
202 { 211 {
203 int active = is_active (); 212 int active = is_active ();
204 if (active) stop (); 213 if (active) stop ();
217 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this)); 226 ev_periodic_again (EV_A_ static_cast<ev_periodic *>(this));
218 } 227 }
219 EV_END_WATCHER (periodic, periodic) 228 EV_END_WATCHER (periodic, periodic)
220 #endif 229 #endif
221 230
222 EV_BEGIN_WATCHER (idle, idle)
223 EV_END_WATCHER (idle, idle)
224
225 EV_BEGIN_WATCHER (prepare, prepare)
226 EV_END_WATCHER (prepare, prepare)
227
228 EV_BEGIN_WATCHER (check, check)
229 EV_END_WATCHER (check, check)
230
231 EV_BEGIN_WATCHER (sig, signal) 231 EV_BEGIN_WATCHER (sig, signal)
232 void set (int signum) 232 void set (int signum)
233 { 233 {
234 int active = is_active (); 234 int active = is_active ();
235 if (active) stop (); 235 if (active) stop ();
258 set (pid); 258 set (pid);
259 start (); 259 start ();
260 } 260 }
261 EV_END_WATCHER (child, child) 261 EV_END_WATCHER (child, child)
262 262
263 #if EV_STAT_ENABLE
264 EV_BEGIN_WATCHER (stat, stat)
265 void set (const char *path, ev_tstamp interval = 0.)
266 {
267 int active = is_active ();
268 if (active) stop ();
269 ev_stat_set (static_cast<ev_stat *>(this), path, interval);
270 if (active) start ();
271 }
272
273 void start (const char *path, ev_tstamp interval = 0.)
274 {
275 stop ();
276 set (path, interval);
277 start ();
278 }
279
280 void update ()
281 {
282 ev_stat_stat (EV_A_ static_cast<ev_stat *>(this));
283 }
284 EV_END_WATCHER (stat, stat)
285 #endif
286
287 EV_BEGIN_WATCHER (idle, idle)
288 void set () { }
289 EV_END_WATCHER (idle, idle)
290
291 EV_BEGIN_WATCHER (prepare, prepare)
292 void set () { }
293 EV_END_WATCHER (prepare, prepare)
294
295 EV_BEGIN_WATCHER (check, check)
296 void set () { }
297 EV_END_WATCHER (check, check)
298
299 #if EV_EMBED_ENABLE
300 EV_BEGIN_WATCHER (embed, embed)
301 void start (struct ev_loop *embedded_loop)
302 {
303 stop ();
304 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
305 start ();
306 }
307
308 void sweep ()
309 {
310 ev_embed_sweep (EV_A_ static_cast<ev_embed *>(this));
311 }
312 EV_END_WATCHER (embed, embed)
313 #endif
314
315 #if EV_FORK_ENABLE
316 EV_BEGIN_WATCHER (fork, fork)
317 void set () { }
318 EV_END_WATCHER (fork, fork)
319 #endif
320
263 #undef EV_CONSTRUCT 321 #undef EV_CONSTRUCT
264 #undef EV_BEGIN_WATCHER 322 #undef EV_BEGIN_WATCHER
323 #undef EV_END_WATCHER
265} 324}
266 325
267#endif 326#endif
268 327

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines