… | |
… | |
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 | template<class ev_watcher, class watcher> |
|
|
72 | struct base : ev_watcher |
|
|
73 | { |
|
|
74 | #if EV_MULTIPLICITY |
|
|
75 | EV_P; |
|
|
76 | |
|
|
77 | void set (EV_P) |
|
|
78 | { |
|
|
79 | this->EV_A = EV_A; |
|
|
80 | } |
|
|
81 | #endif |
|
|
82 | |
|
|
83 | base () |
|
|
84 | { |
|
|
85 | ev_init (this, 0); |
|
|
86 | } |
|
|
87 | |
|
|
88 | void set_ (void *data, void (*cb)(EV_P_ ev_watcher *w, int revents)) |
|
|
89 | { |
|
|
90 | this->data = data; |
|
|
91 | ev_set_cb (static_cast<ev_watcher *>(this), cb); |
|
|
92 | } |
|
|
93 | |
|
|
94 | // method callback |
|
|
95 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
96 | void set (K *object) |
|
|
97 | { |
|
|
98 | set_ (object, method_thunk<K, method>); |
|
|
99 | } |
|
|
100 | |
|
|
101 | template<class K, void (K::*method)(watcher &w, int)> |
|
|
102 | static void method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
103 | { |
|
|
104 | K *obj = static_cast<K *>(w->data); |
|
|
105 | (obj->*method) (*static_cast<watcher *>(w), revents); |
|
|
106 | } |
|
|
107 | |
|
|
108 | // const method callback |
|
|
109 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
110 | void set (const K *object) |
|
|
111 | { |
|
|
112 | set_ (object, const_method_thunk<K, method>); |
|
|
113 | } |
|
|
114 | |
|
|
115 | template<class K, void (K::*method)(watcher &w, int) const> |
|
|
116 | static void const_method_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
117 | { |
|
|
118 | K *obj = static_cast<K *>(w->data); |
|
|
119 | (static_cast<K *>(w->data)->*method) (*static_cast<watcher *>(w), revents); |
|
|
120 | } |
|
|
121 | |
|
|
122 | // function callback |
|
|
123 | template<void (*function)(watcher &w, int)> |
|
|
124 | void set (void *data = 0) |
|
|
125 | { |
|
|
126 | set_ (data, function_thunk<function>); |
|
|
127 | } |
|
|
128 | |
|
|
129 | template<void (*function)(watcher &w, int)> |
|
|
130 | static void function_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
131 | { |
|
|
132 | function (*static_cast<watcher *>(w), revents); |
|
|
133 | } |
|
|
134 | |
|
|
135 | // simple callback |
|
|
136 | template<class K, void (K::*method)()> |
|
|
137 | void set (K *object) |
|
|
138 | { |
|
|
139 | set_ (object, method_noargs_thunk<K, method>); |
|
|
140 | } |
|
|
141 | |
|
|
142 | template<class K, void (K::*method)()> |
|
|
143 | static void method_noargs_thunk (EV_P_ ev_watcher *w, int revents) |
|
|
144 | { |
|
|
145 | K *obj = static_cast<K *>(w->data); |
|
|
146 | (obj->*method) (); |
|
|
147 | } |
|
|
148 | |
|
|
149 | void operator ()(int events = EV_UNDEF) |
|
|
150 | { |
|
|
151 | return ev_cb (static_cast<ev_watcher *>(this)) |
|
|
152 | (static_cast<ev_watcher *>(this), events); |
|
|
153 | } |
|
|
154 | |
|
|
155 | bool is_active () const |
|
|
156 | { |
|
|
157 | return ev_is_active (static_cast<const ev_watcher *>(this)); |
|
|
158 | } |
|
|
159 | |
|
|
160 | bool is_pending () const |
|
|
161 | { |
|
|
162 | return ev_is_pending (static_cast<const ev_watcher *>(this)); |
|
|
163 | } |
|
|
164 | }; |
|
|
165 | |
165 | |
166 | |
166 | inline ev_tstamp now (EV_P) |
167 | inline ev_tstamp now (EV_P) |
167 | { |
168 | { |
168 | return ev_now (EV_A); |
169 | return ev_now (EV_A); |
169 | } |
170 | } |