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.5 by pcg, Wed Mar 26 01:58:46 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;
39 vector<time_watcher *> tw; // actually a heap 39 vector<time_watcher *> tw; // actually a heap
40 40
41 void idle_cb (tstamp &ts); time_watcher *idle; 41 void idle_cb (tstamp &ts); time_watcher *idle;
42public: 42public:
43 43
44 void reschedule_time_watchers ();
45
44 // register a watcher 46 // register a watcher
45 void reg (int fd, short events, io_watcher *w); 47 void reg (int fd, short events, io_watcher *w);
46 void unreg (const io_watcher *w); 48 void unreg (io_watcher *w);
47 void reg (time_watcher *w); 49 void reg (time_watcher *w);
48 void unreg (const time_watcher *w); 50 void unreg (time_watcher *w);
49 51
50 void loop (); 52 void loop ();
51 53
52 io_manager (); 54 io_manager ();
53 ~io_manager (); 55 ~io_manager ();
54}; 56};
55 57
56extern io_manager iom; 58extern io_manager iom;
57 59
58template<class R, class A>
59class callback {
60 struct object { };
61
62 void *obj;
63 R (object::*meth)(A arg);
64
65 // a proxy is a kind of recipe on how to call a specific class method
66 struct proxy_base {
67 virtual R call (void *obj, R (object::*meth)(A), A arg) = 0;
68 };
69 template<class O1, class O2>
70 struct proxy : proxy_base {
71 virtual R call (void *obj, R (object::*meth)(A), A arg)
72 {
73 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<R (O2::*)(A)>(meth)))
74 (arg);
75 }
76 };
77
78 proxy_base *prxy;
79
80public:
81 template<class O1, class O2>
82 callback (O1 *object, R (O2::*method)(A))
83 {
84 static proxy<O1,O2> p;
85 obj = reinterpret_cast<void *>(object);
86 meth = reinterpret_cast<R (object::*)(A)>(method);
87 prxy = &p;
88 }
89
90 R call(A arg) const
91 {
92 return prxy->call (obj, meth, arg);
93 }
94
95 R operator ()(A arg) const
96 {
97 return call (arg);
98 }
99};
100
101struct io_watcher : callback<void, short> { 60struct io_watcher : callback<void, short> {
102 template<class O1, class O2> 61 template<class O1, class O2>
103 io_watcher (O1 *object, void (O2::*method)(short revents)) 62 io_watcher (O1 *object, void (O2::*method)(short revents))
104 : callback<void, short>(object,method) 63 : callback<void, short>(object,method)
105 { } 64 { }
106 65
66 ~io_watcher ()
67 {
68 iom.unreg (this);
69 }
70
107 void start (int fd, short events) 71 void start (int fd, short events)
108 { 72 {
109 iom.reg (fd, events, this); 73 iom.reg (fd, events, this);
110 } 74 }
111 75
112 void stop () const 76 void stop ()
113 { 77 {
114 iom.unreg (this); 78 iom.unreg (this);
115 } 79 }
116}; 80};
117 81
118#define TSTAMP_CANCEL -1. 82#define TSTAMP_CANCEL -1.
119 83
120struct time_watcher : callback<void, tstamp &> { 84struct time_watcher : callback<void, tstamp &> {
85 bool registered; // already registered?
121 tstamp at; 86 tstamp at;
122 87
123 template<class O1, class O2> 88 template<class O1, class O2>
124 time_watcher (O1 *object, void (O2::*method)(tstamp &)) 89 time_watcher (O1 *object, void (O2::*method)(tstamp &))
125 : callback<void, tstamp &>(object,method) 90 : callback<void, tstamp &>(object,method)
91 , registered(false)
126 { } 92 { }
93
94 ~time_watcher ()
95 {
96 iom.unreg (this);
97 }
127 98
128 void set (tstamp when); 99 void set (tstamp when);
129 void trigger (); 100 void trigger ();
130 101
131 void operator ()() 102 void operator ()()
137 void start (tstamp when) 108 void start (tstamp when)
138 { 109 {
139 set (when); 110 set (when);
140 } 111 }
141 112
142 void stop () const 113 void stop ()
143 { 114 {
144 iom.unreg (this); 115 iom.unreg (this);
145 } 116 }
146 117
147 void reset (tstamp when = TSTAMP_CANCEL) 118 void reset (tstamp when = TSTAMP_CANCEL)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines