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.3 by pcg, Sat Nov 29 18:42:07 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;
81#endif
82#if IOM_IDLE
83 io_manager_vec<idle_watcher> iw;
54#endif 84#endif
55 85
56 template<class watcher> 86 template<class watcher>
57 void reg (watcher *w, simplevec<watcher *> &queue); 87 void reg (watcher *w, simplevec<watcher *> &queue);
58 88
60 void unreg (watcher *w, simplevec<watcher *> &queue); 90 void unreg (watcher *w, simplevec<watcher *> &queue);
61 91
62public: 92public:
63 // register a watcher 93 // register a watcher
64#if IOM_IO 94#if IOM_IO
65 void reg (io_watcher *w); void unreg (io_watcher *w); 95 void reg (io_watcher *w); void unreg (io_watcher *w);
66#endif 96#endif
67#if IOM_TIME 97#if IOM_TIME
68 void reg (time_watcher *w); void unreg (time_watcher *w); 98 void reg (time_watcher *w); void unreg (time_watcher *w);
69#endif 99#endif
70#if IOM_CHECK 100#if IOM_CHECK
71 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);
72#endif 105#endif
73 106
74 void loop (); 107 void loop ();
75 108
76 io_manager (); 109 io_manager ();
77 ~io_manager (); 110 ~io_manager ();
78}; 111};
79 112
80extern 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.
81 114
115struct watcher {
116 int active; /* 0 == inactive, else index into respective vector */
117
118 watcher() : active(0) { }
119};
120
82#if IOM_IO 121#if IOM_IO
83enum { EVENT_READ = 1, EVENT_WRITE = 2 }; 122enum { EVENT_READ = 1, EVENT_WRITE = 2 };
84 123
85struct io_watcher : callback2<void, io_watcher &, short> { 124struct io_watcher : watcher, callback2<void, io_watcher &, short> {
86 int fd; 125 int fd;
87 short events; 126 short events;
88 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
89 template<class O1, class O2> 135 template<class O1, class O2>
90 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short)) 136 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
91 : callback2<void, io_watcher &, short>(object,method) 137 : callback2<void, io_watcher &, short>(object,method)
92 { } 138 { }
93 139 ~io_watcher () { stop (); }
94 ~io_watcher ();
95
96 void set(int fd_, short events_) { fd = fd_; events = events_; }
97
98 void set(short events_) { set (fd, events_); }
99 void start (int fd_, short events_) { set (fd_, events_); iom.reg (this); }
100 void stop () { iom.unreg (this); }
101}; 140};
102#endif 141#endif
103 142
104#if IOM_TIME 143#if IOM_TIME
105enum { TSTAMP_CANCEL = -1 };
106
107struct time_watcher : callback1<void, time_watcher &> { 144struct time_watcher : watcher, callback1<void, time_watcher &> {
108 tstamp at; 145 tstamp at;
109
110 template<class O1, class O2>
111 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
112 : callback1<void, time_watcher &>(object,method)
113 { }
114
115 ~time_watcher ();
116 146
117 void trigger (); 147 void trigger ();
118 148
119 void set (tstamp when) { at = when; } 149 void set (tstamp when) { at = when; }
120 void operator ()() { trigger (); } 150 void operator ()() { trigger (); }
121 void start () { iom.reg (this); } 151 void start () { iom.reg (this); }
122 void start (tstamp when) { set (when); iom.reg (this); } 152 void start (tstamp when) { set (when); iom.reg (this); }
123 void stop () { iom.unreg (this); } 153 void stop () { iom.unreg (this); }
124 154
125 void reset (tstamp when = TSTAMP_CANCEL) 155 template<class O1, class O2>
126 { 156 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
127 stop (); 157 : callback1<void, time_watcher &>(object,method)
128 at = when;
129 } 158 { }
159 ~time_watcher () { stop (); }
130}; 160};
131#endif 161#endif
132 162
133#if IOM_CHECK 163#if IOM_CHECK
134// run before checking for new events 164// run before checking for new events
135struct 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
136 template<class O1, class O2> 169 template<class O1, class O2>
137 check_watcher (O1 *object, void (O2::*method)(check_watcher &)) 170 check_watcher (O1 *object, void (O2::*method)(check_watcher &))
138 : callback1<void, check_watcher &>(object,method) 171 : callback1<void, check_watcher &>(object,method)
139 { } 172 { }
173 ~check_watcher () { stop (); }
174};
175#endif
140 176
141 ~check_watcher (); 177#if IOM_IDLE
142 178// run after checking for any i/o, but before waiting
179struct idle_watcher : watcher, callback1<void, idle_watcher &> {
143 void start () { iom.reg (this); } 180 void start () { iom.reg (this); }
144 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 (); }
145}; 188};
146#endif 189#endif
147 190
148#endif 191#endif
149 192

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines