ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.4
Committed: Fri Mar 21 23:17:01 2003 UTC (21 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.3: +26 -5 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 /*
2 iom.h -- I/O multiplexor
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 #ifndef VPE_IOM_H__
20 #define VPE_IOM_H__
21
22 #include <vector>
23
24 #include <sys/poll.h>
25
26 #include "slog.h"
27
28 typedef double tstamp;
29
30 extern tstamp NOW;
31
32 template<class R, class A> class callback;
33 struct io_watcher;
34 struct time_watcher;
35
36 class io_manager {
37 vector<pollfd> pfs;
38 vector<io_watcher *> iow;
39 vector<time_watcher *> tw; // actually a heap
40
41 void idle_cb (tstamp &ts); time_watcher *idle;
42 public:
43
44 // register a watcher
45 void reg (int fd, short events, io_watcher *w);
46 void unreg (io_watcher *w);
47 void reg (time_watcher *w);
48 void unreg (time_watcher *w);
49
50 void loop ();
51
52 io_manager ();
53 ~io_manager ();
54 };
55
56 extern io_manager iom;
57
58 template<class R, class A>
59 class callback {
60 struct object { };
61
62 void *obj;
63 R (object::*meth)(A arg);
64
65 // a proxy is a kind of recipe on how to call a specific class method
66 struct proxy_base {
67 virtual R call (void *obj, void (object::*meth)(A), A arg) = 0;
68 };
69 template<class O1, class O2>
70 struct proxy : proxy_base {
71 virtual R call (void *obj, void (object::*meth)(A), A arg)
72 {
73 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth)))
74 (arg);
75 }
76 };
77
78 proxy_base *prxy;
79
80 public:
81 template<class O1, class O2>
82 callback (O1 *object, void (O2::*method)(A))
83 {
84 static proxy<O1,O2> p;
85 obj = reinterpret_cast<void *>(object);
86 meth = reinterpret_cast<void (object::*)(A)>(method);
87 prxy = &p;
88 }
89
90 R call(A arg)
91 {
92 return prxy->call (obj, meth, arg);
93 }
94
95 R operator ()(A arg)
96 {
97 return call (arg);
98 }
99 };
100
101 struct io_watcher : callback<void, short> {
102 template<class O1, class O2>
103 io_watcher (O1 *object, void (O2::*method)(short revents))
104 : callback<void, short>(object,method)
105 { }
106
107 void start (int fd, short events)
108 {
109 iom.reg (fd, events, this);
110 }
111
112 void stop ()
113 {
114 iom.unreg (this);
115 }
116 };
117
118 #define TSTAMP_CANCEL -1.
119
120 struct time_watcher : callback<void, tstamp &> {
121 tstamp at;
122
123 template<class O1, class O2>
124 time_watcher (O1 *object, void (O2::*method)(tstamp &))
125 : callback<void, tstamp &>(object,method)
126 { }
127
128 void set (tstamp when);
129 void trigger ();
130
131 void operator ()()
132 {
133 trigger ();
134 }
135
136 void start ();
137 void start (tstamp when)
138 {
139 set (when);
140 }
141
142 void stop ()
143 {
144 iom.unreg (this);
145 }
146
147 void reset (tstamp when = TSTAMP_CANCEL)
148 {
149 stop ();
150 at = when;
151 }
152 };
153
154 #endif
155