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