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.18 by root, Tue Dec 11 03:18:33 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 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_ ev_watcher *w, int revents)
39 {
40 K *obj = static_cast<K *>(w->data);
41 (obj->*method) (*static_cast<watcher *>(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_ ev_watcher *w, int revents)
52 {
53 K *obj = static_cast<K *>(w->data);
54 (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(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_ ev_watcher *w, int revents)
65 {
66 function (*static_cast<watcher *>(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));
31 } 83 }
32 }; 84 };
33 85
34 enum { 86 enum {
35 UNDEF = EV_UNDEF, 87 UNDEF = EV_UNDEF,
55 { 107 {
56 return ev_now (EV_A); 108 return ev_now (EV_A);
57 } 109 }
58 110
59 #if EV_MULTIPLICITY 111 #if EV_MULTIPLICITY
60
61 #define EV_CONSTRUCT(cppstem) \
62 EV_P; \ 112 #define EV_CONSTRUCT \
63 \
64 void set (EV_P) \ 113 (EV_P = EV_DEFAULT) \
65 { \ 114 { \
66 this->EV_A = EV_A; \
67 } \
68 \ 115 set (EV_A); \
69 template<class O1, class O2> \ 116 }
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 117 #else
74 118 #define EV_CONSTRUCT \
75 #define EV_CONSTRUCT(cppstem) \ 119 () \
76 template<class O1, class O2> \ 120 { \
77 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 121 }
78 : callback<cppstem> (object, method)
79
80 #endif 122 #endif
81 123
82 /* using a template here would require quite a bit more lines, 124 /* using a template here would require quite a bit more lines,
83 * so a macro solution was chosen */ 125 * so a macro solution was chosen */
84 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 126 #define EV_BEGIN_WATCHER(cppstem,cstem) \
85 \ 127 \
86 struct cppstem : ev_ ## cstem, callback<cppstem> \ 128 struct cppstem : base<ev_ ## cstem, cppstem> \
87 { \ 129 { \
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 () \ 130 void start () \
104 { \ 131 { \
105 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 132 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
106 } \ 133 } \
107 \ 134 \
108 void stop () \ 135 void stop () \
109 { \ 136 { \
110 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 137 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
111 } \ 138 } \
112 \ 139 \
113 void operator ()(int events = EV_UNDEF) \
114 { \ 140 cppstem EV_CONSTRUCT \
115 return call (this, events); \
116 } \
117 \ 141 \
118 ~cppstem () \ 142 ~cppstem () \
119 { \ 143 { \
120 stop (); \ 144 stop (); \
121 } \ 145 } \
122 \ 146 \
147 using base<ev_ ## cstem, cppstem>::set; \
148 \
123 private: \ 149 private: \
124 \ 150 \
125 cppstem (const cppstem &o) \ 151 cppstem (const cppstem &o) \
126 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
127 { /* disabled */ } \ 152 { /* disabled */ } \
128 \ 153 \
129 void operator =(const cppstem &o) { /* disabled */ } \ 154 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 \ 155 \
136 public: 156 public:
137 157
138 #define EV_END_WATCHER(cppstem,cstem) \ 158 #define EV_END_WATCHER(cppstem,cstem) \
139 }; 159 };
248 if (active) start (); 268 if (active) start ();
249 } 269 }
250 270
251 void start (const char *path, ev_tstamp interval = 0.) 271 void start (const char *path, ev_tstamp interval = 0.)
252 { 272 {
273 stop ();
253 set (path, interval); 274 set (path, interval);
254 start (); 275 start ();
255 } 276 }
256 277
257 void update () 278 void update ()
273 void set () { } 294 void set () { }
274 EV_END_WATCHER (check, check) 295 EV_END_WATCHER (check, check)
275 296
276 #if EV_EMBED_ENABLE 297 #if EV_EMBED_ENABLE
277 EV_BEGIN_WATCHER (embed, embed) 298 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) 299 void start (struct ev_loop *embedded_loop)
287 { 300 {
288 set (embedded_loop); 301 stop ();
302 ev_embed_set (static_cast<ev_embed *>(this), embedded_loop);
289 start (); 303 start ();
290 } 304 }
291 305
292 void sweep () 306 void sweep ()
293 { 307 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines