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;
69 void erase_unordered (unsigned int pos)
71 watcher *w = (*this)[size () - 1];
82 io_manager_vec<io_watcher> iow;
85 io_manager_vec<check_watcher> cw;
88 io_manager_vec<time_watcher> tw;
91 io_manager_vec<idle_watcher> iw;
94 template<class watcher>
95 void reg (watcher *w, io_manager_vec<watcher> &queue);
97 template<class watcher>
98 void unreg (watcher *w, io_manager_vec<watcher> &queue);
101 // register a watcher
103 void reg (io_watcher *w); void unreg (io_watcher *w);
106 void reg (time_watcher *w); void unreg (time_watcher *w);
109 void reg (check_watcher *w); void unreg (check_watcher *w);
112 void reg (idle_watcher *w); void unreg (idle_watcher *w);
121 extern io_manager iom; // a singleton, together with it's construction/destruction problems.
124 int active; /* 0 == inactive, else index into respective vector */
126 watcher() : active(0) { }
130 enum { EVENT_READ = 1, EVENT_WRITE = 2 };
132 struct io_watcher : watcher, callback2<void, io_watcher &, short> {
136 void set (int fd_, short events_) { fd = fd_; events = events_; }
138 void set (short events_) { set (fd, events_); }
139 void start () { iom.reg (this); }
140 void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
141 void stop () { iom.unreg (this); }
143 template<class O1, class O2>
144 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
145 : callback2<void, io_watcher &, short>(object,method)
147 ~io_watcher () { stop (); }
152 struct time_watcher : watcher, callback1<void, time_watcher &> {
157 void set (tstamp when) { at = when; }
158 void operator ()() { trigger (); }
159 void start () { iom.reg (this); }
160 void start (tstamp when) { set (when); iom.reg (this); }
161 void stop () { iom.unreg (this); }
163 template<class O1, class O2>
164 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
165 : callback1<void, time_watcher &>(object,method), at(0)
167 ~time_watcher () { stop (); }
172 // run before checking for new events
173 struct check_watcher : watcher, callback1<void, check_watcher &> {
174 void start () { iom.reg (this); }
175 void stop () { iom.unreg (this); }
177 template<class O1, class O2>
178 check_watcher (O1 *object, void (O2::*method)(check_watcher &))
179 : callback1<void, check_watcher &>(object,method)
181 ~check_watcher () { stop (); }
186 // run after checking for any i/o, but before waiting
187 struct idle_watcher : watcher, callback1<void, idle_watcher &> {
188 void start () { iom.reg (this); }
189 void stop () { iom.unreg (this); }
191 template<class O1, class O2>
192 idle_watcher (O1 *object, void (O2::*method)(idle_watcher &))
193 : callback1<void, idle_watcher &>(object,method)
195 ~idle_watcher () { stop (); }