… | |
… | |
46 | # include "ev.h" |
46 | # include "ev.h" |
47 | #endif |
47 | #endif |
48 | |
48 | |
49 | namespace ev { |
49 | namespace ev { |
50 | |
50 | |
51 | template<class ev_watcher, class watcher> |
51 | typedef ev_tstamp tstamp; |
52 | struct base : ev_watcher |
|
|
53 | { |
|
|
54 | #if EV_MULTIPLICITY |
|
|
55 | EV_P; |
|
|
56 | |
|
|
57 | void set (EV_P) |
|
|
58 | { |
|
|
59 | this->EV_A = EV_A; |
|
|
60 | } |
|
|
61 | #endif |
|
|
62 | |
|
|
63 | base () |
|
|
64 | { |
|
|
65 | ev_init (this, 0); |
|
|
66 | } |
|
|
67 | |
|
|
68 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
|
|
69 | { |
|
|
70 | this->data = data; |
|
|
71 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
72 | } |
|
|
73 | |
|
|
74 | // method callback |
|
|
75 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
76 | void set (K *object) |
|
|
77 | { |
|
|
78 | set_ (object, method_thunk<K, method>); |
|
|
79 | } |
|
|
80 | |
|
|
81 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
82 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
83 | { |
|
|
84 | K *obj = static_cast<K *>(w->data); |
|
|
85 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
86 | } |
|
|
87 | |
|
|
88 | // const method callback |
|
|
89 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
90 | void set (const K *object) |
|
|
91 | { |
|
|
92 | set_ (object, const_method_thunk<K, method>); |
|
|
93 | } |
|
|
94 | |
|
|
95 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
96 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
97 | { |
|
|
98 | K *obj = static_cast<K *>(w->data); |
|
|
99 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
100 | } |
|
|
101 | |
|
|
102 | // function callback |
|
|
103 | template<void (*function)(watcher &w, int)> |
|
|
104 | void set (void *data = 0) |
|
|
105 | { |
|
|
106 | set_ (data, function_thunk<function>); |
|
|
107 | } |
|
|
108 | |
|
|
109 | template<void (*function)(watcher &w, int)> |
|
|
110 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
111 | { |
|
|
112 | function (*static_cast<watcher *>(w), revents); |
|
|
113 | } |
|
|
114 | |
|
|
115 | // simple callback |
|
|
116 | template<class K, void (K::*method)()> |
|
|
117 | void set (K *object) |
|
|
118 | { |
|
|
119 | set_ (object, method_noargs_thunk<K, method>); |
|
|
120 | } |
|
|
121 | |
|
|
122 | template<class K, void (K::*method)()> |
|
|
123 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
124 | { |
|
|
125 | K *obj = static_cast<K *>(w->data); |
|
|
126 | (obj->*method) (); |
|
|
127 | } |
|
|
128 | |
|
|
129 | void operator ()(int events = EV_UNDEF) |
|
|
130 | { |
|
|
131 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
132 | (static_cast<ev_watcher *>(this), events); |
|
|
133 | } |
|
|
134 | |
|
|
135 | bool is_active () const |
|
|
136 | { |
|
|
137 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
138 | } |
|
|
139 | |
|
|
140 | bool is_pending () const |
|
|
141 | { |
|
|
142 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
|
|
143 | } |
|
|
144 | }; |
|
|
145 | |
52 | |
146 | enum { |
53 | enum { |
147 | UNDEF = EV_UNDEF, |
54 | UNDEF = EV_UNDEF, |
148 | NONE = EV_NONE, |
55 | NONE = EV_NONE, |
149 | READ = EV_READ, |
56 | READ = EV_READ, |
… | |
… | |
159 | FORK = EV_FORK, |
66 | FORK = EV_FORK, |
160 | EMBED = EV_EMBED, |
67 | EMBED = EV_EMBED, |
161 | ERROR = EV_ERROR, |
68 | ERROR = EV_ERROR, |
162 | }; |
69 | }; |
163 | |
70 | |
164 | typedef ev_tstamp tstamp; |
71 | enum |
|
|
72 | { |
|
|
73 | AUTO = EVFLAG_AUTO, |
|
|
74 | NOENV = EVFLAG_NOENV, |
|
|
75 | FORKCHECK = EVFLAG_FORKCHECK, |
|
|
76 | SELECT = EVBACKEND_SELECT, |
|
|
77 | POLL = EVBACKEND_POLL, |
|
|
78 | EPOLL = EVBACKEND_EPOLL, |
|
|
79 | KQUEUE = EVBACKEND_KQUEUE, |
|
|
80 | DEVPOLL = EVBACKEND_DEVPOLL, |
|
|
81 | PORT = EVBACKEND_PORT |
|
|
82 | }; |
|
|
83 | |
|
|
84 | enum |
|
|
85 | { |
|
|
86 | NONBLOCK = EVLOOP_NONBLOCK, |
|
|
87 | ONESHOT = EVLOOP_ONESHOT |
|
|
88 | }; |
|
|
89 | |
|
|
90 | enum how_t |
|
|
91 | { |
|
|
92 | ONE = EVUNLOOP_ONE, |
|
|
93 | ALL = EVUNLOOP_ALL |
|
|
94 | }; |
|
|
95 | |
|
|
96 | template<class ev_watcher, class watcher> |
|
|
97 | struct base : ev_watcher |
|
|
98 | { |
|
|
99 | #if EV_MULTIPLICITY |
|
|
100 | EV_P; |
|
|
101 | |
|
|
102 | void set (EV_P) |
|
|
103 | { |
|
|
104 | this->EV_A = EV_A; |
|
|
105 | } |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | base () |
|
|
109 | { |
|
|
110 | ev_init (this, 0); |
|
|
111 | } |
|
|
112 | |
|
|
113 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
|
|
114 | { |
|
|
115 | this->data = data; |
|
|
116 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
117 | } |
|
|
118 | |
|
|
119 | // method callback |
|
|
120 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
121 | void set (K *object) |
|
|
122 | { |
|
|
123 | set_ (object, method_thunk<K, method>); |
|
|
124 | } |
|
|
125 | |
|
|
126 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
127 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
128 | { |
|
|
129 | K *obj = static_cast<K *>(w->data); |
|
|
130 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
131 | } |
|
|
132 | |
|
|
133 | // const method callback |
|
|
134 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
135 | void set (const K *object) |
|
|
136 | { |
|
|
137 | set_ (object, const_method_thunk<K, method>); |
|
|
138 | } |
|
|
139 | |
|
|
140 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
141 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
142 | { |
|
|
143 | K *obj = static_cast<K *>(w->data); |
|
|
144 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
145 | } |
|
|
146 | |
|
|
147 | // function callback |
|
|
148 | template<void (*function)(watcher &w, int)> |
|
|
149 | void set (void *data = 0) |
|
|
150 | { |
|
|
151 | set_ (data, function_thunk<function>); |
|
|
152 | } |
|
|
153 | |
|
|
154 | template<void (*function)(watcher &w, int)> |
|
|
155 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
156 | { |
|
|
157 | function (*static_cast<watcher *>(w), revents); |
|
|
158 | } |
|
|
159 | |
|
|
160 | // simple callback |
|
|
161 | template<class K, void (K::*method)()> |
|
|
162 | void set (K *object) |
|
|
163 | { |
|
|
164 | set_ (object, method_noargs_thunk<K, method>); |
|
|
165 | } |
|
|
166 | |
|
|
167 | template<class K, void (K::*method)()> |
|
|
168 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
169 | { |
|
|
170 | K *obj = static_cast<K *>(w->data); |
|
|
171 | (obj->*method) (); |
|
|
172 | } |
|
|
173 | |
|
|
174 | void operator ()(int events = EV_UNDEF) |
|
|
175 | { |
|
|
176 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
177 | (static_cast<ev_watcher *>(this), events); |
|
|
178 | } |
|
|
179 | |
|
|
180 | bool is_active () const |
|
|
181 | { |
|
|
182 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
183 | } |
|
|
184 | |
|
|
185 | bool is_pending () const |
|
|
186 | { |
|
|
187 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
|
|
188 | } |
|
|
189 | |
|
|
190 | void feed_event (int revents) |
|
|
191 | { |
|
|
192 | ev_feed_event (EV_A_ static_cast<const ev_watcher *>(this), revents); |
|
|
193 | } |
|
|
194 | }; |
|
|
195 | |
|
|
196 | inline void delay (tstamp interval) |
|
|
197 | { |
|
|
198 | ev_sleep (interval); |
|
|
199 | } |
|
|
200 | |
|
|
201 | inline int version_major () |
|
|
202 | { |
|
|
203 | return ev_version_major (); |
|
|
204 | } |
|
|
205 | |
|
|
206 | inline int version_minor () |
|
|
207 | { |
|
|
208 | return ev_version_minor (); |
|
|
209 | } |
|
|
210 | |
|
|
211 | inline unsigned int supported_backends () |
|
|
212 | { |
|
|
213 | return ev_supported_backends (); |
|
|
214 | } |
|
|
215 | |
|
|
216 | inline unsigned int recommended_backends () |
|
|
217 | { |
|
|
218 | return ev_recommended_backends (); |
|
|
219 | } |
|
|
220 | |
|
|
221 | inline unsigned int embeddable_backends () |
|
|
222 | { |
|
|
223 | return ev_embeddable_backends (); |
|
|
224 | } |
|
|
225 | |
|
|
226 | inline void set_allocator (void *(*cb)(void *ptr, long size)) |
|
|
227 | { |
|
|
228 | ev_set_allocator (cb); |
|
|
229 | } |
|
|
230 | |
|
|
231 | inline void set_syserr_cb (void (*cb)(const char *msg)) |
|
|
232 | { |
|
|
233 | ev_set_syserr_cb (cb); |
|
|
234 | } |
|
|
235 | |
165 | |
236 | |
166 | inline ev_tstamp now (EV_P) |
237 | inline ev_tstamp now (EV_P) |
167 | { |
238 | { |
168 | return ev_now (EV_A); |
239 | return ev_now (EV_A); |
169 | } |
240 | } |