Mercurial > projects > dynamin
annotate dynamin/core/event.d @ 79:e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Sat, 06 Feb 2010 15:45:53 -0600 |
parents | 651082a9b364 |
children | 73060bc3f004 |
rev | line source |
---|---|
0 | 1 // Written in the D programming language |
2 // www.digitalmars.com/d/ | |
3 | |
4 /* | |
5 * The contents of this file are subject to the Mozilla Public License Version | |
6 * 1.1 (the "License"); you may not use this file except in compliance with | |
7 * the License. You may obtain a copy of the License at | |
8 * http://www.mozilla.org/MPL/ | |
9 * | |
10 * Software distributed under the License is distributed on an "AS IS" basis, | |
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
12 * for the specific language governing rights and limitations under the | |
13 * License. | |
14 * | |
15 * The Original Code is the Dynamin library. | |
16 * | |
17 * The Initial Developer of the Original Code is Jordan Miner. | |
18 * Portions created by the Initial Developer are Copyright (C) 2006-2009 | |
19 * the Initial Developer. All Rights Reserved. | |
20 * | |
21 * Contributor(s): | |
22 * Jordan Miner <jminer7@gmail.com> | |
23 * | |
24 */ | |
25 | |
26 module dynamin.core.event; | |
27 | |
28 import tango.io.Stdout; | |
29 import dynamin.core.global; | |
30 import tango.core.Exception; | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
31 public import tango.core.Traits; |
0 | 32 |
33 /** | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
34 * A struct used to notify handlers of an event. It is similar to .NET's events. |
0 | 35 * Here is an example of its usage: |
36 * ----- | |
37 * class Control { | |
38 * public { | |
39 * protected void whenPainting(PaintingEventArgs e) { | |
40 * /+ painting code goes here +/ | |
41 * } | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
42 * Event!(whenPainting) painting; |
0 | 43 * this() { |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
44 * painting.mainHandler = &whenPainting; |
0 | 45 * } |
46 * } | |
47 * } | |
48 * ----- | |
49 * Then to add a handler to it: | |
50 * ----- | |
51 * Control control = new Control(); | |
52 * control.painting += (PaintingEventArgs e) { | |
53 * /+ painting code goes here +/ | |
54 * }; | |
55 * ----- | |
56 * And fire it in the Control like this: | |
57 * ----- | |
58 * painting(new PaintingEventArgs()); | |
59 * ----- | |
60 * Not as easy to use as I would like, but still much better than Java's | |
61 * event handling. | |
62 * When the event is fired, all the handlers are called first, followed | |
63 * by the delegate passed into the constructor. | |
64 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
65 struct Event(alias mainHandler_) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
66 alias ParameterTupleOf!(mainHandler_)[0] ArgsType; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
67 /// void delegate(ArgsType e) |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
68 public alias void delegate(ArgsType e) Handler; |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
69 /// void delegate(ArgsType e) |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
70 public alias void delegate(ArgsType e) Dispatcher; |
0 | 71 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
72 Handler[] handlers; |
79
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
73 private void* ptr; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
74 private void function(ArgsType e) mainHandler; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
75 private void function(ArgsType e) dispatcher; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
76 |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
77
diff
changeset
|
77 void setUp(Handler mainHandler, Dispatcher dispatcher = null) { |
79
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
78 if(mainHandler.ptr != dispatcher.ptr && dispatcher.ptr != null) |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
79 throw new Exception("mainHandler.ptr must equal dispatcher.ptr"); |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
80 ptr = mainHandler.ptr; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
81 this.mainHandler = mainHandler.funcptr; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
82 this.dispatcher = dispatcher.funcptr; |
78
651082a9b364
Add Event.setUp() and use in place of mainHandler and dispatcher.
Jordan Miner <jminer7@gmail.com>
parents:
77
diff
changeset
|
83 } |
79
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
84 |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
85 void defaultDispatch(ArgsType e) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
86 callHandlers(e); |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
87 callMainHandler(e); |
0 | 88 } |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
89 |
0 | 90 /** |
91 * Calls all the handlers added to this event, passing e to them. | |
92 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
93 void opCall(ArgsType e) { |
0 | 94 if(e is null) |
95 Stdout("Warning: EventArgs null").newline; | |
79
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
96 if(!dispatcher) { |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
97 defaultDispatch(e); |
79
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
98 return; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
99 } |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
100 Dispatcher dg; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
101 dg.ptr = ptr; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
102 dg.funcptr = dispatcher; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
103 dg(e); |
0 | 104 } |
105 /** | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
106 * Adds the specified handler to this event. The handler will be called |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
107 * when the event is fired. |
0 | 108 */ |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
109 void opAddAssign(Handler handler) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
110 if(!handler.funcptr) throw new Exception("handler cannot be null"); |
0 | 111 handlers.length = handlers.length + 1; |
112 handlers[length-1] = handler; | |
113 // TODO: use a list? | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
114 //handlers.add(handler); |
0 | 115 } |
116 /// ditto | |
117 void opAddAssign(void delegate() handler) { | |
118 struct Foo { | |
119 void delegate() handler; | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
120 void wrapper(ArgsType e) { handler(); } |
0 | 121 } |
122 Foo* f = new Foo; | |
123 f.handler = handler; | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
124 *this += &f.wrapper; |
0 | 125 // I really wish D could do this: |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
126 //this += (ArgsType e) { handler(); }; |
0 | 127 } |
128 /// TODO: implement this method | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
129 void opSubAssign(Handler handler) { |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
130 throw new Exception("removing handlers not yet implemented"); |
0 | 131 } |
132 /** | |
133 * Calls the handlers (not including the main handler) added to this event. | |
134 * Only use this method from a method that does custom dispatching. | |
135 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
136 void callHandlers(ArgsType e) { |
0 | 137 foreach(handler; handlers) |
138 handler(e); | |
139 } | |
140 /** | |
141 * Calls the main handler unless the StopEventArgs.stopped has been | |
142 * set to true. | |
143 * Only use this method from a method that does custom dispatching. | |
144 */ | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
145 void callMainHandler(ArgsType e) { |
0 | 146 auto stopEventArgs = cast(StopEventArgs)e; |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
147 // if e is an instance of StopEventArgs, then check if it is stopped |
79
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
148 if(stopEventArgs is null || !stopEventArgs.stopped) { |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
149 Handler dg; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
150 dg.ptr = ptr; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
151 dg.funcptr = mainHandler; |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
152 dg(e); |
e7595d58f8a3
Reduce the size of an Event by sharing the mainHandler and dispatcher frame pointers.
Jordan Miner <jminer7@gmail.com>
parents:
78
diff
changeset
|
153 } |
0 | 154 } |
155 } | |
156 | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
157 // usage: mixin Event!(whenMoved) moved; |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
158 // mixin Event!(whenMoved, dispatchMoved) moved; |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
159 template Event2(alias mainHandler, alias dispatcher) { |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
160 private: |
0 | 161 alias ParameterTupleOf!(mainHandler)[0] ArgsType; |
162 /// void delegate(ArgsType e) | |
163 public alias void delegate(ArgsType e) Handler; | |
164 /// void delegate(ArgsType e) | |
165 public alias void delegate(ArgsType e) Dispatcher; | |
166 | |
167 // TODO: use a list-like struct? | |
168 Handler[] handlers; | |
169 public: | |
170 | |
171 /** | |
172 * Calls all the handlers added to this event, passing e to them. | |
173 */ | |
174 void opCall(ArgsType e) { | |
175 if(e is null) | |
176 Stdout("Warning: EventArgs null").newline; | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
177 dispatcher(e); |
0 | 178 } |
179 /** | |
180 * Adds the specified handler to this event. The handler will be called | |
181 * when the event is fired. | |
182 */ | |
183 void opAddAssign(Handler handler) { | |
184 if(!handler.funcptr) throw new Exception("handler cannot be null"); | |
185 handlers.length = handlers.length + 1; | |
186 handlers[length-1] = handler; | |
187 // TODO: use a list? | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
188 //handlers.add(handler); |
0 | 189 } |
190 /// ditto | |
191 void opAddAssign(void delegate() handler) { | |
192 struct Foo { | |
193 void delegate() handler; | |
194 void wrapper(ArgsType e) { handler(); } | |
195 } | |
196 Foo* f = new Foo; | |
197 f.handler = handler; | |
198 this += &f.wrapper; | |
199 // I really wish D could do this: | |
200 //this += (ArgsType e) { handler(); }; | |
201 } | |
202 /// TODO: implement this method | |
203 void opSubAssign(Handler handler) { | |
204 throw new Exception("removing handlers not yet implemented"); | |
205 } | |
206 /** | |
207 * Calls the handlers (not including the main handler) added to this event. | |
208 * Only use this method from a method that does custom dispatching. | |
209 */ | |
210 void callHandlers(ArgsType e) { | |
211 foreach(handler; handlers) | |
212 handler(e); | |
213 } | |
214 /** | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
215 * Calls the main handler unless the StopEventArgs.stopped has been |
0 | 216 * set to true. |
217 * Only use this method from a method that does custom dispatching. | |
218 */ | |
219 void callMainHandler(ArgsType e) { | |
220 auto stopEventArgs = cast(StopEventArgs)e; | |
221 // if e is an instance of StopEventArgs, then check if it is stopped | |
222 if(stopEventArgs is null || !stopEventArgs.stopped) | |
223 mainHandler(e); | |
224 } | |
225 } | |
226 | |
7
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
227 template Event2(alias mainHandler) { |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
228 alias ParameterTupleOf!(mainHandler)[0] ArgsType; |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
229 void defaultDispatch(ArgsType e) { |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
230 callHandlers(e); |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
231 callMainHandler(e); |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
232 } |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
233 mixin Event2!(mainHandler, defaultDispatch); |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
234 } |
1311fae1ca9b
Change template mixin (still unused) to take a dispatcher
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
235 |
0 | 236 /// The base class for passing arguments to event handlers. |
237 // TODO: shorter name? | |
238 class EventArgs { | |
239 } | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
240 /// |
0 | 241 class StopEventArgs : EventArgs { |
242 /** | |
10
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
243 * If stopped is set to true, then the Control will not respond to |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
244 * the event. For instance, if a key is typed while a text box is focused, |
ccc108b25a0a
Convert to using a struct for events. Fix some comments too.
Jordan Miner <jminer7@gmail.com>
parents:
7
diff
changeset
|
245 * but a handler sets stopped to true, the text box will not |
0 | 246 * respond to the key. |
247 */ | |
248 bool stopped = false; | |
249 } |