ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.2
Committed: Fri Mar 21 21:17:02 2003 UTC (21 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +23 -17 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 public:
41
42 // register a watcher
43 void reg (int fd, short events, io_watcher *w);
44 void unreg (io_watcher *w);
45 void reg (time_watcher *w);
46 void unreg (time_watcher *w);
47
48 void loop ();
49
50 io_manager ();
51 ~io_manager ();
52 };
53
54 extern io_manager iom;
55
56 template<class R, class A>
57 class callback {
58 struct object { };
59
60 void *obj;
61 R (object::*meth)(A arg);
62
63 // a proxy is a kind of recipe on how to call a specific class method
64 struct proxy_base {
65 virtual R call (void *obj, void (object::*meth)(A), A arg) = 0;
66 };
67 template<class O1, class O2>
68 struct proxy : proxy_base {
69 virtual R call (void *obj, void (object::*meth)(A), A arg)
70 {
71 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth)))
72 (arg);
73 }
74 };
75
76 proxy_base *prxy;
77
78 public:
79 template<class O1, class O2>
80 callback (O1 *object, void (O2::*method)(A))
81 {
82 static proxy<O1,O2> p;
83 obj = reinterpret_cast<void *>(object);
84 meth = reinterpret_cast<void (object::*)(A)>(method);
85 prxy = &p;
86 }
87
88 R call(A arg)
89 {
90 return prxy->call (obj, meth, arg);
91 }
92
93 void stop ()
94 {
95 iom.unreg (this);
96 }
97 };
98
99 struct io_watcher : callback<void, short> {
100 template<class O1, class O2>
101 io_watcher (O1 *object, void (O2::*method)(short revents))
102 : callback<void, short>(object,method)
103 { }
104
105 void start (int fd, short events)
106 {
107 iom.reg (fd, events, this);
108 }
109
110 };
111
112 struct time_watcher : callback<void, tstamp &> {
113 tstamp at;
114
115 template<class O1, class O2>
116 time_watcher (O1 *object, void (O2::*method)(tstamp &))
117 : callback<void, tstamp &>(object,method)
118 { }
119
120 void set (tstamp when);
121
122 void trigger ()
123 {
124 call (at);
125 }
126
127 void start (tstamp when)
128 {
129 set (when);
130 }
131 };
132
133 #endif
134