author | Claus Gittinger <cg@exept.de> |
Fri, 04 Apr 2003 19:07:54 +0200 | |
changeset 1737 | a1ed08195ee7 |
parent 1554 | d01ffa42ca7a |
child 1760 | 48478a48da82 |
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 |
||
138 |
doesNotUnderstand:aMessage |
|
228 | 139 |
"catch unhandled messages by looking in my simulated protocol |
140 |
definition; if there is some block for it, return its value. |
|
141 |
Otherwise, fall into the real doesNotUnderstand error." |
|
142 |
||
54 | 143 |
|block| |
144 |
||
145 |
block := simulatedProtocol at:aMessage selector ifAbsent:[]. |
|
146 |
block isNil ifTrue:[ |
|
228 | 147 |
^ super doesNotUnderstand:aMessage |
54 | 148 |
]. |
149 |
^ block valueWithArguments:(aMessage arguments) |
|
228 | 150 |
|
151 |
"Modified: 27.4.1996 / 16:15:34 / cg" |
|
377 | 152 |
! |
153 |
||
1317 | 154 |
update:something with:aParameter from:changedObject |
155 |
"catch unhandled messages by looking in my simulated protocol |
|
156 |
definition; if there is some block for it, return its value. |
|
157 |
Otherwise, fall into the real update." |
|
158 |
||
159 |
|block| |
|
160 |
||
161 |
block := simulatedProtocol at:#'update:with:from:' ifAbsent:[]. |
|
162 |
block isNil ifTrue:[ |
|
163 |
^ super update:something with:aParameter from:changedObject |
|
164 |
]. |
|
165 |
^ block valueWithArguments:(Array with:something with:aParameter with:changedObject) |
|
166 |
||
167 |
! |
|
168 |
||
377 | 169 |
value |
170 |
"catch this one - its so common" |
|
171 |
||
172 |
^ self doesNotUnderstand:(Message selector:#value arguments:#()) |
|
173 |
||
174 |
"Created: 21.1.1997 / 15:21:24 / cg" |
|
175 |
! |
|
176 |
||
177 |
value:arg |
|
178 |
"catch this one - its so common" |
|
179 |
||
180 |
^ self doesNotUnderstand:(Message selector:#value: argument:arg) |
|
181 |
||
182 |
"Created: 21.1.1997 / 15:21:36 / cg" |
|
54 | 183 |
! ! |
184 |
||
137 | 185 |
!Plug methodsFor:'protocol definition'! |
186 |
||
228 | 187 |
forgetAbout:aSelector |
188 |
"tell the receiver to forget about how to respond to the given by selector" |
|
189 |
||
190 |
simulatedProtocol removeKey:aSelector ifAbsent:nil |
|
191 |
||
192 |
" |
|
193 |
|p| |
|
194 |
||
195 |
p := Plug new. |
|
268 | 196 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
197 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 198 |
|
199 |
p foo. |
|
200 |
p foo:'hello'. |
|
201 |
||
202 |
p forgetAbout:#foo. |
|
203 |
||
204 |
p foo. |
|
205 |
" |
|
206 |
||
207 |
"Modified: 27.4.1996 / 16:14:19 / cg" |
|
208 |
"Created: 27.4.1996 / 16:19:08 / cg" |
|
209 |
! |
|
210 |
||
137 | 211 |
respondTo:aSelector with:aBlock |
228 | 212 |
"tell the receiver to respond to a message given by selector, |
213 |
with evaluating aBlock. The number of arguments as defined by the |
|
214 |
selector must match the number of blockArsg expected by the block. |
|
215 |
The value returned from aBlock will be the value returned from the |
|
216 |
message." |
|
217 |
||
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
218 |
(self class includesSelector:aSelector) ifFalse:[ |
444 | 219 |
(self class superclass canUnderstand:aSelector) ifTrue:[ |
377 | 220 |
|
221 |
"/ sorry - this implementation is too quick of a hack. |
|
222 |
"/ (must be rewritten to inherit from nil, in order |
|
223 |
"/ to be able to catch more ...) |
|
224 |
||
225 |
self error:'inherited message cannot be redefined: ' , aSelector. |
|
226 |
] |
|
227 |
]. |
|
137 | 228 |
simulatedProtocol at:aSelector put:aBlock |
228 | 229 |
|
230 |
" |
|
231 |
|p| |
|
232 |
||
233 |
p := Plug new. |
|
268 | 234 |
p respondTo:#foo with:[Transcript showCR:'foo']. |
235 |
p respondTo:#foo: with:[:arg | Transcript show:'foo:'; showCR:arg]. |
|
228 | 236 |
|
237 |
p foo. |
|
238 |
p foo:'hello' |
|
239 |
" |
|
240 |
||
444 | 241 |
"Modified: 21.2.1997 / 18:35:07 / cg" |
137 | 242 |
! ! |
243 |
||
82 | 244 |
!Plug methodsFor:'queries'! |
245 |
||
246 |
respondsTo:aSelector |
|
228 | 247 |
"return true, if the receiver responds to a message" |
248 |
||
82 | 249 |
(simulatedProtocol includesKey:aSelector) ifTrue:[^ true]. |
250 |
^ super respondsTo:aSelector |
|
228 | 251 |
|
252 |
"Modified: 27.4.1996 / 16:14:41 / cg" |
|
82 | 253 |
! ! |
254 |
||
137 | 255 |
!Plug class methodsFor:'documentation'! |
54 | 256 |
|
137 | 257 |
version |
1554
d01ffa42ca7a
implements -> includesSelector
Claus Gittinger <cg@exept.de>
parents:
1317
diff
changeset
|
258 |
^ '$Header: /cvs/stx/stx/libview2/Plug.st,v 1.18 2002-02-25 19:59:46 cg Exp $' |
54 | 259 |
! ! |