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

Comparing libev/ev++.h (file contents):
Revision 1.12 by root, Thu Nov 29 17:36:35 2007 UTC vs.
Revision 1.13 by root, Tue Dec 4 16:23:29 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 }
24
25 void set_ (void *object, void (*cb)(ev_watcher *w, int revents))
26 {
27 this->data = object;
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_watcher *w, int revents)
39 {
40 watcher *self = static_cast<watcher *>(w);
41 K *obj = static_cast<K *>(self->data);
30 (o->*m) (*w, revents); 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));
31 } 85 }
32 }; 86 };
33 87
34 enum { 88 enum {
35 UNDEF = EV_UNDEF, 89 UNDEF = EV_UNDEF,
55 { 109 {
56 return ev_now (EV_A); 110 return ev_now (EV_A);
57 } 111 }
58 112
59 #if EV_MULTIPLICITY 113 #if EV_MULTIPLICITY
60
61 #define EV_CONSTRUCT(cppstem) \
62 EV_P; \ 114 #define EV_CONSTRUCT \
63 \
64 void set (EV_P) \ 115 (EV_P = EV_DEFAULT) \
65 { \ 116 { \
66 this->EV_A = EV_A; \
67 } \
68 \ 117 set (EV_A); \
69 template<class O1, class O2> \ 118 }
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 119 #else
74 120 #define EV_CONSTRUCT \
75 #define EV_CONSTRUCT(cppstem) \ 121 () \
76 template<class O1, class O2> \ 122 { \
77 explicit cppstem (O1 *object, void (O2::*method)(cppstem &, int)) \ 123 }
78 : callback<cppstem> (object, method)
79
80 #endif 124 #endif
81 125
82 /* using a template here would require quite a bit more lines, 126 /* using a template here would require quite a bit more lines,
83 * so a macro solution was chosen */ 127 * so a macro solution was chosen */
84 #define EV_BEGIN_WATCHER(cppstem,cstem) \ 128 #define EV_BEGIN_WATCHER(cppstem,cstem) \
85 \ 129 \
86 struct cppstem : ev_ ## cstem, callback<cppstem> \ 130 struct cppstem : base<ev_ ## cstem, cppstem> \
87 { \ 131 { \
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 () \ 132 void start () \
104 { \ 133 { \
105 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 134 ev_ ## cstem ## _start (EV_A_ static_cast<ev_ ## cstem *>(this)); \
106 } \ 135 } \
107 \ 136 \
108 void stop () \ 137 void stop () \
109 { \ 138 { \
110 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \ 139 ev_ ## cstem ## _stop (EV_A_ static_cast<ev_ ## cstem *>(this)); \
111 } \ 140 } \
112 \ 141 \
113 void operator ()(int events = EV_UNDEF) \
114 { \ 142 cppstem EV_CONSTRUCT \
115 return call (this, events); \
116 } \
117 \ 143 \
118 ~cppstem () \ 144 ~cppstem () \
119 { \ 145 { \
120 stop (); \ 146 stop (); \
121 } \ 147 } \
122 \ 148 \
149 using base<ev_ ## cstem, cppstem>::set; \
150 \
123 private: \ 151 private: \
124 \ 152 \
125 cppstem (const cppstem &o) \ 153 cppstem (const cppstem &o) \
126 : callback<cppstem> (this, (void (cppstem::*)(cppstem &, int))0) \
127 { /* disabled */ } \ 154 { /* disabled */ } \
128 \ 155 \
129 void operator =(const cppstem &o) { /* disabled */ } \ 156 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 \ 157 \
136 public: 158 public:
137 159
138 #define EV_END_WATCHER(cppstem,cstem) \ 160 #define EV_END_WATCHER(cppstem,cstem) \
139 }; 161 };

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines