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.19 by root, Fri Dec 14 17:47:52 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines