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.1 by pcg, Fri Mar 21 20:33:36 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 timestamp; 29typedef double tstamp;
29 30
30extern timestamp 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
56template<class R, class A> 60struct io_watcher : callback2<void, int, short> {
57class 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> 61 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 {
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
94struct 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)) 62 io_watcher (O1 *object, void (O2::*method)(int fd, short revents))
97 : callback<void, short>(object,method) 63 : callback2<void, int, short>(object,method)
98 { 64 { }
99 iom.reg (fd, events, this);
100 }
101 65
102 ~io_watcher () 66 ~io_watcher ()
103 { 67 {
104 iom.unreg (this); 68 iom.unreg (this);
105 } 69 }
70
71 void start (int fd, short events)
72 {
73 iom.reg (fd, events, this);
74 }
75
76 void stop ()
77 {
78 iom.unreg (this);
79 }
106}; 80};
107 81
108struct time_watcher : callback<void, timestamp &> { 82#define TSTAMP_CANCEL -1.
109 timestamp at;
110 83
111 void set (timestamp when); 84struct time_watcher : callback1<void, tstamp &> {
85 bool registered; // already registered?
86 tstamp at;
112 87
113 template<class O1, class O2> 88 template<class O1, class O2>
114 time_watcher (timestamp when, O1 *object, void (O2::*method)(timestamp &)) 89 time_watcher (O1 *object, void (O2::*method)(tstamp &))
115 : callback<void, timestamp &>(object,method) 90 : callback1<void, tstamp &>(object,method)
116 , at(when) 91 , registered(false)
117 { 92 { }
118 iom.reg (this);
119 }
120 93
121 ~time_watcher () 94 ~time_watcher ()
122 { 95 {
123 iom.unreg (this); 96 iom.unreg (this);
124 } 97 }
98
99 void set (tstamp when);
100 void trigger ();
101
102 void operator ()()
103 {
104 trigger ();
105 }
106
107 void start ();
108 void start (tstamp when)
109 {
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;
122 }
125}; 123};
126 124
127#endif 125#endif
128 126

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines