ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/iom.h
(Generate patch)

Comparing rxvt-unicode/src/iom.h (file contents):
Revision 1.2 by pcg, Wed Nov 26 10:42:34 2003 UTC vs.
Revision 1.6 by pcg, Fri Jan 16 22:11:09 2004 UTC

20#ifndef VPE_IOM_H__ 20#ifndef VPE_IOM_H__
21#define VPE_IOM_H__ 21#define VPE_IOM_H__
22 22
23#include <cassert> 23#include <cassert>
24 24
25#include "callback.h"
25#include "rxvtvec.h" 26#include "rxvtvec.h"
26#include "callback.h"
27 27
28#ifndef IOM_IO
28#define IOM_IO 1 29# define IOM_IO 1
30#endif
31#ifndef IOM_TIME
29#define IOM_TIME 1 32# define IOM_TIME 1
33#endif
30#undef IOM_CHECK 34#ifndef IOM_CHECK
35# define IOM_CHECK 1
36#endif
37#ifndef IOM_IDLE
38# define IOM_IDLE 0
39#endif
40
41typedef double tstamp;
42extern tstamp NOW;
31 43
32#if IOM_IO 44#if IOM_IO
33 typedef double tstamp;
34 extern tstamp NOW;
35
36 struct io_watcher; 45 struct io_watcher;
37#endif 46#endif
38#if IOM_TIME 47#if IOM_TIME
39 struct time_watcher; 48 struct time_watcher;
40#endif 49#endif
41#if IOM_CHECK 50#if IOM_CHECK
42 struct check_watcher; 51 struct check_watcher;
43#endif 52#endif
53#if IOM_IDLE
54 struct idle_watcher;
55#endif
56
57template<class watcher>
58struct io_manager_vec : protected simplevec<watcher *> {
59 friend class io_manager;
60protected:
61 void erase_unordered (unsigned int pos)
62 {
63 watcher *w = (*this)[size () - 1];
64 pop_back ();
65
66 if (size ())
67 if ((*this)[pos] = w)
68 w->active = pos + 1;
69 }
70};
44 71
45class io_manager { 72class io_manager {
46#if IOM_IO 73#if IOM_IO
47 simplevec<io_watcher *> iow; 74 io_manager_vec<io_watcher> iow;
48#endif 75#endif
49#if IOM_CHECK 76#if IOM_CHECK
50 simplevec<check_watcher *> cw; 77 io_manager_vec<check_watcher> cw;
51#endif 78#endif
52#if IOM_TIME 79#if IOM_TIME
53 simplevec<time_watcher *> tw; 80 io_manager_vec<time_watcher> tw;
54 81#endif
55 void idle_cb (time_watcher &w); time_watcher *idle; 82#if IOM_IDLE
83 io_manager_vec<idle_watcher> iw;
56#endif 84#endif
57 85
58 template<class watcher> 86 template<class watcher>
59 void reg (watcher *w, simplevec<watcher *> &queue); 87 void reg (watcher *w, simplevec<watcher *> &queue);
60 88
62 void unreg (watcher *w, simplevec<watcher *> &queue); 90 void unreg (watcher *w, simplevec<watcher *> &queue);
63 91
64public: 92public:
65 // register a watcher 93 // register a watcher
66#if IOM_IO 94#if IOM_IO
67 void reg (io_watcher *w); void unreg (io_watcher *w); 95 void reg (io_watcher *w); void unreg (io_watcher *w);
68#endif 96#endif
69#if IOM_TIME 97#if IOM_TIME
70 void reg (time_watcher *w); void unreg (time_watcher *w); 98 void reg (time_watcher *w); void unreg (time_watcher *w);
71#endif 99#endif
72#if IOM_CHECK 100#if IOM_CHECK
73 void reg (check_watcher *w); void unreg (check_watcher *w); 101 void reg (check_watcher *w); void unreg (check_watcher *w);
102#endif
103#if IOM_IDLE
104 void reg (idle_watcher *w); void unreg (idle_watcher *w);
74#endif 105#endif
75 106
76 void loop (); 107 void loop ();
77 108
78 io_manager (); 109 io_manager ();
79 ~io_manager (); 110 ~io_manager ();
80}; 111};
81 112
82extern io_manager iom; // a singleton, together with it's construction/destruction problems. 113extern io_manager iom; // a singleton, together with it's construction/destruction problems.
83 114
115struct watcher {
116 int active; /* 0 == inactive, else index into respective vector */
117
118 watcher() : active(0) { }
119};
120
84#if IOM_IO 121#if IOM_IO
85enum { EVENT_READ = 1, EVENT_WRITE = 2 }; 122enum { EVENT_READ = 1, EVENT_WRITE = 2 };
86 123
87struct io_watcher : callback2<void, io_watcher &, short> { 124struct io_watcher : watcher, callback2<void, io_watcher &, short> {
88 int fd; 125 int fd;
89 short events; 126 short events;
90 127
128 void set (int fd_, short events_) { fd = fd_; events = events_; }
129
130 void set (short events_) { set (fd, events_); }
131 void start () { iom.reg (this); }
132 void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
133 void stop () { iom.unreg (this); }
134
91 template<class O1, class O2> 135 template<class O1, class O2>
92 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) 136 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
93 : callback2<void, io_watcher &, short>(object,method) 137 : callback2<void, io_watcher &, short>(object,method)
94 { } 138 { }
95 139 ~io_watcher () { stop (); }
96 ~io_watcher ();
97
98 void set(int fd_, short events_) { fd = fd_; events = events_; }
99
100 void set(short events_) { set (fd, events_); }
101 void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
102 void stop () { iom.unreg (this); }
103}; 140};
104#endif 141#endif
105 142
106#if IOM_TIME 143#if IOM_TIME
107enum { TSTAMP_CANCEL = -1 };
108
109struct time_watcher : callback1<void, time_watcher &> { 144struct time_watcher : watcher, callback1<void, time_watcher &> {
110 tstamp at; 145 tstamp at;
111
112 template<class O1, class O2>
113 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
114 : callback1<void, time_watcher &>(object,method)
115 { }
116
117 ~time_watcher ();
118 146
119 void trigger (); 147 void trigger ();
120 148
121 void set (tstamp when) { at = when; } 149 void set (tstamp when) { at = when; }
122 void operator ()() { trigger (); } 150 void operator ()() { trigger (); }
123 void start () { iom.reg (this); } 151 void start () { iom.reg (this); }
124 void start (tstamp when) { set (when); iom.reg (this); } 152 void start (tstamp when) { set (when); iom.reg (this); }
125 void stop () { iom.unreg (this); } 153 void stop () { iom.unreg (this); }
126 154
127 void reset (tstamp when = TSTAMP_CANCEL) 155 template<class O1, class O2>
128 { 156 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
129 stop (); 157 : callback1<void, time_watcher &>(object,method)
130 at = when;
131 } 158 { }
159 ~time_watcher () { stop (); }
132}; 160};
133#endif 161#endif
134 162
135#if IOM_CHECK 163#if IOM_CHECK
136// run before checking for new events 164// run before checking for new events
137struct check_watcher : callback1<void, check_watcher &> { 165struct check_watcher : watcher, callback1<void, check_watcher &> {
166 void start () { iom.reg (this); }
167 void stop () { iom.unreg (this); }
168
138 template<class O1, class O2> 169 template<class O1, class O2>
139 check_watcher (O1 *object, void (O2::*method)(check_watcher &)) 170 check_watcher (O1 *object, void (O2::*method)(check_watcher &))
140 : callback1<void, check_watcher &>(object,method) 171 : callback1<void, check_watcher &>(object,method)
141 { } 172 { }
173 ~check_watcher () { stop (); }
174};
175#endif
142 176
143 ~check_watcher (); 177#if IOM_IDLE
144 178// run after checking for any i/o, but before waiting
179struct idle_watcher : watcher, callback1<void, idle_watcher &> {
145 void start () { iom.reg (this); } 180 void start () { iom.reg (this); }
146 void stop () { iom.unreg (this); } 181 void stop () { iom.unreg (this); }
182
183 template<class O1, class O2>
184 idle_watcher (O1 *object, void (O2::*method)(idle_watcher &))
185 : callback1<void, idle_watcher &>(object,method)
186 { }
187 ~idle_watcher () { stop (); }
147}; 188};
148#endif 189#endif
149 190
150#endif 191#endif
151 192

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines