… | |
… | |
23 | |
23 | |
24 | #include <sys/poll.h> |
24 | #include <sys/poll.h> |
25 | |
25 | |
26 | #include "slog.h" |
26 | #include "slog.h" |
27 | |
27 | |
28 | typedef double timestamp; |
28 | typedef double tstamp; |
29 | |
29 | |
30 | extern timestamp NOW; |
30 | extern tstamp NOW; |
31 | |
31 | |
32 | template<class R, class A> class callback; |
32 | template<class R, class A> class callback; |
33 | struct io_watcher; |
33 | struct io_watcher; |
34 | struct time_watcher; |
34 | struct time_watcher; |
35 | |
35 | |
36 | class io_manager { |
36 | class io_manager { |
37 | vector<pollfd> pfs; |
37 | vector<pollfd> pfs; |
38 | vector<io_watcher *> iow; |
38 | vector<const io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
39 | vector<time_watcher *> tw; // actually a heap |
|
|
40 | |
|
|
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
40 | public: |
42 | public: |
|
|
43 | |
|
|
44 | void reschedule_time_watchers (); |
41 | |
45 | |
42 | // register a watcher |
46 | // register a watcher |
43 | void reg (int fd, short events, io_watcher *w); |
47 | void reg (int fd, short events, io_watcher *w); |
44 | void unreg (io_watcher *w); |
48 | void unreg (io_watcher *w); |
45 | void reg (time_watcher *w); |
49 | void reg (time_watcher *w); |
… | |
… | |
60 | void *obj; |
64 | void *obj; |
61 | R (object::*meth)(A arg); |
65 | R (object::*meth)(A arg); |
62 | |
66 | |
63 | // a proxy is a kind of recipe on how to call a specific class method |
67 | // a proxy is a kind of recipe on how to call a specific class method |
64 | struct proxy_base { |
68 | struct proxy_base { |
65 | virtual R call (void *obj, void (object::*meth)(A), A arg) = 0; |
69 | virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
66 | }; |
70 | }; |
67 | template<class O1, class O2> |
71 | template<class O1, class O2> |
68 | struct proxy : proxy_base { |
72 | struct proxy : proxy_base { |
69 | virtual R call (void *obj, void (object::*meth)(A), A arg) |
73 | virtual R call (void *obj, R (object::*meth)(A), A arg) |
70 | { |
74 | { |
71 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
75 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
72 | (arg); |
76 | (arg); |
73 | } |
77 | } |
74 | }; |
78 | }; |
75 | |
79 | |
76 | proxy_base *prxy; |
80 | proxy_base *prxy; |
77 | |
81 | |
78 | public: |
82 | public: |
79 | template<class O1, class O2> |
83 | template<class O1, class O2> |
80 | callback (O1 *object, void (O2::*method)(A)) |
84 | callback (O1 *object, R (O2::*method)(A)) |
81 | { |
85 | { |
82 | static proxy<O1,O2> p; |
86 | static proxy<O1,O2> p; |
83 | obj = reinterpret_cast<void *>(object); |
87 | obj = reinterpret_cast<void *>(object); |
84 | meth = reinterpret_cast<void (object::*)(A)>(method); |
88 | meth = reinterpret_cast<R (object::*)(A)>(method); |
85 | prxy = &p; |
89 | prxy = &p; |
86 | } |
90 | } |
87 | |
91 | |
88 | R call(A arg) |
92 | R call(A arg) const |
89 | { |
93 | { |
90 | return prxy->call (obj, meth, arg); |
94 | return prxy->call (obj, meth, arg); |
|
|
95 | } |
|
|
96 | |
|
|
97 | R operator ()(A arg) const |
|
|
98 | { |
|
|
99 | return call (arg); |
91 | } |
100 | } |
92 | }; |
101 | }; |
93 | |
102 | |
94 | struct io_watcher : callback<void, short> { |
103 | struct io_watcher : callback<void, short> { |
95 | template<class O1, class O2> |
104 | template<class O1, class O2> |
96 | io_watcher (int fd, short events, O1 *object, void (O2::*method)(short revents)) |
105 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
97 | : callback<void, short>(object,method) |
106 | : callback<void, short>(object,method) |
98 | { |
107 | { } |
99 | iom.reg (fd, events, this); |
|
|
100 | } |
|
|
101 | |
108 | |
102 | ~io_watcher () |
109 | ~io_watcher () |
103 | { |
110 | { |
104 | iom.unreg (this); |
111 | iom.unreg (this); |
105 | } |
112 | } |
|
|
113 | |
|
|
114 | void start (int fd, short events) |
|
|
115 | { |
|
|
116 | iom.reg (fd, events, this); |
|
|
117 | } |
|
|
118 | |
|
|
119 | void stop () |
|
|
120 | { |
|
|
121 | iom.unreg (this); |
|
|
122 | } |
106 | }; |
123 | }; |
107 | |
124 | |
108 | struct time_watcher : callback<void, timestamp &> { |
125 | #define TSTAMP_CANCEL -1. |
109 | timestamp at; |
|
|
110 | |
126 | |
111 | void set (timestamp when); |
127 | struct time_watcher : callback<void, tstamp &> { |
|
|
128 | bool registered; // already registered? |
|
|
129 | tstamp at; |
112 | |
130 | |
113 | template<class O1, class O2> |
131 | template<class O1, class O2> |
114 | time_watcher (timestamp when, O1 *object, void (O2::*method)(timestamp &)) |
132 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
115 | : callback<void, timestamp &>(object,method) |
133 | : callback<void, tstamp &>(object,method) |
116 | , at(when) |
134 | , registered(false) |
117 | { |
135 | { } |
118 | iom.reg (this); |
|
|
119 | } |
|
|
120 | |
136 | |
121 | ~time_watcher () |
137 | ~time_watcher () |
122 | { |
138 | { |
123 | iom.unreg (this); |
139 | iom.unreg (this); |
124 | } |
140 | } |
|
|
141 | |
|
|
142 | void set (tstamp when); |
|
|
143 | void trigger (); |
|
|
144 | |
|
|
145 | void operator ()() |
|
|
146 | { |
|
|
147 | trigger (); |
|
|
148 | } |
|
|
149 | |
|
|
150 | void start (); |
|
|
151 | void start (tstamp when) |
|
|
152 | { |
|
|
153 | set (when); |
|
|
154 | } |
|
|
155 | |
|
|
156 | void stop () |
|
|
157 | { |
|
|
158 | iom.unreg (this); |
|
|
159 | } |
|
|
160 | |
|
|
161 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
162 | { |
|
|
163 | stop (); |
|
|
164 | at = when; |
|
|
165 | } |
125 | }; |
166 | }; |
126 | |
167 | |
127 | #endif |
168 | #endif |
128 | |
169 | |