ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/iom.h
Revision: 1.1
Committed: Fri Mar 21 20:33:36 2003 UTC (21 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.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 timestamp;
29    
30     extern timestamp 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    
94     struct io_watcher : callback<void, short> {
95     template<class O1, class O2>
96     io_watcher (int fd, short events, O1 *object, void (O2::*method)(short revents))
97     : callback<void, short>(object,method)
98     {
99     iom.reg (fd, events, this);
100     }
101    
102     ~io_watcher ()
103     {
104     iom.unreg (this);
105     }
106     };
107    
108     struct time_watcher : callback<void, timestamp &> {
109     timestamp at;
110    
111     void set (timestamp when);
112    
113     template<class O1, class O2>
114     time_watcher (timestamp when, O1 *object, void (O2::*method)(timestamp &))
115     : callback<void, timestamp &>(object,method)
116     , at(when)
117     {
118     iom.reg (this);
119     }
120    
121     ~time_watcher ()
122     {
123     iom.unreg (this);
124     }
125     };
126    
127     #endif
128