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.4 by pcg, Fri Mar 21 23:17:01 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 40
41 void idle_cb (tstamp &ts); time_watcher *idle; 41 void idle_cb (tstamp &ts); time_watcher *idle;
42public: 42public:
43
44 void reschedule_time_watchers ();
43 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 (io_watcher *w); 48 void unreg (io_watcher *w);
47 void reg (time_watcher *w); 49 void reg (time_watcher *w);
53 ~io_manager (); 55 ~io_manager ();
54}; 56};
55 57
56extern io_manager iom; 58extern io_manager iom;
57 59
60struct io_watcher : callback2<void, int, short> {
58template<class R, class A> 61 template<class O1, class O2>
59class callback { 62 io_watcher (O1 *object, void (O2::*method)(int fd, short revents))
60 struct object { }; 63 : callback2<void, int, short>(object,method)
64 { }
61 65
62 void *obj; 66 ~io_watcher ()
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, void (object::*meth)(A), A arg) = 0;
68 };
69 template<class O1, class O2>
70 struct proxy : proxy_base {
71 virtual R call (void *obj, void (object::*meth)(A), A arg)
72 {
73 ((reinterpret_cast<O1 *>(obj)) ->* (reinterpret_cast<void (O2::*)(A)>(meth)))
74 (arg);
75 }
76 };
77
78 proxy_base *prxy;
79
80public:
81 template<class O1, class O2>
82 callback (O1 *object, void (O2::*method)(A))
83 { 67 {
84 static proxy<O1,O2> p; 68 iom.unreg (this);
85 obj = reinterpret_cast<void *>(object);
86 meth = reinterpret_cast<void (object::*)(A)>(method);
87 prxy = &p;
88 } 69 }
89
90 R call(A arg)
91 {
92 return prxy->call (obj, meth, arg);
93 }
94
95 R operator ()(A arg)
96 {
97 return call (arg);
98 }
99};
100
101struct io_watcher : callback<void, short> {
102 template<class O1, class O2>
103 io_watcher (O1 *object, void (O2::*method)(short revents))
104 : callback<void, short>(object,method)
105 { }
106 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 }
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 : callback1<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 : callback1<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 ()()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines