author | Claus Gittinger <cg@exept.de> |
Sat, 11 Nov 1995 16:28:41 +0100 | |
changeset 530 | 07d0bce293c9 |
parent 383 | 8e3ec8164440 |
child 1183 | e3d58d115e53 |
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 |
|
344 | 14 |
instanceVariableNames:'receiver' |
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
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 |
version |
|
530
07d0bce293c9
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
383
diff
changeset
|
37 |
^ '$Header: /cvs/stx/stx/libbasic/MessageSend.st,v 1.7 1995-11-11 15:21:25 cg Exp $' |
96 | 38 |
! |
39 |
||
40 |
documentation |
|
41 |
" |
|
42 |
Instances of MessageSend can be used for simulation programs. |
|
43 |
They keep some receiver, selector and arguments and can be evaluated |
|
44 |
at any time later. |
|
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 |
||
96 | 51 |
However, the send-operation itself is faster in a block, since it |
52 |
will use a better caching scheme (inline-cache) for its send, while |
|
53 |
sending here is done with a #perform:, which is not inline-cached. |
|
363 | 54 |
Thus it is not sure, which one is actually better to use ... |
55 |
||
56 |
You can either store arguments in the messageSend object, or |
|
57 |
leave them undefined until the send is actually performed, and |
|
58 |
pass any arguments with the value:-messages. |
|
96 | 59 |
|
344 | 60 |
If you plan to write a simulator and want to queue cillions of blocks, |
363 | 61 |
try to use MessageSends instead of blocks |
62 |
(or even: message, if the receiver is constant); |
|
63 |
this will save you a lot of memory. |
|
344 | 64 |
|
96 | 65 |
Example: |
66 |
||
67 |
|m| |
|
68 |
||
69 |
m := MessageSend receiver:1 selector:#+ arguments:#(2). |
|
363 | 70 |
m value. |
96 | 71 |
|
344 | 72 |
is almost the same as: |
73 |
||
74 |
|m| |
|
75 |
||
76 |
m := [1+2]. |
|
363 | 77 |
m value. |
344 | 78 |
|
79 |
||
96 | 80 |
Example2 (a simulation) |
81 |
|q| |
|
82 |
||
83 |
q := Queue new. |
|
84 |
... |
|
85 |
'put some action into the queue' |
|
344 | 86 |
q nextPut:(MessageSend receiver:someone selector:#foo arguments:#(). |
96 | 87 |
... |
88 |
'evaluate next action from the queue' |
|
89 |
q next value |
|
90 |
... |
|
344 | 91 |
|
92 |
if all sends are going to the same receiver, use: |
|
93 |
|q| |
|
94 |
||
95 |
q := Queue new. |
|
96 |
... |
|
97 |
'put some action into the queue' |
|
98 |
q nextPut:(Message selector:#foo arguments:#(). |
|
99 |
... |
|
100 |
'evaluate next action from the queue' |
|
101 |
q next sendTo:someone |
|
102 |
... |
|
96 | 103 |
" |
104 |
! ! |
|
105 |
||
106 |
!MessageSend class methodsFor:'instance creation'! |
|
107 |
||
108 |
receiver:r selector:sel arguments:argArray |
|
109 |
|newMessage| |
|
110 |
||
344 | 111 |
newMessage := super new setSelector:sel arguments:argArray. |
96 | 112 |
newMessage receiver:r. |
113 |
^ newMessage |
|
114 |
! ! |
|
115 |
||
116 |
!MessageSend methodsFor:'evaluation'! |
|
117 |
||
118 |
value |
|
160 | 119 |
"evaluate the messagesend with the original arguments" |
96 | 120 |
|
121 |
^ receiver perform:selector withArguments:args |
|
122 |
! |
|
123 |
||
124 |
value:someArgument |
|
125 |
"evaluate the messagesend, with someArgument instead of the original" |
|
126 |
||
127 |
^ receiver perform:selector with:someArgument |
|
128 |
! |
|
129 |
||
130 |
value:arg1 value:arg2 |
|
160 | 131 |
"evaluate the messagesend, with arg1 and arg2 instead of the original |
132 |
arguments" |
|
96 | 133 |
|
134 |
^ receiver perform:selector with:arg1 with:arg2 |
|
135 |
! |
|
136 |
||
137 |
value:arg1 value:arg2 value:arg3 |
|
160 | 138 |
"evaluate the messagesend, with arg1, arg2 and arg3 instead of the original |
139 |
arguments" |
|
96 | 140 |
|
141 |
^ receiver perform:selector with:arg1 with:arg2 with:arg3 |
|
363 | 142 |
! |
143 |
||
144 |
valueWithArguments:argArray |
|
145 |
"evaluate the messagesend, with arguments taken from argArray, |
|
146 |
instead of the original arguments" |
|
147 |
||
148 |
^ receiver perform:selector withArguments:argArray |
|
96 | 149 |
! ! |
150 |
||
151 |
!MessageSend methodsFor:'accessing'! |
|
152 |
||
153 |
receiver:r |
|
154 |
"set the receiver" |
|
155 |
||
156 |
receiver := r |
|
157 |
! |
|
158 |
||
159 |
receiver |
|
160 |
"return the receiver" |
|
161 |
||
162 |
^ receiver |
|
363 | 163 |
! |
164 |
||
165 |
numArgs |
|
166 |
^ selector numArgs |
|
96 | 167 |
! ! |
168 |
||
169 |
!MessageSend methodsFor:'printing & storing'! |
|
170 |
||
171 |
displayString |
|
172 |
"return a string for display in inspectors etc." |
|
173 |
||
174 |
^ 'MessageSend(' , receiver displayString , '>>' , selector , ')' |
|
175 |
! |
|
176 |
||
177 |
printOn:aStream |
|
178 |
"return a string for printing the receiver" |
|
179 |
||
180 |
receiver printOn:aStream. |
|
181 |
aStream nextPutAll:'>>'. |
|
182 |
selector printOn:aStream |
|
183 |
! ! |