1 | /* |
1 | /* |
2 | iom.h -- generic I/O multiplexor |
2 | iom.h -- generic I/O multiplexor |
3 | Copyright (C) 2003 Marc Lehmann <pcg@goof.com> |
3 | Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
15 | You should have received a copy of the GNU General Public License |
15 | You should have received a copy of the GNU General Public License |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #ifndef VPE_IOM_H__ |
20 | #ifndef IOM_H__ |
21 | #define VPE_IOM_H__ |
21 | #define IOM_H__ |
22 | |
22 | |
23 | #include <cassert> |
23 | // required: |
|
|
24 | // - a vector template like simplevec or stl's vector |
|
|
25 | // - defines for all watcher types required in your app |
|
|
26 | // edit iom_conf.h as appropriate. |
|
|
27 | #include "iom_conf.h" |
24 | |
28 | |
25 | #include "callback.h" |
29 | #include "callback.h" |
26 | #include "rxvtvec.h" |
|
|
27 | |
30 | |
28 | #ifndef IOM_IO |
31 | #ifndef IOM_IO |
29 | # define IOM_IO 1 |
32 | # define IOM_IO 0 |
30 | #endif |
33 | #endif |
31 | #ifndef IOM_TIME |
34 | #ifndef IOM_TIME |
32 | # define IOM_TIME 1 |
35 | # define IOM_TIME 0 |
33 | #endif |
36 | #endif |
34 | #ifndef IOM_CHECK |
37 | #ifndef IOM_CHECK |
35 | # define IOM_CHECK 1 |
38 | # define IOM_CHECK 0 |
36 | #endif |
39 | #endif |
37 | #ifndef IOM_IDLE |
40 | #ifndef IOM_IDLE |
38 | # define IOM_IDLE 0 |
41 | # define IOM_IDLE 0 |
39 | #endif |
42 | #endif |
40 | |
43 | |
41 | typedef double tstamp; |
44 | typedef double tstamp; |
42 | extern tstamp NOW; |
45 | extern tstamp NOW; |
43 | |
46 | |
|
|
47 | struct watcher; |
44 | #if IOM_IO |
48 | #if IOM_IO |
45 | struct io_watcher; |
49 | struct io_watcher; |
46 | #endif |
50 | #endif |
47 | #if IOM_TIME |
51 | #if IOM_TIME |
48 | struct time_watcher; |
52 | struct time_watcher; |
49 | #endif |
53 | #endif |
50 | #if IOM_CHECK |
54 | #if IOM_CHECK |
51 | struct check_watcher; |
55 | struct check_watcher; |
52 | #endif |
56 | #endif |
53 | #if IOM_IDLE |
57 | #if IOM_IDLE |
54 | struct idle_watcher; |
58 | struct idle_watcher; |
55 | #endif |
59 | #endif |
56 | |
60 | |
57 | template<class watcher> |
61 | template<class watcher> |
58 | struct io_manager_vec : protected simplevec<watcher *> { |
62 | struct io_manager_vec : protected vector<watcher *> { |
59 | friend class io_manager; |
63 | friend class io_manager; |
60 | protected: |
64 | protected: |
61 | void erase_unordered (unsigned int pos) |
65 | void erase_unordered (unsigned int pos) |
62 | { |
66 | { |
63 | watcher *w = (*this)[size () - 1]; |
67 | watcher *w = (*this)[size () - 1]; |
… | |
… | |
82 | #if IOM_IDLE |
86 | #if IOM_IDLE |
83 | io_manager_vec<idle_watcher> iw; |
87 | io_manager_vec<idle_watcher> iw; |
84 | #endif |
88 | #endif |
85 | |
89 | |
86 | template<class watcher> |
90 | template<class watcher> |
87 | void reg (watcher *w, simplevec<watcher *> &queue); |
91 | void reg (watcher *w, io_manager_vec<watcher> &queue); |
88 | |
92 | |
89 | template<class watcher> |
93 | template<class watcher> |
90 | void unreg (watcher *w, simplevec<watcher *> &queue); |
94 | void unreg (watcher *w, io_manager_vec<watcher> &queue); |
91 | |
95 | |
92 | public: |
96 | public: |
93 | // register a watcher |
97 | // register a watcher |
94 | #if IOM_IO |
98 | #if IOM_IO |
95 | void reg (io_watcher *w); void unreg (io_watcher *w); |
99 | void reg (io_watcher *w); void unreg (io_watcher *w); |
… | |
… | |
152 | void start (tstamp when) { set (when); iom.reg (this); } |
156 | void start (tstamp when) { set (when); iom.reg (this); } |
153 | void stop () { iom.unreg (this); } |
157 | void stop () { iom.unreg (this); } |
154 | |
158 | |
155 | template<class O1, class O2> |
159 | template<class O1, class O2> |
156 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
160 | time_watcher (O1 *object, void (O2::*method)(time_watcher &)) |
157 | : callback1<void, time_watcher &>(object,method) |
161 | : callback1<void, time_watcher &>(object,method), at(0) |
158 | { } |
162 | { } |
159 | ~time_watcher () { stop (); } |
163 | ~time_watcher () { stop (); } |
160 | }; |
164 | }; |
161 | #endif |
165 | #endif |
162 | |
166 | |