… | |
… | |
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 | |
40 | |
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
41 | void idle_cb (tstamp &ts); time_watcher *idle; |
42 | public: |
42 | public: |
43 | |
43 | |
44 | // register a watcher |
44 | // register a watcher |
45 | void reg (int fd, short events, io_watcher *w); |
45 | void reg (int fd, short events, io_watcher *w); |
46 | void unreg (io_watcher *w); |
46 | void unreg (const io_watcher *w); |
47 | void reg (time_watcher *w); |
47 | void reg (time_watcher *w); |
48 | void unreg (time_watcher *w); |
48 | void unreg (const time_watcher *w); |
49 | |
49 | |
50 | void loop (); |
50 | void loop (); |
51 | |
51 | |
52 | io_manager (); |
52 | io_manager (); |
53 | ~io_manager (); |
53 | ~io_manager (); |
… | |
… | |
62 | void *obj; |
62 | void *obj; |
63 | R (object::*meth)(A arg); |
63 | R (object::*meth)(A arg); |
64 | |
64 | |
65 | // 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 |
66 | struct proxy_base { |
66 | struct proxy_base { |
67 | 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; |
68 | }; |
68 | }; |
69 | template<class O1, class O2> |
69 | template<class O1, class O2> |
70 | struct proxy : proxy_base { |
70 | struct proxy : proxy_base { |
71 | virtual R call (void *obj, void (object::*meth)(A), A arg) |
71 | virtual R call (void *obj, R (object::*meth)(A), A arg) |
72 | { |
72 | { |
73 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
73 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth))) |
74 | (arg); |
74 | (arg); |
75 | } |
75 | } |
76 | }; |
76 | }; |
77 | |
77 | |
78 | proxy_base *prxy; |
78 | proxy_base *prxy; |
79 | |
79 | |
80 | public: |
80 | public: |
81 | template<class O1, class O2> |
81 | template<class O1, class O2> |
82 | callback (O1 *object, void (O2::*method)(A)) |
82 | callback (O1 *object, R (O2::*method)(A)) |
83 | { |
83 | { |
84 | static proxy<O1,O2> p; |
84 | static proxy<O1,O2> p; |
85 | obj = reinterpret_cast<void *>(object); |
85 | obj = reinterpret_cast<void *>(object); |
86 | meth = reinterpret_cast<void (object::*)(A)>(method); |
86 | meth = reinterpret_cast<R (object::*)(A)>(method); |
87 | prxy = &p; |
87 | prxy = &p; |
88 | } |
88 | } |
89 | |
89 | |
90 | R call(A arg) |
90 | R call(A arg) const |
91 | { |
91 | { |
92 | return prxy->call (obj, meth, arg); |
92 | return prxy->call (obj, meth, arg); |
93 | } |
93 | } |
94 | |
94 | |
95 | R operator ()(A arg) |
95 | R operator ()(A arg) const |
96 | { |
96 | { |
97 | return call (arg); |
97 | return call (arg); |
98 | } |
98 | } |
99 | }; |
99 | }; |
100 | |
100 | |
… | |
… | |
107 | void start (int fd, short events) |
107 | void start (int fd, short events) |
108 | { |
108 | { |
109 | iom.reg (fd, events, this); |
109 | iom.reg (fd, events, this); |
110 | } |
110 | } |
111 | |
111 | |
112 | void stop () |
112 | void stop () const |
113 | { |
113 | { |
114 | iom.unreg (this); |
114 | iom.unreg (this); |
115 | } |
115 | } |
116 | }; |
116 | }; |
117 | |
117 | |
… | |
… | |
137 | void start (tstamp when) |
137 | void start (tstamp when) |
138 | { |
138 | { |
139 | set (when); |
139 | set (when); |
140 | } |
140 | } |
141 | |
141 | |
142 | void stop () |
142 | void stop () const |
143 | { |
143 | { |
144 | iom.unreg (this); |
144 | iom.unreg (this); |
145 | } |
145 | } |
146 | |
146 | |
147 | void reset (tstamp when = TSTAMP_CANCEL) |
147 | void reset (tstamp when = TSTAMP_CANCEL) |