… | |
… | |
21 | |
21 | |
22 | #include <vector> |
22 | #include <vector> |
23 | |
23 | |
24 | #include <sys/poll.h> |
24 | #include <sys/poll.h> |
25 | |
25 | |
|
|
26 | #include "callback.h" |
26 | #include "slog.h" |
27 | #include "slog.h" |
27 | |
28 | |
28 | typedef double tstamp; |
29 | typedef double tstamp; |
29 | |
30 | |
30 | extern tstamp NOW; |
31 | extern tstamp NOW; |
31 | |
32 | |
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<io_watcher *> iow; |
39 | vector<time_watcher *> tw; // actually a heap |
39 | vector<time_watcher *> tw; // actually a heap |
|
|
40 | |
|
|
41 | void idle_cb (time_watcher &w); time_watcher *idle; |
40 | public: |
42 | public: |
41 | |
43 | |
|
|
44 | void reschedule_time_watchers (); |
|
|
45 | |
42 | // register a watcher |
46 | // register a watcher |
43 | void reg (int fd, short events, io_watcher *w); |
47 | void reg (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); |
46 | void unreg (time_watcher *w); |
50 | void unreg (time_watcher *w); |
47 | |
51 | |
48 | void loop (); |
52 | void loop (); |
… | |
… | |
51 | ~io_manager (); |
55 | ~io_manager (); |
52 | }; |
56 | }; |
53 | |
57 | |
54 | extern io_manager iom; |
58 | extern io_manager iom; |
55 | |
59 | |
56 | template<class R, class A> |
60 | struct io_watcher : callback2<void, io_watcher &, short> { |
57 | class callback { |
61 | int fd; |
58 | struct object { }; |
62 | short events; |
59 | |
63 | |
60 | void *obj; |
64 | template<class O1, class O2> |
61 | R (object::*meth)(A arg); |
65 | io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) |
|
|
66 | : callback2<void, io_watcher &, short>(object,method) |
|
|
67 | { } |
62 | |
68 | |
63 | // a proxy is a kind of recipe on how to call a specific class method |
69 | ~io_watcher () |
64 | struct proxy_base { |
|
|
65 | virtual R call (void *obj, void (object::*meth)(A), A arg) = 0; |
|
|
66 | }; |
|
|
67 | template<class O1, class O2> |
|
|
68 | struct proxy : proxy_base { |
|
|
69 | virtual R call (void *obj, void (object::*meth)(A), A arg) |
|
|
70 | { |
|
|
71 | ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth))) |
|
|
72 | (arg); |
|
|
73 | } |
|
|
74 | }; |
|
|
75 | |
|
|
76 | proxy_base *prxy; |
|
|
77 | |
|
|
78 | public: |
|
|
79 | template<class O1, class O2> |
|
|
80 | callback (O1 *object, void (O2::*method)(A)) |
|
|
81 | { |
70 | { |
82 | static proxy<O1,O2> p; |
71 | iom.unreg (this); |
83 | obj = reinterpret_cast<void *>(object); |
|
|
84 | meth = reinterpret_cast<void (object::*)(A)>(method); |
|
|
85 | prxy = &p; |
|
|
86 | } |
72 | } |
87 | |
73 | |
88 | R call(A arg) |
74 | void start (int fd_, short events_) |
89 | { |
75 | { |
90 | return prxy->call (obj, meth, arg); |
76 | fd = fd_; |
|
|
77 | events = events_; |
|
|
78 | iom.reg (this); |
91 | } |
79 | } |
92 | |
80 | |
93 | void stop () |
81 | void stop () |
94 | { |
82 | { |
95 | iom.unreg (this); |
83 | iom.unreg (this); |
96 | } |
84 | } |
97 | }; |
85 | }; |
98 | |
86 | |
99 | struct io_watcher : callback<void, short> { |
87 | #define TSTAMP_CANCEL -1. |
100 | template<class O1, class O2> |
|
|
101 | io_watcher (O1 *object, void (O2::*method)(short revents)) |
|
|
102 | : callback<void, short>(object,method) |
|
|
103 | { } |
|
|
104 | |
88 | |
105 | void start (int fd, short events) |
|
|
106 | { |
|
|
107 | iom.reg (fd, events, this); |
|
|
108 | } |
|
|
109 | |
|
|
110 | }; |
|
|
111 | |
|
|
112 | struct time_watcher : callback<void, tstamp &> { |
89 | struct time_watcher : callback1<void, time_watcher &> { |
|
|
90 | bool registered; // already registered? |
113 | tstamp at; |
91 | tstamp at; |
114 | |
92 | |
115 | template<class O1, class O2> |
93 | template<class O1, class O2> |
116 | time_watcher (O1 *object, void (O2::*method)(tstamp &)) |
94 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
117 | : callback<void, tstamp &>(object,method) |
95 | : callback1<void, time_watcher &>(object,method) |
|
|
96 | , registered(false) |
118 | { } |
97 | { } |
119 | |
98 | |
120 | void set (tstamp when); |
99 | ~time_watcher () |
121 | |
|
|
122 | void trigger () |
|
|
123 | { |
100 | { |
124 | call (at); |
101 | iom.unreg (this); |
125 | } |
102 | } |
126 | |
103 | |
|
|
104 | void set (tstamp when); |
|
|
105 | void trigger (); |
|
|
106 | |
|
|
107 | void operator ()() |
|
|
108 | { |
|
|
109 | trigger (); |
|
|
110 | } |
|
|
111 | |
|
|
112 | void start (); |
127 | void start (tstamp when = NOW) |
113 | void start (tstamp when) |
128 | { |
114 | { |
129 | set (when); |
115 | set (when); |
|
|
116 | } |
|
|
117 | |
|
|
118 | void stop () |
|
|
119 | { |
|
|
120 | iom.unreg (this); |
|
|
121 | } |
|
|
122 | |
|
|
123 | void reset (tstamp when = TSTAMP_CANCEL) |
|
|
124 | { |
|
|
125 | stop (); |
|
|
126 | at = when; |
130 | } |
127 | } |
131 | }; |
128 | }; |
132 | |
129 | |
133 | #endif |
130 | #endif |
134 | |
131 | |