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