… | |
… | |
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); |
91 | } |
93 | } |
92 | |
94 | |
93 | void stop () |
95 | R operator ()(A arg) const |
94 | { |
96 | { |
95 | iom.unreg (this); |
97 | return call (arg); |
96 | } |
98 | } |
97 | }; |
99 | }; |
98 | |
100 | |
99 | struct io_watcher : callback<void, short> { |
101 | struct io_watcher : callback<void, short> { |
100 | template<class O1, class O2> |
102 | template<class O1, class O2> |
… | |
… | |
105 | void start (int fd, short events) |
107 | void start (int fd, short events) |
106 | { |
108 | { |
107 | iom.reg (fd, events, this); |
109 | iom.reg (fd, events, this); |
108 | } |
110 | } |
109 | |
111 | |
|
|
112 | void stop () const |
|
|
113 | { |
|
|
114 | iom.unreg (this); |
|
|
115 | } |
110 | }; |
116 | }; |
|
|
117 | |
|
|
118 | #define TSTAMP_CANCEL -1. |
111 | |
119 | |
112 | struct time_watcher : callback<void, tstamp &> { |
120 | struct time_watcher : callback<void, tstamp &> { |
113 | tstamp at; |
121 | tstamp at; |
114 | |
122 | |
115 | template<class O1, class O2> |
123 | template<class O1, class O2> |
116 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
124 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
117 | : callback<void, tstamp &>(object,method) |
125 | : callback<void, tstamp &>(object,method) |
118 | { } |
126 | { } |
119 | |
127 | |
120 | void set (tstamp when); |
128 | void set (tstamp when); |
|
|
129 | void trigger (); |
121 | |
130 | |
122 | void trigger () |
131 | void operator ()() |
123 | { |
132 | { |
124 | call (at); |
133 | trigger (); |
125 | } |
134 | } |
126 | |
135 | |
|
|
136 | void start (); |
127 | void start (tstamp when) |
137 | void start (tstamp when) |
128 | { |
138 | { |
129 | set (when); |
139 | set (when); |
|
|
140 | } |
|
|
141 | |
|
|
142 | void stop () const |
|
|
143 | { |
|
|
144 | iom.unreg (this); |
|
|
145 | } |
|
|
146 | |
|
|
147 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
148 | { |
|
|
149 | stop (); |
|
|
150 | at = when; |
130 | } |
151 | } |
131 | }; |
152 | }; |
132 | |
153 | |
133 | #endif |
154 | #endif |
134 | |
155 | |