author | martin |
Wed, 07 May 2003 16:09:30 +0200 | |
changeset 1760 | 48478a48da82 |
parent 1554 | d01ffa42ca7a |
child 1982 | 61c4a1986224 |
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 |
162 | 16 |
instanceVariableNames:'simulatedProtocol' |
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. |
228 | 42 |
A plugs interface can be changed dynamically. |
43 |
||
226 | 44 |
Its main use is for the demo doIts, to play the role of a model, |
228 | 45 |
when no actual modelClass is available for the demonstration. |
46 |
However, it can be used wherever some object is needed which responds to |
|
47 |
some protocol AND you do not want to add a class for it |
|
48 |
(lightWeight objects). |
|
49 |
||
50 |
There is a slight performance penalty - compared to `normal' objects, |
|
51 |
getting `normal' messages, though. |
|
224 | 52 |
|
53 |
[author:] |
|
54 |
Claus Gittinger |
|
228 | 55 |
|
56 |
[see also:] |
|
57 |
Model |
|
54 | 58 |
" |
59 |
! |
|
60 |
||
61 |
examples |
|
62 |
" |
|
228 | 63 |
[exBegin] |
54 | 64 |
|plug| |
65 |
||
66 |
plug := Plug new. |
|
268 | 67 |
plug respondTo:#foo with:[Transcript showCR:'received foo']. |
68 |
plug respondTo:#foo: with:[:arg | Transcript showCR:'received foo: ', arg printString]. |
|
54 | 69 |
|
70 |
plug foo. |
|
71 |
plug foo:'some argument' |
|
228 | 72 |
[exEnd] |
162 | 73 |
|
360 | 74 |
using a plug as a generator (simulates a readStream): |
75 |
[exBegin] |
|
76 |
|generator num| |
|
77 |
||
78 |
num := 0. |
|
79 |
generator := Plug new. |
|
80 |
generator respondTo:#next |
|
81 |
with:[num := num + 1. num]. |
|
82 |
generator respondTo:#atEnd |
|
83 |
with:[false]. |
|
84 |
||
85 |
10 timesRepeat:[ |
|
86 |
Transcript showCR:(generator next) |
|
87 |
] |
|
88 |
[exEnd] |
|
162 | 89 |
|
90 |
simulating ``instance variables'': |
|
91 |
(actually, this is somewhat expensive - the contexts locals are used for them ...) |
|
92 |
be careful with unintended variable sharing (if plugs are created in a loop ..) |
|
228 | 93 |
[exBegin] |
162 | 94 |
|plug1 plug2 local1 local2| |
95 |
||
96 |
plug1 := Plug new. |
|
97 |
plug1 respondTo:#get with:[local1]. |
|
98 |
plug1 respondTo:#set: with:[:arg | local1 := arg]. |
|
99 |
||
100 |
plug2 := Plug new. |
|
101 |
plug2 respondTo:#get with:[local2]. |
|
102 |
plug2 respondTo:#set: with:[:arg | local2 := arg]. |
|
103 |
||
268 | 104 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
105 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
162 | 106 |
|
107 |
plug1 set:5. |
|
108 |
plug2 set:17. |
|
109 |
||
268 | 110 |
Transcript show:'plug1''s value: '; showCR:plug1 get. |
111 |
Transcript show:'plug2''s value: '; showCR:plug2 get. |
|
228 | 112 |
[exEnd] |
54 | 113 |
" |
114 |
! ! |
|
115 |
||
116 |
!Plug class methodsFor:'instance creation'! |
|
117 |
||
118 |
new |
|
232 | 119 |
^ super basicNew privateInitialize |
228 | 120 |
|
121 |
"Modified: 27.4.1996 / 16:16:59 / cg" |
|
54 | 122 |
! ! |
123 |
||
137 | 124 |
!Plug methodsFor:'initialization'! |
125 |
||
232 | 126 |
privateInitialize |
228 | 127 |
"this method is NOT called `#initialize' to allow plugging that |
137 | 128 |
selector ..." |
129 |
||
130 |
simulatedProtocol := IdentityDictionary new. |
|
228 | 131 |
|
132 |
"Modified: 27.4.1996 / 16:15:45 / cg" |
|
133 |
"Created: 27.4.1996 / 16:17:07 / cg" |
|
137 | 134 |
! ! |
135 |
||
54 | 136 |
!Plug methodsFor:'message sending'! |
137 |
||
1760 | 138 |
at:key |
139 |
"catch this one - its so common" |
|
140 |
||
141 |
^ self doesNotUnderstand:(Message selector:#at: arguments:(Array with:key)) |
|
142 |
! |
|
143 |
||
144 |
at:key put:value |
|
145 |
^ self doesNotUnderstand:(Message selector:#at:put: arguments:(Array with:key with:value)) |
|
146 |
! |
|
147 |
||
54 | 148 |
doesNotUnderstand:aMessage |
228 | 149 |
"catch unhandled messages by looking in my simulated protocol |
150 |
definition; if there is some block for it, return its value. |
|
151 |
Otherwise, fall into the real doesNotUnderstand error." |
|
152 |
||
54 | 153 |
|block| |
154 |
||
155 |
block := simulatedProtocol at:aMessage selector ifAbsent:[]. |
|
156 |
block isNil ifTrue:[ |
|
228 | 157 |
^ super doesNotUnderstand:aMessage |
54 | 158 |
]. |
159 |
^ block valueWithArguments:(aMessage arguments) |
|
228 | 160 |
|
161 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
377 | 162 |
! |
163 |
||
1317 | 164 |
update:something with:aParameter from:changedObject |
165 |
"catch unhandled messages by looking in my simulated protocol |
|
166 |
definition; if there is some block for it, return its value. |
|
167 |
Otherwise, fall into the real update." |
|
168 |
||
169 |
|block| |
|
170 |
||
171 |
block := simulatedProtocol at:#'update:with:from:' ifAbsent:[]. |
|
172 |
block isNil ifTrue:[ |
|
173 |
^ super update:something with:aParameter from:changedObject |
|
174 |
]. |
|
175 |
^ block valueWithArguments:(Array with:something with:aParameter with:changedObject) |
|
176 |
||
177 |
! |
|
178 |
||
377 | 179 |
value |
180 |
"catch this one - its so common" |
|
181 |
||
182 |
^ self doesNotUnderstand:(Message selector:#value arguments:#()) |
|
183 |
||
184 |
"Created: 21.1.1997 / 15:21:24 / cg" |
|
185 |
! |
|
186 |
||
187 |
value:arg |
|
188 |
"catch this one - its so common" |
|
189 |
||
190 |
^ self doesNotUnderstand:(Message selector:#value: argument:arg) |
|
191 |
||
192 |
"Created: 21.1.1997 / 15:21:36 / cg" |
|
54 | 193 |
! ! |
194 |
||
137 | 195 |
!Plug methodsFor:'protocol definition'! |
196 |
||
228 | 197 |
forgetAbout:aSelector |
198 |
"tell the receiver to forget about how to respond to the given by selector" |
|
199 |
||
200 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
201 |
||
202 |
" |
|
203 |
|p| |
|
204 |
||
205 |
p := Plug new. |
|
268 | 206 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
207 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 208 |
|
209 |
p foo. |
|
210 |
p foo:'hello'. |
|
211 |
||
212 |
p forgetAbout:#foo. |
|
213 |
||
214 |
p foo. |
|
215 |
" |
|
216 |
||
217 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
218 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
219 |
! |
|
220 |
||
137 | 221 |
respondTo:aSelector with:aBlock |
228 | 222 |
"tell the receiver to respond to a message given by selector, |
223 |
with evaluating aBlock. The number of arguments as defined by the |
|
224 |
selector must match the number of blockArsg expected by the block. |
|
225 |
The value returned from aBlock will be the value returned from the |
|
226 |
message." |
|
227 |
||
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
228 |
(self class includesSelector:aSelector) ifFalse:[ |
444 | 229 |
(self class superclass canUnderstand:aSelector) ifTrue:[ |
377 | 230 |
|
231 |
"/ sorry - this implementation is too quick of a hack. |
|
232 |
"/ (must be rewritten to inherit from nil, in order |
|
233 |
"/ to be able to catch more ...) |
|
234 |
||
235 |
self error:'inherited message cannot be redefined: ' , aSelector. |
|
236 |
] |
|
237 |
]. |
|
137 | 238 |
simulatedProtocol at:aSelector put:aBlock |
228 | 239 |
|
240 |
" |
|
241 |
|p| |
|
242 |
||
243 |
p := Plug new. |
|
268 | 244 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
245 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 246 |
|
247 |
p foo. |
|
248 |
p foo:'hello' |
|
249 |
" |
|
250 |
||
444 | 251 |
"Modified: 21.2.1997 / 18:35:07 / cg" |
137 | 252 |
! ! |
253 |
||
82 | 254 |
!Plug methodsFor:'queries'! |
255 |
||
256 |
respondsTo:aSelector |
|
228 | 257 |
"return true, if the receiver responds to a message" |
258 |
||
82 | 259 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
260 |
^ super respondsTo:aSelector |
|
228 | 261 |
|
262 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 263 |
! ! |
264 |
||
137 | 265 |
!Plug class methodsFor:'documentation'! |
54 | 266 |
|
137 | 267 |
version |
1760 | 268 |
^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.19 2003-05-07 14:09:30 martin Exp $' |
54 | 269 |
! ! |