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
|
344
|
14 |
instanceVariableNames:'receiver'
|
|
15 |
classVariableNames:''
|
|
16 |
poolDictionaries:''
|
|
17 |
category:'Kernel-Methods'
|
96
|
18 |
!
|
|
19 |
|
|
20 |
MessageSend comment:'
|
|
21 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
344
|
22 |
All Rights Reserved
|
96
|
23 |
|
363
|
24 |
$Header: /cvs/stx/stx/libbasic/MessageSend.st,v 1.4 1995-07-22 21:48:28 claus Exp $
|
96
|
25 |
'!
|
|
26 |
|
|
27 |
!MessageSend class methodsFor:'documentation'!
|
|
28 |
|
|
29 |
copyright
|
|
30 |
"
|
|
31 |
COPYRIGHT (c) 1994 by Claus Gittinger
|
344
|
32 |
All Rights Reserved
|
96
|
33 |
|
|
34 |
This software is furnished under a license and may be used
|
|
35 |
only in accordance with the terms of that license and with the
|
|
36 |
inclusion of the above copyright notice. This software may not
|
|
37 |
be provided or otherwise made available to, or used by, any
|
|
38 |
other person. No title to or ownership of the software is
|
|
39 |
hereby transferred.
|
|
40 |
"
|
|
41 |
!
|
|
42 |
|
|
43 |
version
|
|
44 |
"
|
363
|
45 |
$Header: /cvs/stx/stx/libbasic/MessageSend.st,v 1.4 1995-07-22 21:48:28 claus Exp $
|
96
|
46 |
"
|
|
47 |
!
|
|
48 |
|
|
49 |
documentation
|
|
50 |
"
|
|
51 |
Instances of MessageSend can be used for simulation programs.
|
|
52 |
They keep some receiver, selector and arguments and can be evaluated
|
|
53 |
at any time later.
|
|
54 |
They can also be used as replacement for simple [self foo]-blocks.
|
|
55 |
Of course, they could also be replaced by blocks such as
|
|
56 |
'[receiver perform:selector withArguments:arguments]',
|
344
|
57 |
but blocks are somewhat more expensive in their creation and require
|
|
58 |
more storage.
|
|
59 |
|
96
|
60 |
However, the send-operation itself is faster in a block, since it
|
|
61 |
will use a better caching scheme (inline-cache) for its send, while
|
|
62 |
sending here is done with a #perform:, which is not inline-cached.
|
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.
|
96
|
68 |
|
344
|
69 |
If you plan to write a simulator and want to queue cillions of blocks,
|
363
|
70 |
try to use MessageSends instead of blocks
|
|
71 |
(or even: message, if the receiver is constant);
|
|
72 |
this will save you a lot of memory.
|
344
|
73 |
|
96
|
74 |
Example:
|
|
75 |
|
|
76 |
|m|
|
|
77 |
|
|
78 |
m := MessageSend receiver:1 selector:#+ arguments:#(2).
|
363
|
79 |
m value.
|
96
|
80 |
|
344
|
81 |
is almost the same as:
|
|
82 |
|
|
83 |
|m|
|
|
84 |
|
|
85 |
m := [1+2].
|
363
|
86 |
m value.
|
344
|
87 |
|
|
88 |
|
96
|
89 |
Example2 (a simulation)
|
|
90 |
|q|
|
|
91 |
|
|
92 |
q := Queue new.
|
|
93 |
...
|
|
94 |
'put some action into the queue'
|
344
|
95 |
q nextPut:(MessageSend receiver:someone selector:#foo arguments:#().
|
96
|
96 |
...
|
|
97 |
'evaluate next action from the queue'
|
|
98 |
q next value
|
|
99 |
...
|
344
|
100 |
|
|
101 |
if all sends are going to the same receiver, use:
|
|
102 |
|q|
|
|
103 |
|
|
104 |
q := Queue new.
|
|
105 |
...
|
|
106 |
'put some action into the queue'
|
|
107 |
q nextPut:(Message selector:#foo arguments:#().
|
|
108 |
...
|
|
109 |
'evaluate next action from the queue'
|
|
110 |
q next sendTo:someone
|
|
111 |
...
|
96
|
112 |
"
|
|
113 |
! !
|
|
114 |
|
|
115 |
!MessageSend class methodsFor:'instance creation'!
|
|
116 |
|
|
117 |
receiver:r selector:sel arguments:argArray
|
|
118 |
|newMessage|
|
|
119 |
|
344
|
120 |
newMessage := super new setSelector:sel arguments:argArray.
|
96
|
121 |
newMessage receiver:r.
|
|
122 |
^ newMessage
|
|
123 |
! !
|
|
124 |
|
|
125 |
!MessageSend methodsFor:'evaluation'!
|
|
126 |
|
|
127 |
value
|
160
|
128 |
"evaluate the messagesend with the original arguments"
|
96
|
129 |
|
|
130 |
^ receiver perform:selector withArguments:args
|
|
131 |
!
|
|
132 |
|
|
133 |
value:someArgument
|
|
134 |
"evaluate the messagesend, with someArgument instead of the original"
|
|
135 |
|
|
136 |
^ receiver perform:selector with:someArgument
|
|
137 |
!
|
|
138 |
|
|
139 |
value:arg1 value:arg2
|
160
|
140 |
"evaluate the messagesend, with arg1 and arg2 instead of the original
|
|
141 |
arguments"
|
96
|
142 |
|
|
143 |
^ receiver perform:selector with:arg1 with:arg2
|
|
144 |
!
|
|
145 |
|
|
146 |
value:arg1 value:arg2 value:arg3
|
160
|
147 |
"evaluate the messagesend, with arg1, arg2 and arg3 instead of the original
|
|
148 |
arguments"
|
96
|
149 |
|
|
150 |
^ receiver perform:selector with:arg1 with:arg2 with:arg3
|
363
|
151 |
!
|
|
152 |
|
|
153 |
valueWithArguments:argArray
|
|
154 |
"evaluate the messagesend, with arguments taken from argArray,
|
|
155 |
instead of the original arguments"
|
|
156 |
|
|
157 |
^ receiver perform:selector withArguments:argArray
|
96
|
158 |
! !
|
|
159 |
|
|
160 |
!MessageSend methodsFor:'accessing'!
|
|
161 |
|
|
162 |
receiver:r
|
|
163 |
"set the receiver"
|
|
164 |
|
|
165 |
receiver := r
|
|
166 |
!
|
|
167 |
|
|
168 |
receiver
|
|
169 |
"return the receiver"
|
|
170 |
|
|
171 |
^ receiver
|
363
|
172 |
!
|
|
173 |
|
|
174 |
numArgs
|
|
175 |
^ selector numArgs
|
96
|
176 |
! !
|
|
177 |
|
|
178 |
!MessageSend methodsFor:'printing & storing'!
|
|
179 |
|
|
180 |
displayString
|
|
181 |
"return a string for display in inspectors etc."
|
|
182 |
|
|
183 |
^ 'MessageSend(' , receiver displayString , '>>' , selector , ')'
|
|
184 |
!
|
|
185 |
|
|
186 |
printOn:aStream
|
|
187 |
"return a string for printing the receiver"
|
|
188 |
|
|
189 |
receiver printOn:aStream.
|
|
190 |
aStream nextPutAll:'>>'.
|
|
191 |
selector printOn:aStream
|
|
192 |
! !
|