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.3 by pcg, Fri Mar 21 21:21:02 2003 UTC vs.
Revision 1.9 by pcg, Wed Apr 2 05:15:00 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;
38 vector<io_watcher *> iow; 38 vector<const io_watcher *> iow;
39 vector<time_watcher *> tw; // actually a heap 39 vector<time_watcher *> tw; // actually a heap
40
41 void idle_cb (tstamp &ts); time_watcher *idle;
40public: 42public:
43
44 void reschedule_time_watchers ();
41 45
42 // register a watcher 46 // register a watcher
43 void reg (int fd, short events, io_watcher *w); 47 void reg (int fd, short events, io_watcher *w);
44 void unreg (io_watcher *w); 48 void unreg (io_watcher *w);
45 void reg (time_watcher *w); 49 void reg (time_watcher *w);
51 ~io_manager (); 55 ~io_manager ();
52}; 56};
53 57
54extern io_manager iom; 58extern io_manager iom;
55 59
60struct io_watcher : callback2<void, int, short> {
56template<class R, class A> 61 template<class O1, class O2>
57class callback { 62 io_watcher (O1 *object, void (O2::*method)(int fd, short revents))
58 struct object { }; 63 : callback2<void, int, short>(object,method)
64 { }
59 65
60 void *obj; 66 ~io_watcher ()
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
78public:
79 template<class O1, class O2>
80 callback (O1 *object, void (O2::*method)(A))
81 { 67 {
82 static proxy<O1,O2> p; 68 iom.unreg (this);
83 obj = reinterpret_cast<void *>(object);
84 meth = reinterpret_cast<void (object::*)(A)>(method);
85 prxy = &p;
86 } 69 }
87 70
88 R call(A arg) 71 void start (int fd, short events)
89 { 72 {
90 return prxy->call (obj, meth, arg); 73 iom.reg (fd, events, this);
91 } 74 }
92 75
93 void stop () 76 void stop ()
94 { 77 {
95 iom.unreg (this); 78 iom.unreg (this);
96 } 79 }
97}; 80};
98 81
99struct io_watcher : callback<void, short> { 82#define TSTAMP_CANCEL -1.
100 template<class O1, class O2>
101 io_watcher (O1 *object, void (O2::*method)(short revents))
102 : callback<void, short>(object,method)
103 { }
104 83
105 void start (int fd, short events)
106 {
107 iom.reg (fd, events, this);
108 }
109
110};
111
112struct time_watcher : callback<void, tstamp &> { 84struct time_watcher : callback1<void, tstamp &> {
85 bool registered; // already registered?
113 tstamp at; 86 tstamp at;
114 87
115 template<class O1, class O2> 88 template<class O1, class O2>
116 time_watcher (O1 *object, void (O2::*method)(tstamp &)) 89 time_watcher (O1 *object, void (O2::*method)(tstamp &))
117 : callback<void, tstamp &>(object,method) 90 : callback1<void, tstamp &>(object,method)
91 , registered(false)
118 { } 92 { }
119 93
120 void set (tstamp when); 94 ~time_watcher ()
121
122 void trigger ()
123 { 95 {
124 call (at); 96 iom.unreg (this);
125 } 97 }
126 98
99 void set (tstamp when);
100 void trigger ();
101
102 void operator ()()
103 {
104 trigger ();
105 }
106
107 void start ();
127 void start (tstamp when = NOW) 108 void start (tstamp when)
128 { 109 {
129 set (when); 110 set (when);
111 }
112
113 void stop ()
114 {
115 iom.unreg (this);
116 }
117
118 void reset (tstamp when = TSTAMP_CANCEL)
119 {
120 stop ();
121 at = when;
130 } 122 }
131}; 123};
132 124
133#endif 125#endif
134 126

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines