|
|
1 | /* |
|
|
2 | * libev simple C++ wrapper classes |
|
|
3 | * |
|
|
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
5 | * All rights reserved. |
|
|
6 | * |
|
|
7 | * Redistribution and use in source and binary forms, with or without modifica- |
|
|
8 | * tion, are permitted provided that the following conditions are met: |
|
|
9 | * |
|
|
10 | * 1. Redistributions of source code must retain the above copyright notice, |
|
|
11 | * this list of conditions and the following disclaimer. |
|
|
12 | * |
|
|
13 | * 2. Redistributions in binary form must reproduce the above copyright |
|
|
14 | * notice, this list of conditions and the following disclaimer in the |
|
|
15 | * documentation and/or other materials provided with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
|
|
18 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
|
|
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
|
|
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
|
|
21 | * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
|
|
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
|
|
23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|
|
24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
|
|
25 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
|
|
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
|
|
27 | * |
|
|
28 | * Alternatively, the contents of this file may be used under the terms of |
|
|
29 | * the GNU General Public License ("GPL") version 2 or any later version, |
|
|
30 | * in which case the provisions of the GPL are applicable instead of |
|
|
31 | * the above. If you wish to allow the use of your version of this file |
|
|
32 | * only under the terms of the GPL and not to allow others to use your |
|
|
33 | * version of this file under the BSD license, indicate your decision |
|
|
34 | * by deleting the provisions above and replace them with the notice |
|
|
35 | * and other provisions required by the GPL. If you do not delete the |
|
|
36 | * provisions above, a recipient may use your version of this file under |
|
|
37 | * either the BSD or the GPL. |
|
|
38 | */ |
|
|
39 | |
1 | #ifndef EVPP_H__ |
40 | #ifndef EVPP_H__ |
2 | #define EVPP_H__ |
41 | #define EVPP_H__ |
3 | |
42 | |
4 | #ifdef EV_H |
43 | #ifdef EV_H |
5 | # include EV_H |
44 | # include EV_H |
6 | #else |
45 | #else |
7 | # include <ev.h> |
46 | # include "ev.h" |
8 | #endif |
47 | #endif |
9 | |
48 | |
10 | namespace ev { |
49 | namespace ev { |
11 | |
50 | |
12 | template<class ev_watcher, class watcher> |
51 | typedef ev_tstamp tstamp; |
13 | struct base : ev_watcher |
|
|
14 | { |
|
|
15 | #if EV_MULTIPLICITY |
|
|
16 | EV_P; |
|
|
17 | |
|
|
18 | void set (EV_P) |
|
|
19 | { |
|
|
20 | this->EV_A = EV_A; |
|
|
21 | } |
|
|
22 | #endif |
|
|
23 | |
|
|
24 | base () |
|
|
25 | { |
|
|
26 | ev_init (this, 0); |
|
|
27 | } |
|
|
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)); |
|
|
104 | } |
|
|
105 | }; |
|
|
106 | |
52 | |
107 | enum { |
53 | enum { |
108 | UNDEF = EV_UNDEF, |
54 | UNDEF = EV_UNDEF, |
109 | NONE = EV_NONE, |
55 | NONE = EV_NONE, |
110 | READ = EV_READ, |
56 | READ = EV_READ, |
… | |
… | |
120 | FORK = EV_FORK, |
66 | FORK = EV_FORK, |
121 | EMBED = EV_EMBED, |
67 | EMBED = EV_EMBED, |
122 | ERROR = EV_ERROR, |
68 | ERROR = EV_ERROR, |
123 | }; |
69 | }; |
124 | |
70 | |
125 | 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 | |
126 | |
191 | |
127 | inline ev_tstamp now (EV_P) |
192 | inline ev_tstamp now (EV_P) |
128 | { |
193 | { |
129 | return ev_now (EV_A); |
194 | return ev_now (EV_A); |
130 | } |
195 | } |
… | |
… | |
167 | \ |
232 | \ |
168 | using base<ev_ ## cstem, cppstem>::set; \ |
233 | using base<ev_ ## cstem, cppstem>::set; \ |
169 | \ |
234 | \ |
170 | private: \ |
235 | private: \ |
171 | \ |
236 | \ |
172 | cppstem (const cppstem &o) \ |
237 | cppstem (const cppstem &o); \ |
173 | { /* disabled */ } \ |
|
|
174 | \ |
238 | \ |
175 | void operator =(const cppstem &o) { /* disabled */ } \ |
239 | cppstem & operator =(const cppstem &o); \ |
176 | \ |
240 | \ |
177 | public: |
241 | public: |
178 | |
242 | |
179 | #define EV_END_WATCHER(cppstem,cstem) \ |
243 | #define EV_END_WATCHER(cppstem,cstem) \ |
180 | }; |
244 | }; |