… | |
… | |
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: |
41 | |
43 | |
42 | // register a watcher |
44 | // register a watcher |
43 | void reg (int fd, short events, io_watcher *w); |
45 | void reg (int fd, short events, io_watcher *w); |
44 | void unreg (io_watcher *w); |
46 | void unreg (const io_watcher *w); |
45 | void reg (time_watcher *w); |
47 | void reg (time_watcher *w); |
46 | void unreg (time_watcher *w); |
48 | void unreg (const time_watcher *w); |
47 | |
49 | |
48 | void loop (); |
50 | void loop (); |
49 | |
51 | |
50 | io_manager (); |
52 | io_manager (); |
51 | ~io_manager (); |
53 | ~io_manager (); |
… | |
… | |
60 | void *obj; |
62 | void *obj; |
61 | R (object::*meth)(A arg); |
63 | R (object::*meth)(A arg); |
62 | |
64 | |
63 | // a proxy is a kind of recipe on how to call a specific class method |
65 | // a proxy is a kind of recipe on how to call a specific class method |
64 | struct proxy_base { |
66 | struct proxy_base { |
65 | virtual R call (void *obj, void (object::*meth)(A), A arg) = 0; |
67 | virtual R call (void *obj, R (object::*meth)(A), A arg) = 0; |
66 | }; |
68 | }; |
67 | template<class O1, class O2> |
69 | template<class O1, class O2> |
68 | struct proxy : proxy_base { |
70 | struct proxy : proxy_base { |
69 | virtual R call (void *obj, void (object::*meth)(A), A arg) |
71 | virtual R call (void *obj, R (object::*meth)(A), A arg) |
70 | { |
72 | { |
71 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
73 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
72 | (arg); |
74 | (arg); |
73 | } |
75 | } |
74 | }; |
76 | }; |
75 | |
77 | |
76 | proxy_base *prxy; |
78 | proxy_base *prxy; |
77 | |
79 | |
78 | public: |
80 | public: |
79 | template<class O1, class O2> |
81 | template<class O1, class O2> |
80 | callback (O1 *object, void (O2::*method)(A)) |
82 | callback (O1 *object, R (O2::*method)(A)) |
81 | { |
83 | { |
82 | static proxy<O1,O2> p; |
84 | static proxy<O1,O2> p; |
83 | obj = reinterpret_cast<void *>(object); |
85 | obj = reinterpret_cast<void *>(object); |
84 | meth = reinterpret_cast<void (object::*)(A)>(method); |
86 | meth = reinterpret_cast<R (object::*)(A)>(method); |
85 | prxy = &p; |
87 | prxy = &p; |
86 | } |
88 | } |
87 | |
89 | |
88 | R call(A arg) |
90 | R call(A arg) const |
89 | { |
91 | { |
90 | return prxy->call (obj, meth, arg); |
92 | return prxy->call (obj, meth, arg); |
|
|
93 | } |
|
|
94 | |
|
|
95 | R operator ()(A arg) const |
|
|
96 | { |
|
|
97 | return call (arg); |
91 | } |
98 | } |
92 | }; |
99 | }; |
93 | |
100 | |
94 | struct io_watcher : callback<void, short> { |
101 | struct io_watcher : callback<void, short> { |
95 | template<class O1, class O2> |
102 | template<class O1, class O2> |
96 | io_watcher (int fd, short events, O1 *object, void (O2::*method)(short revents)) |
103 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
97 | : callback<void, short>(object,method) |
104 | : callback<void, short>(object,method) |
|
|
105 | { } |
|
|
106 | |
|
|
107 | void start (int fd, short events) |
98 | { |
108 | { |
99 | iom.reg (fd, events, this); |
109 | iom.reg (fd, events, this); |
100 | } |
110 | } |
101 | |
111 | |
102 | ~io_watcher () |
112 | void stop () const |
103 | { |
113 | { |
104 | iom.unreg (this); |
114 | iom.unreg (this); |
105 | } |
115 | } |
106 | }; |
116 | }; |
107 | |
117 | |
108 | struct time_watcher : callback<void, timestamp &> { |
118 | #define TSTAMP_CANCEL -1. |
109 | timestamp at; |
|
|
110 | |
119 | |
111 | void set (timestamp when); |
120 | struct time_watcher : callback<void, tstamp &> { |
|
|
121 | tstamp at; |
112 | |
122 | |
113 | template<class O1, class O2> |
123 | template<class O1, class O2> |
114 | time_watcher (timestamp when, O1 *object, void (O2::*method)(timestamp &)) |
124 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
115 | : callback<void, timestamp &>(object,method) |
125 | : callback<void, tstamp &>(object,method) |
116 | , at(when) |
126 | { } |
|
|
127 | |
|
|
128 | void set (tstamp when); |
|
|
129 | void trigger (); |
|
|
130 | |
|
|
131 | void operator ()() |
117 | { |
132 | { |
118 | iom.reg (this); |
133 | trigger (); |
119 | } |
134 | } |
120 | |
135 | |
121 | ~time_watcher () |
136 | void start (); |
|
|
137 | void start (tstamp when) |
|
|
138 | { |
|
|
139 | set (when); |
|
|
140 | } |
|
|
141 | |
|
|
142 | void stop () const |
122 | { |
143 | { |
123 | iom.unreg (this); |
144 | iom.unreg (this); |
|
|
145 | } |
|
|
146 | |
|
|
147 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
148 | { |
|
|
149 | stop (); |
|
|
150 | at = when; |
124 | } |
151 | } |
125 | }; |
152 | }; |
126 | |
153 | |
127 | #endif |
154 | #endif |
128 | |
155 | |