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

Comparing libev/ev++.h (file contents):
Revision 1.11 by root, Tue Nov 27 22:31:52 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#ifdef EV_H
5# include EV_H
6#else
4#include "ev.h" 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 klass; // it is vital that this is never defined 15 #if EV_MULTIPLICITY
16 EV_P;
12 17
13 klass *o; 18 void set (EV_P)
14 void (klass::*m)(watcher &, int);
15
16 public:
17 template<class O1, class O2>
18 explicit callback (O1 *object, void (O2::*method)(watcher &, int))
19 { 19 {
20 o = reinterpret_cast<klass *>(object); 20 this->EV_A = EV_A;
21 m = reinterpret_cast<void (klass::*)(watcher &, int)>(method);
22 } 21 }
22 #endif
23 23
24 // this works because a standards-compliant C++ compiler 24 base ()
25 // basically can't help it: it doesn't have the knowledge 25 {
26 // required to miscompile (klass is not defined anywhere 26 ev_init (this, 0);
27 // and nothing is known about the constructor arguments) :)
28 void call (watcher *w, int revents)
29 { 27 }
30 (o->*m) (*w, revents); 28
29 void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents))
30 {
31 this->data = data;
32 ev_set_cb (static_cast<ev_watcher *>(this), cb);
33 }
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));
31 } 104 }
32 }; 105 };
33 106
34 enum { 107 enum {
35 UNDEF = EV_UNDEF, 108 UNDEF = EV_UNDEF,
55 { 128 {
56 return ev_now (EV_A); 129 return ev_now (EV_A);
57 } 130 }
58 131
59 #if EV_MULTIPLICITY 132 #if EV_MULTIPLICITY
60
61 #define EV_CONSTRUCT(cppstem) \
62 EV_P; \ 133 #define EV_CONSTRUCT \
63 \
64 void set (EV_P) \ 134 (EV_P = EV_DEFAULT) \
65 { \ 135 { \
66 this->EV_A = EV_A; \
67 } \
68 \ 136 set (EV_A); \
69 template<class O1, class O2> \ 137 }
70 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int), EV_P = ev_default_loop (0)) \
71 : callback<cppstem> (object, method), EV_A (EV_A)
72
73 #else 138 #else
74 139 #define EV_CONSTRUCT \
75 #define EV_CONSTRUCT(cppstem) \ 140 () \
76 template<class O1, class O2> \ 141 { \
77 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 142 }
78 : callback<cppstem> (object, method)
79
80 #endif 143 #endif
81 144
82 /* using a template here would require quite a bit more lines, 145 /* using a template here would require quite a bit more lines,
83 * so a macro solution was chosen */ 146 * so a macro solution was chosen */
84 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 147 #define EV_BEGIN_WATCHER(cppstem,cstem) \
85 \ 148 \
86 struct cppstem : ev_ ## cstem, callback<cppstem> \ 149 struct cppstem : base<ev_ ## cstem, cppstem> \
87 { \ 150 { \
88 EV_CONSTRUCT (cppstem) \
89 { \
90 ev_init (static_cast<ev_ ## cstem *>(this), thunk); \
91 } \
92 \
93 bool is_active () const \
94 { \
95 return ev_is_active (static_cast<const ev_ ## cstem *>(this)); \
96 } \
97 \
98 bool is_pending () const \
99 { \
100 return ev_is_pending (static_cast<const ev_ ## cstem *>(this)); \
101 } \
102 \
103 void start () \ 151 void start () \
104 { \ 152 { \
105 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 153 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
106 } \ 154 } \
107 \ 155 \
108 void stop () \ 156 void stop () \
109 { \ 157 { \
110 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 158 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
111 } \ 159 } \
112 \ 160 \
113 void operator ()(int events = EV_UNDEF) \
114 { \ 161 cppstem EV_CONSTRUCT \
115 return call (this, events); \
116 } \
117 \ 162 \
118 ~cppstem () \ 163 ~cppstem () \
119 { \ 164 { \
120 stop (); \ 165 stop (); \
121 } \ 166 } \
122 \ 167 \
168 using base<ev_ ## cstem, cppstem>::set; \
169 \
123 private: \ 170 private: \
124 \ 171 \
125 cppstem (const cppstem &o) \ 172 cppstem (const cppstem &o) \
126 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
127 { /* disabled */ } \ 173 { /* disabled */ } \
128 \ 174 \
129 void operator =(const cppstem &o) { /* disabled */ } \ 175 void operator =(const cppstem &o) { /* disabled */ } \
130 \
131 static void thunk (EV_P_ struct ev_ ## cstem *w, int revents) \
132 { \
133 (*static_cast<cppstem *>(w))(revents); \
134 } \
135 \ 176 \
136 public: 177 public:
137 178
138 #define EV_END_WATCHER(cppstem,cstem) \ 179 #define EV_END_WATCHER(cppstem,cstem) \
139 }; 180 };
248 if (active) start (); 289 if (active) start ();
249 } 290 }
250 291
251 void start (const char *path, ev_tstamp interval = 0.) 292 void start (const char *path, ev_tstamp interval = 0.)
252 { 293 {
294 stop ();
253 set (path, interval); 295 set (path, interval);
254 start (); 296 start ();
255 } 297 }
256 298
257 void update () 299 void update ()
273 void set () { } 315 void set () { }
274 EV_END_WATCHER (check, check) 316 EV_END_WATCHER (check, check)
275 317
276 #if EV_EMBED_ENABLE 318 #if EV_EMBED_ENABLE
277 EV_BEGIN_WATCHER (embed, embed) 319 EV_BEGIN_WATCHER (embed, embed)
278 void set (struct ev_loop *loop)
279 {
280 int active = is_active ();
281 if (active) stop ();
282 ev_embed_set (static_cast<ev_embed *>(this), loop);
283 if (active) start ();
284 }
285
286 void start (struct ev_loop *embedded_loop) 320 void start (struct ev_loop *embedded_loop)
287 { 321 {
288 set (embedded_loop); 322 stop ();
323 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
289 start (); 324 start ();
290 } 325 }
291 326
292 void sweep () 327 void sweep ()
293 { 328 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines