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

Comparing gvpe/src/iom.h (file contents):
Revision 1.6 by pcg, Fri Mar 28 04:05:10 2003 UTC vs.
Revision 1.8 by pcg, Wed Apr 2 03:06:22 2003 UTC

21 21
22#include <vector> 22#include <vector>
23 23
24#include <sys/poll.h> 24#include <sys/poll.h>
25 25
26#include "callback.h"
26#include "slog.h" 27#include "slog.h"
27 28
28typedef double tstamp; 29typedef double tstamp;
29 30
30extern tstamp NOW; 31extern tstamp NOW;
31 32
32template<class R, class A> class callback;
33struct io_watcher; 33struct io_watcher;
34struct time_watcher; 34struct time_watcher;
35 35
36class io_manager { 36class io_manager {
37 vector<pollfd> pfs; 37 vector<pollfd> pfs;
43 43
44 void reschedule_time_watchers (); 44 void reschedule_time_watchers ();
45 45
46 // register a watcher 46 // register a watcher
47 void reg (int fd, short events, io_watcher *w); 47 void reg (int fd, short events, io_watcher *w);
48 void unreg (const io_watcher *w); 48 void unreg (io_watcher *w);
49 void reg (time_watcher *w); 49 void reg (time_watcher *w);
50 void unreg (const time_watcher *w); 50 void unreg (time_watcher *w);
51 51
52 void loop (); 52 void loop ();
53 53
54 io_manager (); 54 io_manager ();
55 ~io_manager (); 55 ~io_manager ();
56}; 56};
57 57
58extern io_manager iom; 58extern io_manager iom;
59
60template<class R, class A>
61class callback {
62 struct object { };
63
64 void *obj;
65 R (object::*meth)(A arg);
66
67 // a proxy is a kind of recipe on how to call a specific class method
68 struct proxy_base {
69 virtual R call (void *obj, R (object::*meth)(A), A arg) = 0;
70 };
71 template<class O1, class O2>
72 struct proxy : proxy_base {
73 virtual R call (void *obj, R (object::*meth)(A), A arg)
74 {
75 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth)))
76 (arg);
77 }
78 };
79
80 proxy_base *prxy;
81
82public:
83 template<class O1, class O2>
84 callback (O1 *object, R (O2::*method)(A))
85 {
86 static proxy<O1,O2> p;
87 obj = reinterpret_cast<void *>(object);
88 meth = reinterpret_cast<R (object::*)(A)>(method);
89 prxy = &p;
90 }
91
92 R call(A arg) const
93 {
94 return prxy->call (obj, meth, arg);
95 }
96
97 R operator ()(A arg) const
98 {
99 return call (arg);
100 }
101};
102 59
103struct io_watcher : callback<void, short> { 60struct io_watcher : callback<void, short> {
104 template<class O1, class O2> 61 template<class O1, class O2>
105 io_watcher (O1 *object, void (O2::*method)(short revents)) 62 io_watcher (O1 *object, void (O2::*method)(short revents))
106 : callback<void, short>(object,method) 63 : callback<void, short>(object,method)
114 void start (int fd, short events) 71 void start (int fd, short events)
115 { 72 {
116 iom.reg (fd, events, this); 73 iom.reg (fd, events, this);
117 } 74 }
118 75
119 void stop () const 76 void stop ()
120 { 77 {
121 iom.unreg (this); 78 iom.unreg (this);
122 } 79 }
123}; 80};
124 81
151 void start (tstamp when) 108 void start (tstamp when)
152 { 109 {
153 set (when); 110 set (when);
154 } 111 }
155 112
156 void stop () const 113 void stop ()
157 { 114 {
158 iom.unreg (this); 115 iom.unreg (this);
159 } 116 }
160 117
161 void reset (tstamp when = TSTAMP_CANCEL) 118 void reset (tstamp when = TSTAMP_CANCEL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines