author | fm |
Tue, 24 Apr 2007 18:17:40 +0200 | |
changeset 2325 | 634b74929d2d |
parent 2101 | 58a78b51f78e |
child 3940 | d5fb7f7790eb |
permissions | -rw-r--r-- |
88 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
3 |
All Rights Reserved |
88 | 4 |
|
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
||
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
13 |
"{ Package: 'stx:libview2' }" |
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
14 |
|
55 | 15 |
Model subclass:#Plug |
2100 | 16 |
instanceVariableNames:'simulatedProtocol inheritedClasses' |
162 | 17 |
classVariableNames:'' |
18 |
poolDictionaries:'' |
|
19 |
category:'Kernel-Objects' |
|
54 | 20 |
! |
21 |
||
22 |
!Plug class methodsFor:'documentation'! |
|
23 |
||
88 | 24 |
copyright |
25 |
" |
|
26 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
114
e577a2f332d0
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
96
diff
changeset
|
27 |
All Rights Reserved |
88 | 28 |
|
29 |
This software is furnished under a license and may be used |
|
30 |
only in accordance with the terms of that license and with the |
|
31 |
inclusion of the above copyright notice. This software may not |
|
32 |
be provided or otherwise made available to, or used by, any |
|
33 |
other person. No title to or ownership of the software is |
|
34 |
hereby transferred. |
|
35 |
" |
|
36 |
! |
|
37 |
||
54 | 38 |
documentation |
39 |
" |
|
228 | 40 |
A Plug is an object which simulates a protocol and evaluates |
54 | 41 |
a corresponding block when receiving messages. |
2100 | 42 |
A plug's interface can be changed dynamically. |
43 |
A plug can also be told to simulate inheriting messages from other classes, |
|
44 |
even multiple inheritance is possible. |
|
228 | 45 |
|
226 | 46 |
Its main use is for the demo doIts, to play the role of a model, |
228 | 47 |
when no actual modelClass is available for the demonstration. |
48 |
However, it can be used wherever some object is needed which responds to |
|
49 |
some protocol AND you do not want to add a class for it |
|
50 |
(lightWeight objects). |
|
51 |
||
52 |
There is a slight performance penalty - compared to `normal' objects, |
|
53 |
getting `normal' messages, though. |
|
224 | 54 |
|
55 |
[author:] |
|
56 |
Claus Gittinger |
|
228 | 57 |
|
58 |
[see also:] |
|
59 |
Model |
|
54 | 60 |
" |
61 |
! |
|
62 |
||
63 |
examples |
|
64 |
" |
|
2100 | 65 |
a simple plug: |
228 | 66 |
[exBegin] |
2100 | 67 |
|plug| |
54 | 68 |
|
2100 | 69 |
plug := Plug new. |
70 |
plug respondTo:#foo with:[Transcript showCR:'Plug received foo']. |
|
71 |
plug respondTo:#foo: with:[:arg | Transcript showCR:'Plug received foo: ', arg printString]. |
|
54 | 72 |
|
2100 | 73 |
plug foo. |
74 |
plug foo:'some argument' |
|
228 | 75 |
[exEnd] |
162 | 76 |
|
2101 | 77 |
|
360 | 78 |
using a plug as a generator (simulates a readStream): |
79 |
[exBegin] |
|
2100 | 80 |
|generator num| |
360 | 81 |
|
2100 | 82 |
num := 0. |
83 |
generator := Plug new. |
|
84 |
generator respondTo:#next with:[num := num + 1. num]. |
|
85 |
generator respondTo:#atEnd with:[false]. |
|
360 | 86 |
|
2100 | 87 |
10 timesRepeat:[ |
88 |
Transcript showCR:(generator next) |
|
89 |
] |
|
360 | 90 |
[exEnd] |
162 | 91 |
|
2101 | 92 |
|
93 |
simulating a big collection: |
|
94 |
[exBegin] |
|
95 |
|virtualList| |
|
96 |
||
97 |
virtualList := Plug new. |
|
98 |
virtualList inheritFrom:SequenceableCollection. |
|
99 |
virtualList respondTo:#size with:[ 1000000 ]. |
|
100 |
virtualList respondTo:#at: with:[:lineNr | 'List line Nr. ' , lineNr printString ]. |
|
101 |
virtualList inspect. |
|
102 |
[exEnd] |
|
103 |
||
104 |
||
162 | 105 |
simulating ``instance variables'': |
106 |
(actually, this is somewhat expensive - the contexts locals are used for them ...) |
|
107 |
be careful with unintended variable sharing (if plugs are created in a loop ..) |
|
228 | 108 |
[exBegin] |
2100 | 109 |
|plug1 plug2 local1 local2| |
110 |
||
111 |
plug1 := Plug new. |
|
112 |
plug1 respondTo:#get with:[local1]. |
|
113 |
plug1 respondTo:#set: with:[:arg | local1 := arg]. |
|
162 | 114 |
|
2100 | 115 |
plug2 := Plug new. |
116 |
plug2 respondTo:#get with:[local2]. |
|
117 |
plug2 respondTo:#set: with:[:arg | local2 := arg]. |
|
118 |
||
119 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
|
120 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
121 |
||
122 |
plug1 set:5. |
|
123 |
plug2 set:17. |
|
162 | 124 |
|
2100 | 125 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
126 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
127 |
[exEnd] |
|
128 |
||
2101 | 129 |
|
2100 | 130 |
simulating a big list in a ListView |
131 |
(real applications would read the lines from a database or file): |
|
132 |
[exBegin] |
|
133 |
|virtualList top lv| |
|
162 | 134 |
|
2100 | 135 |
virtualList := Plug new. |
136 |
virtualList inheritFrom:SequenceableCollection. |
|
137 |
virtualList respondTo:#size with:[ 1000000 ]. |
|
138 |
virtualList respondTo:#at: with:[:lineNr | 'List line Nr. ' , lineNr printString ]. |
|
162 | 139 |
|
2100 | 140 |
top := StandardSystemView extent:200@200. |
162 | 141 |
|
2100 | 142 |
lv := ScrollableView for:ListView in:top. |
143 |
lv origin:0.0 @ 0.0 corner:1.0 @ 1.0. |
|
144 |
lv list:virtualList expandTabs:false scanForNonStrings:false includesNonStrings:false. |
|
145 |
||
146 |
top open. |
|
228 | 147 |
[exEnd] |
54 | 148 |
" |
149 |
! ! |
|
150 |
||
151 |
!Plug class methodsFor:'instance creation'! |
|
152 |
||
153 |
new |
|
232 | 154 |
^ super basicNew privateInitialize |
228 | 155 |
|
156 |
"Modified: 27.4.1996 / 16:16:59 / cg" |
|
54 | 157 |
! ! |
158 |
||
137 | 159 |
!Plug methodsFor:'initialization'! |
160 |
||
232 | 161 |
privateInitialize |
228 | 162 |
"this method is NOT called `#initialize' to allow plugging that |
137 | 163 |
selector ..." |
164 |
||
165 |
simulatedProtocol := IdentityDictionary new. |
|
228 | 166 |
|
167 |
"Modified: 27.4.1996 / 16:15:45 / cg" |
|
168 |
"Created: 27.4.1996 / 16:17:07 / cg" |
|
137 | 169 |
! ! |
170 |
||
54 | 171 |
!Plug methodsFor:'message sending'! |
172 |
||
1760 | 173 |
at:key |
174 |
"catch this one - its so common" |
|
175 |
||
176 |
^ self doesNotUnderstand:(Message selector:#at: arguments:(Array with:key)) |
|
177 |
! |
|
178 |
||
179 |
at:key put:value |
|
180 |
^ self doesNotUnderstand:(Message selector:#at:put: arguments:(Array with:key with:value)) |
|
181 |
! |
|
182 |
||
54 | 183 |
doesNotUnderstand:aMessage |
228 | 184 |
"catch unhandled messages by looking in my simulated protocol |
185 |
definition; if there is some block for it, return its value. |
|
186 |
Otherwise, fall into the real doesNotUnderstand error." |
|
187 |
||
54 | 188 |
|block| |
189 |
||
2041 | 190 |
block := simulatedProtocol at:(aMessage selector) ifAbsent:[]. |
2100 | 191 |
block notNil ifTrue:[ |
192 |
^ block valueWithArguments:(aMessage arguments) |
|
54 | 193 |
]. |
2100 | 194 |
inheritedClasses notNil ifTrue:[ |
195 |
inheritedClasses do:[:eachClass | |
|
196 |
|method| |
|
197 |
||
198 |
method := eachClass lookupMethodFor:(aMessage selector). |
|
199 |
method notNil ifTrue:[ |
|
200 |
^ method valueWithReceiver:self arguments:(aMessage arguments). |
|
201 |
]. |
|
202 |
]. |
|
203 |
]. |
|
204 |
^ super doesNotUnderstand:aMessage |
|
228 | 205 |
|
206 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
377 | 207 |
! |
208 |
||
2101 | 209 |
isSequenceable |
210 |
"catch this one - its so common" |
|
211 |
||
212 |
^ self doesNotUnderstand:(Message selector:#isSequenceable arguments:#()) |
|
213 |
! |
|
214 |
||
2100 | 215 |
size |
216 |
"catch this one - its so common" |
|
217 |
||
218 |
^ self doesNotUnderstand:(Message selector:#size arguments:#()) |
|
219 |
! |
|
220 |
||
1317 | 221 |
update:something with:aParameter from:changedObject |
222 |
"catch unhandled messages by looking in my simulated protocol |
|
223 |
definition; if there is some block for it, return its value. |
|
224 |
Otherwise, fall into the real update." |
|
225 |
||
226 |
|block| |
|
227 |
||
228 |
block := simulatedProtocol at:#'update:with:from:' ifAbsent:[]. |
|
229 |
block isNil ifTrue:[ |
|
230 |
^ super update:something with:aParameter from:changedObject |
|
231 |
]. |
|
232 |
^ block valueWithArguments:(Array with:something with:aParameter with:changedObject) |
|
233 |
||
234 |
! |
|
235 |
||
377 | 236 |
value |
237 |
"catch this one - its so common" |
|
238 |
||
1982
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
239 |
|block| |
377 | 240 |
|
1982
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
241 |
block := simulatedProtocol at:#value ifAbsent:[]. |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
242 |
block isNil ifTrue:[ |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
243 |
^ self |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
244 |
]. |
61c4a1986224
special case for #value message
Claus Gittinger <cg@exept.de>
parents:
1760
diff
changeset
|
245 |
^ block valueWithArguments:#() |
377 | 246 |
! |
247 |
||
248 |
value:arg |
|
249 |
"catch this one - its so common" |
|
250 |
||
251 |
^ self doesNotUnderstand:(Message selector:#value: argument:arg) |
|
252 |
||
253 |
"Created: 21.1.1997 / 15:21:36 / cg" |
|
54 | 254 |
! ! |
255 |
||
137 | 256 |
!Plug methodsFor:'protocol definition'! |
257 |
||
228 | 258 |
forgetAbout:aSelector |
259 |
"tell the receiver to forget about how to respond to the given by selector" |
|
260 |
||
261 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
262 |
||
263 |
" |
|
264 |
|p| |
|
265 |
||
266 |
p := Plug new. |
|
268 | 267 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
268 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 269 |
|
270 |
p foo. |
|
271 |
p foo:'hello'. |
|
272 |
||
273 |
p forgetAbout:#foo. |
|
274 |
||
275 |
p foo. |
|
276 |
" |
|
277 |
||
278 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
279 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
280 |
! |
|
281 |
||
2100 | 282 |
inheritFrom:aClass |
283 |
"very tricky - change the inheritance. |
|
284 |
This cannot be done by changing my class directly, because the instance layout |
|
285 |
must still be correct for Plugs instance variables. |
|
286 |
Therefore, the inheritance is remembered, and done dynamically in the doesNotUnderstand |
|
287 |
implementation." |
|
288 |
||
289 |
self assert:(aClass instSize == 0). |
|
290 |
inheritedClasses isNil ifTrue:[ |
|
291 |
inheritedClasses := Array with:aClass |
|
292 |
] ifFalse:[ |
|
293 |
inheritedClasses := inheritedClasses copyWith:aClass |
|
294 |
]. |
|
295 |
! |
|
296 |
||
137 | 297 |
respondTo:aSelector with:aBlock |
228 | 298 |
"tell the receiver to respond to a message given by selector, |
299 |
with evaluating aBlock. The number of arguments as defined by the |
|
300 |
selector must match the number of blockArsg expected by the block. |
|
301 |
The value returned from aBlock will be the value returned from the |
|
302 |
message." |
|
303 |
||
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
304 |
(self class includesSelector:aSelector) ifFalse:[ |
444 | 305 |
(self class superclass canUnderstand:aSelector) ifTrue:[ |
377 | 306 |
|
307 |
"/ sorry - this implementation is too quick of a hack. |
|
308 |
"/ (must be rewritten to inherit from nil, in order |
|
309 |
"/ to be able to catch more ...) |
|
310 |
||
311 |
self error:'inherited message cannot be redefined: ' , aSelector. |
|
312 |
] |
|
313 |
]. |
|
137 | 314 |
simulatedProtocol at:aSelector put:aBlock |
228 | 315 |
|
316 |
" |
|
317 |
|p| |
|
318 |
||
319 |
p := Plug new. |
|
268 | 320 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
321 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 322 |
|
323 |
p foo. |
|
324 |
p foo:'hello' |
|
325 |
" |
|
326 |
||
444 | 327 |
"Modified: 21.2.1997 / 18:35:07 / cg" |
137 | 328 |
! ! |
329 |
||
82 | 330 |
!Plug methodsFor:'queries'! |
331 |
||
332 |
respondsTo:aSelector |
|
228 | 333 |
"return true, if the receiver responds to a message" |
334 |
||
82 | 335 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
336 |
^ super respondsTo:aSelector |
|
228 | 337 |
|
338 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 339 |
! ! |
340 |
||
137 | 341 |
!Plug class methodsFor:'documentation'! |
54 | 342 |
|
137 | 343 |
version |
2101 | 344 |
^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.23 2006-02-14 11:08:47 cg Exp $' |
54 | 345 |
! ! |