2 iom.h -- generic I/O multiplexor
3 Copyright (C) 2003, 2004 Marc Lehmann <pcg@goof.com>
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
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
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
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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.
44 typedef double tstamp;
61 template<class watcher>
62 struct io_manager_vec : protected vector<watcher *> {
63 friend class io_manager;
65 void erase_unordered (unsigned int pos)
67 watcher *w = (*this)[size () - 1];
78 io_manager_vec<io_watcher> iow;
81 io_manager_vec<check_watcher> cw;
84 io_manager_vec<time_watcher> tw;
87 io_manager_vec<idle_watcher> iw;
90 template<class watcher>
91 void reg (watcher *w, io_manager_vec<watcher> &queue);
93 template<class watcher>
94 void unreg (watcher *w, io_manager_vec<watcher> &queue);
99 void reg (io_watcher *w); void unreg (io_watcher *w);
102 void reg (time_watcher *w); void unreg (time_watcher *w);
105 void reg (check_watcher *w); void unreg (check_watcher *w);
108 void reg (idle_watcher *w); void unreg (idle_watcher *w);
117 extern io_manager iom; // a singleton, together with it's construction/destruction problems.
120 int active; /* 0 == inactive, else index into respective vector */
122 watcher() : active(0) { }
126 enum { EVENT_READ = 1, EVENT_WRITE = 2 };
128 struct io_watcher : watcher, callback2<void, io_watcher &, short> {
132 void set (int fd_, short events_) { fd = fd_; events = events_; }
134 void set (short events_) { set (fd, events_); }
135 void start () { iom.reg (this); }
136 void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
137 void stop () { iom.unreg (this); }
139 template<class O1, class O2>
140 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
141 : callback2<void, io_watcher &, short>(object,method)
143 ~io_watcher () { stop (); }
148 struct time_watcher : watcher, callback1<void, time_watcher &> {
153 void set (tstamp when) { at = when; }
154 void operator ()() { trigger (); }
155 void start () { iom.reg (this); }
156 void start (tstamp when) { set (when); iom.reg (this); }
157 void stop () { iom.unreg (this); }
159 template<class O1, class O2>
160 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
161 : callback1<void, time_watcher &>(object,method)
163 ~time_watcher () { stop (); }
168 // run before checking for new events
169 struct check_watcher : watcher, callback1<void, check_watcher &> {
170 void start () { iom.reg (this); }
171 void stop () { iom.unreg (this); }
173 template<class O1, class O2>
174 check_watcher (O1 *object, void (O2::*method)(check_watcher &))
175 : callback1<void, check_watcher &>(object,method)
177 ~check_watcher () { stop (); }
182 // run after checking for any i/o, but before waiting
183 struct idle_watcher : watcher, callback1<void, idle_watcher &> {
184 void start () { iom.reg (this); }
185 void stop () { iom.unreg (this); }
187 template<class O1, class O2>
188 idle_watcher (O1 *object, void (O2::*method)(idle_watcher &))
189 : callback1<void, idle_watcher &>(object,method)
191 ~idle_watcher () { stop (); }