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.11 by pcg, Fri Apr 4 05:26:45 2003 UTC

19#ifndef VPE_IOM_H__ 19#ifndef VPE_IOM_H__
20#define VPE_IOM_H__ 20#define VPE_IOM_H__
21 21
22#include <vector> 22#include <vector>
23 23
24#include <cassert>
25
24#include <sys/poll.h> 26#include <sys/poll.h>
25 27
28#include "callback.h"
26#include "slog.h" 29#include "slog.h"
27 30
28typedef double tstamp; 31typedef double tstamp;
29 32
30extern tstamp NOW; 33extern tstamp NOW;
31 34
32template<class R, class A> class callback;
33struct io_watcher; 35struct io_watcher;
34struct time_watcher; 36struct time_watcher;
35 37
36class io_manager { 38class io_manager {
37 vector<pollfd> pfs; 39 vector<pollfd> pfs;
38 vector<io_watcher *> iow; 40 vector<io_watcher *> iow;
39 vector<time_watcher *> tw; // actually a heap 41 vector<time_watcher *> tw; // actually a heap
40 42
41 void idle_cb (tstamp &ts); time_watcher *idle; 43 void idle_cb (time_watcher &w); time_watcher *idle;
42public: 44public:
43 45
46 void reschedule_time_watchers ();
47
44 // register a watcher 48 // register a watcher
45 void reg (int fd, short events, io_watcher *w); 49 void reg (io_watcher *w);
46 void unreg (io_watcher *w); 50 void unreg (io_watcher *w);
47 void reg (time_watcher *w); 51 void reg (time_watcher *w);
48 void unreg (time_watcher *w); 52 void unreg (time_watcher *w);
49 53
50 void loop (); 54 void loop ();
51 55
52 io_manager (); 56 io_manager ();
53 ~io_manager (); 57 ~io_manager ();
54}; 58};
55 59
56extern io_manager iom; 60extern io_manager iom; // a singleton, together with it's construction/destruction problems.
57 61
58template<class R, class A> 62struct io_watcher : callback2<void, io_watcher &, short> {
59class callback { 63 pollfd *p;
60 struct object { };
61 64
62 void *obj; 65 template<class O1, class O2>
63 R (object::*meth)(A arg); 66 io_watcher (O1 *object, void (O2::*method)(io_watcher &, short))
67 : callback2<void, io_watcher &, short>(object,method)
68 { }
64 69
65 // a proxy is a kind of recipe on how to call a specific class method 70 ~io_watcher ();
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 71
78 proxy_base *prxy; 72 void set(int fd, short events)
79
80public:
81 template<class O1, class O2>
82 callback (O1 *object, void (O2::*method)(A))
83 { 73 {
84 static proxy<O1,O2> p; 74 assert (p);
85 obj = reinterpret_cast<void *>(object); 75 p->fd = fd;
86 meth = reinterpret_cast<void (object::*)(A)>(method); 76 p->events = events;
87 prxy = &p;
88 } 77 }
89 78
90 R call(A arg) 79 void set(short events)
91 { 80 {
92 return prxy->call (obj, meth, arg); 81 assert (p);
82 p->events = events;
93 } 83 }
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 84
107 void start (int fd, short events) 85 void start (int fd, short events)
108 { 86 {
109 iom.reg (fd, events, this); 87 iom.reg (this); // make sure pfd is set
88
89 p->fd = fd;
90 p->events = events;
110 } 91 }
111 92
112 void stop () 93 void stop ()
113 { 94 {
114 iom.unreg (this); 95 iom.unreg (this);
115 } 96 }
116}; 97};
117 98
118#define TSTAMP_CANCEL -1. 99#define TSTAMP_CANCEL -1.
119 100
120struct time_watcher : callback<void, tstamp &> { 101struct time_watcher : callback1<void, time_watcher &> {
102 bool registered; // already registered?
121 tstamp at; 103 tstamp at;
122 104
123 template<class O1, class O2> 105 template<class O1, class O2>
124 time_watcher (O1 *object, void (O2::*method)(tstamp &)) 106 time_watcher (O1 *object, void (O2::*method)(time_watcher &))
125 : callback<void, tstamp &>(object,method) 107 : callback1<void, time_watcher &>(object,method)
108 , registered(false)
126 { } 109 { }
110
111 ~time_watcher ();
127 112
128 void set (tstamp when); 113 void set (tstamp when);
129 void trigger (); 114 void trigger ();
130 115
131 void operator ()() 116 void operator ()()
145 } 130 }
146 131
147 void reset (tstamp when = TSTAMP_CANCEL) 132 void reset (tstamp when = TSTAMP_CANCEL)
148 { 133 {
149 stop (); 134 stop ();
135
150 at = when; 136 at = when;
151 } 137 }
152}; 138};
153 139
154#endif 140#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines