author | Claus Gittinger <cg@exept.de> |
Fri, 07 Oct 2011 10:51:42 +0200 | |
changeset 13769 | 8259f3bf7fa2 |
parent 13184 | 3a98fb1116bf |
child 14290 | 1561011a34fd |
permissions | -rw-r--r-- |
96 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
344 | 3 |
All Rights Reserved |
96 | 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 |
" |
|
7781 | 12 |
"{ Package: 'stx:libbasic' }" |
5591 | 13 |
|
96 | 14 |
Message subclass:#MessageSend |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
15 |
instanceVariableNames:'receiver' |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
16 |
classVariableNames:'' |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
17 |
poolDictionaries:'' |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
18 |
category:'Kernel-Methods' |
96 | 19 |
! |
20 |
||
21 |
!MessageSend class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
344 | 26 |
All Rights Reserved |
96 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
37 |
documentation |
|
38 |
" |
|
39 |
Instances of MessageSend can be used for simulation programs. |
|
40 |
They keep some receiver, selector and arguments and can be evaluated |
|
1269 | 41 |
at any time later. (basically, they are like MessageObjects, but keep |
42 |
the receiver in addition to the selector & arguments). |
|
43 |
||
96 | 44 |
They can also be used as replacement for simple [self foo]-blocks. |
45 |
Of course, they could also be replaced by blocks such as |
|
46 |
'[receiver perform:selector withArguments:arguments]', |
|
344 | 47 |
but blocks are somewhat more expensive in their creation and require |
48 |
more storage. |
|
49 |
||
1269 | 50 |
If you plan to write a simulator and want to queue cillions of blocks, |
51 |
try to use MessageSends instead of blocks |
|
52 |
(or even: message, if the receiver is constant); |
|
53 |
this will save you a lot of memory. |
|
54 |
||
96 | 55 |
However, the send-operation itself is faster in a block, since it |
56 |
will use a better caching scheme (inline-cache) for its send, while |
|
57 |
sending here is done with a #perform:, which is not inline-cached. |
|
1269 | 58 |
Also, blocks are more flexible, in that they allow access to local |
59 |
variables of the defining method - and work without a need to define an |
|
60 |
extra visited method (read literature on visitor patterns). |
|
61 |
||
363 | 62 |
Thus it is not sure, which one is actually better to use ... |
63 |
||
64 |
You can either store arguments in the messageSend object, or |
|
65 |
leave them undefined until the send is actually performed, and |
|
66 |
pass any arguments with the value:-messages. |
|
1270 | 67 |
|
68 |
[See also:] |
|
69 |
Block Message |
|
1293 | 70 |
|
71 |
[author:] |
|
72 |
Claus Gittinger |
|
1269 | 73 |
" |
74 |
! |
|
96 | 75 |
|
1269 | 76 |
examples |
77 |
" |
|
96 | 78 |
Example: |
79 |
|m| |
|
80 |
||
81 |
m := MessageSend receiver:1 selector:#+ arguments:#(2). |
|
363 | 82 |
m value. |
96 | 83 |
|
1316 | 84 |
|
85 |
||
1269 | 86 |
is almost the same as: |
344 | 87 |
|m| |
88 |
||
89 |
m := [1+2]. |
|
363 | 90 |
m value. |
344 | 91 |
|
92 |
||
1269 | 93 |
|
96 | 94 |
Example2 (a simulation) |
1269 | 95 |
|q| |
96 | 96 |
|
1269 | 97 |
q := Queue new. |
98 |
... |
|
99 |
'put some action into the queue' |
|
100 |
q nextPut:(MessageSend receiver:someone selector:#foo arguments:#(). |
|
101 |
... |
|
102 |
'evaluate next action from the queue' |
|
103 |
q next value |
|
104 |
... |
|
344 | 105 |
|
106 |
if all sends are going to the same receiver, use: |
|
1269 | 107 |
|q| |
344 | 108 |
|
1269 | 109 |
q := Queue new. |
110 |
... |
|
111 |
'put some action into the queue' |
|
112 |
q nextPut:(Message selector:#foo arguments:#(). |
|
113 |
... |
|
114 |
'evaluate next action from the queue' |
|
115 |
q next sendTo:someone |
|
116 |
... |
|
96 | 117 |
" |
118 |
! ! |
|
119 |
||
120 |
!MessageSend class methodsFor:'instance creation'! |
|
121 |
||
2473 | 122 |
receiver:r selector:sel |
123 |
"create & return a new instance which can be used to |
|
124 |
send sel to some receiver, r" |
|
125 |
||
126 |
^ self receiver:r selector:sel arguments:nil |
|
127 |
||
128 |
" |
|
129 |
(MessageSend receiver:nil selector:#foo) value |
|
130 |
" |
|
131 |
||
132 |
"Modified: 20.3.1997 / 21:55:16 / cg" |
|
133 |
! |
|
134 |
||
5669 | 135 |
receiver:r selector:sel argument:something |
136 |
"create & return a new instance which can be used to |
|
137 |
send sel with arguments to some receiver, r" |
|
138 |
||
139 |
^ self receiver:r selector:sel arguments:(Array with:something) |
|
140 |
||
141 |
" |
|
142 |
(MessageSend receiver:nil selector:#foo: argument:1) value |
|
143 |
" |
|
144 |
! |
|
145 |
||
96 | 146 |
receiver:r selector:sel arguments:argArray |
1269 | 147 |
"create & return a new instance which can be used to |
148 |
send sel with arguments to some receiver, r" |
|
149 |
||
96 | 150 |
|newMessage| |
151 |
||
344 | 152 |
newMessage := super new setSelector:sel arguments:argArray. |
96 | 153 |
newMessage receiver:r. |
154 |
^ newMessage |
|
1269 | 155 |
|
2473 | 156 |
" |
157 |
(MessageSend receiver:nil selector:#foo: arguments:#(1)) value |
|
158 |
" |
|
159 |
||
160 |
"Modified: 20.3.1997 / 21:55:44 / cg" |
|
96 | 161 |
! ! |
162 |
||
13184 | 163 |
!MessageSend methodsFor:'Compatibility-V''Age'! |
164 |
||
165 |
evaluate |
|
166 |
^ self value |
|
167 |
||
168 |
"Created: / 22-12-2010 / 12:59:25 / cg" |
|
169 |
! |
|
170 |
||
171 |
evaluateWith:someArgument |
|
172 |
^ self value:someArgument |
|
173 |
||
174 |
"Created: / 22-12-2010 / 13:50:33 / cg" |
|
175 |
! |
|
176 |
||
177 |
evaluateWithArguments:argArray |
|
178 |
^ self valueWithArguments:argArray |
|
179 |
||
180 |
"Created: / 22-12-2010 / 12:59:42 / cg" |
|
181 |
! ! |
|
182 |
||
8688
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
183 |
!MessageSend methodsFor:'accessing'! |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
184 |
|
8688
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
185 |
argumentCount |
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
186 |
"VisualAge/ANSI compatibility: return the number of arguments of the message" |
1269 | 187 |
|
8688
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
188 |
^ selector argumentCount |
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
189 |
|
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
190 |
"Modified: 23.4.1996 / 16:52:51 / cg" |
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
191 |
! |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
192 |
|
8581 | 193 |
numArgs |
194 |
"return the number of arguments of the message" |
|
195 |
||
196 |
^ selector numArgs |
|
197 |
||
198 |
"Modified: 23.4.1996 / 16:52:51 / cg" |
|
199 |
! |
|
200 |
||
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
201 |
receiver |
1269 | 202 |
"return the receiver of the message" |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
203 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
204 |
^ receiver |
1269 | 205 |
|
206 |
"Modified: 23.4.1996 / 16:52:59 / cg" |
|
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
207 |
! |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
208 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
209 |
receiver:r |
1269 | 210 |
"set the receiver of the message" |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
211 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
212 |
receiver := r |
1269 | 213 |
|
214 |
"Modified: 23.4.1996 / 16:53:04 / cg" |
|
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
215 |
! ! |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
216 |
|
96 | 217 |
!MessageSend methodsFor:'evaluation'! |
218 |
||
219 |
value |
|
160 | 220 |
"evaluate the messagesend with the original arguments" |
96 | 221 |
|
222 |
^ receiver perform:selector withArguments:args |
|
223 |
! |
|
224 |
||
225 |
value:someArgument |
|
226 |
"evaluate the messagesend, with someArgument instead of the original" |
|
13058 | 227 |
|
228 |
^ receiver perform:selector withOptionalArgument:someArgument |
|
229 |
||
230 |
"Modified: / 14-09-2010 / 16:04:39 / cg" |
|
96 | 231 |
! |
232 |
||
233 |
value:arg1 value:arg2 |
|
160 | 234 |
"evaluate the messagesend, with arg1 and arg2 instead of the original |
235 |
arguments" |
|
96 | 236 |
|
13058 | 237 |
^ receiver perform:selector withOptionalArgument:arg1 and:arg2 |
238 |
||
239 |
"Modified: / 14-09-2010 / 16:04:58 / cg" |
|
96 | 240 |
! |
241 |
||
242 |
value:arg1 value:arg2 value:arg3 |
|
160 | 243 |
"evaluate the messagesend, with arg1, arg2 and arg3 instead of the original |
244 |
arguments" |
|
96 | 245 |
|
8580 | 246 |
^ receiver perform:selector with:arg1 with:arg2 with:arg3 |
363 | 247 |
! |
248 |
||
249 |
valueWithArguments:argArray |
|
250 |
"evaluate the messagesend, with arguments taken from argArray, |
|
251 |
instead of the original arguments" |
|
252 |
||
8580 | 253 |
^ receiver perform:selector withArguments:argArray |
11417 | 254 |
! |
255 |
||
256 |
valueWithOptionalArgument:arg |
|
257 |
"evaluate the messagesend. |
|
258 |
Optionally pass an argument (if the selector is for a one arg message)." |
|
259 |
||
260 |
^ receiver perform:selector withOptionalArgument:arg |
|
261 |
! |
|
262 |
||
263 |
valueWithOptionalArgument:arg1 and:arg2 |
|
264 |
"evaluate the messagesend. |
|
265 |
Optionally pass up to two arguments." |
|
266 |
||
267 |
^ receiver perform:selector withOptionalArgument:arg1 and:arg2 |
|
96 | 268 |
! ! |
269 |
||
270 |
!MessageSend methodsFor:'printing & storing'! |
|
271 |
||
272 |
displayString |
|
273 |
"return a string for display in inspectors etc." |
|
274 |
||
5591 | 275 |
^ self class name , '(' , receiver displayString , '>>' , selector , ')' |
96 | 276 |
! |
277 |
||
278 |
printOn:aStream |
|
4392 | 279 |
"append a user printed representation of the receiver to aStream. |
280 |
The format is suitable for a human - not meant to be read back." |
|
96 | 281 |
|
282 |
receiver printOn:aStream. |
|
283 |
aStream nextPutAll:'>>'. |
|
284 |
selector printOn:aStream |
|
1269 | 285 |
|
286 |
"Modified: 23.4.1996 / 16:53:35 / cg" |
|
96 | 287 |
! ! |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
288 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
289 |
!MessageSend class methodsFor:'documentation'! |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
290 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
291 |
version |
13184 | 292 |
^ '$Header: /cvs/stx/stx/libbasic/MessageSend.st,v 1.23 2010-12-22 12:50:41 cg Exp $' |
13058 | 293 |
! |
294 |
||
295 |
version_CVS |
|
13184 | 296 |
^ '$Header: /cvs/stx/stx/libbasic/MessageSend.st,v 1.23 2010-12-22 12:50:41 cg Exp $' |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
297 |
! ! |