author | Jan Vrany <jan.vrany@fit.cvut.cz> |
Mon, 15 Jun 2009 20:55:05 +0100 | |
branch | jv |
changeset 17711 | 39faaaf888b4 |
parent 11417 | af0c3065a5c1 |
child 17761 | b0e5971141bc |
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 |
||
8688
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
163 |
!MessageSend methodsFor:'accessing'! |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
164 |
|
8688
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
165 |
argumentCount |
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
166 |
"VisualAge/ANSI compatibility: return the number of arguments of the message" |
1269 | 167 |
|
8688
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
168 |
^ selector argumentCount |
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
169 |
|
c3de1df6642a
Define #argumentCount as ANSI alias for #numArgs
Stefan Vogel <sv@exept.de>
parents:
8581
diff
changeset
|
170 |
"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
|
171 |
! |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
172 |
|
8581 | 173 |
numArgs |
174 |
"return the number of arguments of the message" |
|
175 |
||
176 |
^ selector numArgs |
|
177 |
||
178 |
"Modified: 23.4.1996 / 16:52:51 / cg" |
|
179 |
! |
|
180 |
||
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
181 |
receiver |
1269 | 182 |
"return the receiver of the message" |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
183 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
184 |
^ receiver |
1269 | 185 |
|
186 |
"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
|
187 |
! |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
188 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
189 |
receiver:r |
1269 | 190 |
"set the receiver of the message" |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
191 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
192 |
receiver := r |
1269 | 193 |
|
194 |
"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
|
195 |
! ! |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
196 |
|
96 | 197 |
!MessageSend methodsFor:'evaluation'! |
198 |
||
199 |
value |
|
160 | 200 |
"evaluate the messagesend with the original arguments" |
96 | 201 |
|
202 |
^ receiver perform:selector withArguments:args |
|
203 |
! |
|
204 |
||
205 |
value:someArgument |
|
206 |
"evaluate the messagesend, with someArgument instead of the original" |
|
207 |
||
208 |
^ receiver perform:selector with:someArgument |
|
209 |
! |
|
210 |
||
211 |
value:arg1 value:arg2 |
|
160 | 212 |
"evaluate the messagesend, with arg1 and arg2 instead of the original |
213 |
arguments" |
|
96 | 214 |
|
8580 | 215 |
^ receiver perform:selector with:arg1 with:arg2 |
96 | 216 |
! |
217 |
||
218 |
value:arg1 value:arg2 value:arg3 |
|
160 | 219 |
"evaluate the messagesend, with arg1, arg2 and arg3 instead of the original |
220 |
arguments" |
|
96 | 221 |
|
8580 | 222 |
^ receiver perform:selector with:arg1 with:arg2 with:arg3 |
363 | 223 |
! |
224 |
||
225 |
valueWithArguments:argArray |
|
226 |
"evaluate the messagesend, with arguments taken from argArray, |
|
227 |
instead of the original arguments" |
|
228 |
||
8580 | 229 |
^ receiver perform:selector withArguments:argArray |
11417 | 230 |
! |
231 |
||
232 |
valueWithOptionalArgument:arg |
|
233 |
"evaluate the messagesend. |
|
234 |
Optionally pass an argument (if the selector is for a one arg message)." |
|
235 |
||
236 |
^ receiver perform:selector withOptionalArgument:arg |
|
237 |
! |
|
238 |
||
239 |
valueWithOptionalArgument:arg1 and:arg2 |
|
240 |
"evaluate the messagesend. |
|
241 |
Optionally pass up to two arguments." |
|
242 |
||
243 |
^ receiver perform:selector withOptionalArgument:arg1 and:arg2 |
|
96 | 244 |
! ! |
245 |
||
246 |
!MessageSend methodsFor:'printing & storing'! |
|
247 |
||
248 |
displayString |
|
249 |
"return a string for display in inspectors etc." |
|
250 |
||
5591 | 251 |
^ self class name , '(' , receiver displayString , '>>' , selector , ')' |
96 | 252 |
! |
253 |
||
254 |
printOn:aStream |
|
4392 | 255 |
"append a user printed representation of the receiver to aStream. |
256 |
The format is suitable for a human - not meant to be read back." |
|
96 | 257 |
|
258 |
receiver printOn:aStream. |
|
259 |
aStream nextPutAll:'>>'. |
|
260 |
selector printOn:aStream |
|
1269 | 261 |
|
262 |
"Modified: 23.4.1996 / 16:53:35 / cg" |
|
96 | 263 |
! ! |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
264 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
265 |
!MessageSend class methodsFor:'documentation'! |
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
266 |
|
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
267 |
version |
17711 | 268 |
^ '$Id: MessageSend.st 10447 2009-06-14 13:09:55Z vranyj1 $' |
1183
e3d58d115e53
subclasses of fixed classes are still possible
Claus Gittinger <cg@exept.de>
parents:
530
diff
changeset
|
269 |
! ! |