author | Claus Gittinger <cg@exept.de> |
Mon, 26 Aug 2019 10:24:45 +0200 | |
changeset 5153 | 9bf4a6f51b33 |
parent 5151 | bfed222dcaad |
child 5157 | ad4d06ecfb6c |
permissions | -rw-r--r-- |
3531 | 1 |
"{ Encoding: utf8 }" |
2 |
||
166 | 3 |
" |
4 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
1624 | 5 |
All Rights Reserved |
166 | 6 |
|
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
936 | 14 |
"{ Package: 'stx:libbasic2' }" |
15 |
||
3469 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1621 | 18 |
PeekableStream subclass:#ActorStream |
3089 | 19 |
instanceVariableNames:'nextPutBlock nextPutAllBlock nextPutLineBlock nextBlock |
4968 | 20 |
atEndBlock peekBlock contentsSpecies bufferedLine' |
3089 | 21 |
classVariableNames:'' |
22 |
poolDictionaries:'' |
|
23 |
category:'Streams' |
|
166 | 24 |
! |
25 |
||
26 |
!ActorStream class methodsFor:'documentation'! |
|
27 |
||
28 |
copyright |
|
29 |
" |
|
30 |
COPYRIGHT (c) 1989 by Claus Gittinger |
|
1624 | 31 |
All Rights Reserved |
166 | 32 |
|
33 |
This software is furnished under a license and may be used |
|
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
39 |
" |
|
40 |
! |
|
41 |
||
42 |
documentation |
|
43 |
" |
|
44 |
This class provides a hook for general objects to behave like Stream objects. |
|
45 |
To the outside, actorStreams behave like streams and respond to the |
|
46 |
usual stream messages (i.e. nextPut:, nextPutAll: etc). |
|
47 |
In the inside, for every nextPut-message, the nextPutBlock is evaluated |
|
48 |
passing the argument of nextPut: as block argument. |
|
49 |
||
50 |
These blocks are to be provided by the user of the ActorStream. |
|
51 |
Example use is in the Transcript, which is made Stream-like this way. |
|
52 |
||
252 | 53 |
[Instance variables:] |
54 |
nextPutBlock <Block> the block to evaluate for nextPut:-messages |
|
55 |
nextPutAllBlock <Block> same for nextPutAll:-messages |
|
56 |
nextBlock <Block> the Block to evaluate for the next element |
|
57 |
||
259 | 58 |
[author:] |
59 |
Claus Gittinger |
|
60 |
||
252 | 61 |
[See also:] |
62 |
TextCollector |
|
63 |
||
166 | 64 |
" |
65 |
! ! |
|
66 |
||
67 |
!ActorStream class methodsFor:'instance creation'! |
|
68 |
||
69 |
new |
|
70 |
"have to re-allow new - it was disabled in Stream" |
|
71 |
^ self basicNew |
|
72 |
! ! |
|
73 |
||
1624 | 74 |
!ActorStream methodsFor:'accessing'! |
75 |
||
76 |
contentsSpecies |
|
3089 | 77 |
"return a class of which instances will be returned, when |
78 |
parts of the collection are asked for. |
|
79 |
(see upTo-kind of methods in Stream)" |
|
80 |
||
1624 | 81 |
^ contentsSpecies ? String |
82 |
! |
|
83 |
||
84 |
contentsSpecies:something |
|
85 |
contentsSpecies := something. |
|
86 |
! ! |
|
87 |
||
166 | 88 |
!ActorStream methodsFor:'accessing-read/write'! |
89 |
||
90 |
next |
|
91 |
"return the next element from the stream by evaluating the nextBlock" |
|
92 |
||
93 |
nextBlock notNil ifTrue:[ |
|
1624 | 94 |
^ nextBlock value |
166 | 95 |
]. |
96 |
self error:'action for next is undefined' |
|
97 |
! |
|
98 |
||
4968 | 99 |
nextPut:aCharacter |
166 | 100 |
"put something onto the stream by evaluating the nextPutBlock with |
101 |
something as argument" |
|
102 |
||
987 | 103 |
nextPutBlock notNil ifTrue:[ |
5151 | 104 |
nextPutBlock value:aCharacter. |
105 |
^ self. |
|
166 | 106 |
]. |
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
107 |
nextPutAllBlock notNil ifTrue:[ |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
108 |
"/ fallBack to nextPutAll. |
4968 | 109 |
self nextPutAll:(aCharacter asString). |
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
110 |
^ self |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
111 |
]. |
4968 | 112 |
"/ if there is a nextPutLineBlock, then collect the line in a buffer and wait for the cr |
113 |
nextPutLineBlock notNil ifTrue:[ |
|
114 |
aCharacter == Character cr ifFalse:[ |
|
115 |
bufferedLine := (bufferedLine ? '') , aCharacter. |
|
116 |
] ifTrue:[ |
|
117 |
self nextPutLine:(bufferedLine ? ''). |
|
118 |
bufferedLine := ''. |
|
119 |
]. |
|
120 |
^ self |
|
121 |
]. |
|
122 |
||
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
123 |
self error:'action for nextPut:/nextPutAll: are undefined' |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
124 |
|
4968 | 125 |
"Modified: / 28-01-2002 / 20:59:32 / micha" |
126 |
"Modified: / 29-05-2019 / 23:28:34 / Claus Gittinger" |
|
166 | 127 |
! |
128 |
||
129 |
nextPutAll:something |
|
130 |
"put all elements of something onto the stream by evaluating |
|
987 | 131 |
the nextPutAllBlock with something as argument. |
132 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited)" |
|
166 | 133 |
|
987 | 134 |
nextPutAllBlock notNil ifTrue:[ |
5151 | 135 |
nextPutAllBlock value:something. |
136 |
] ifFalse:[ |
|
137 |
super nextPutAll:something |
|
166 | 138 |
]. |
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
139 |
! |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
140 |
|
3531 | 141 |
nextPutAll:count from:buffer startingAt:start |
142 |
"put some elements of something onto the stream by evaluating |
|
143 |
the nextPutAllBlock with something as argument. |
|
5151 | 144 |
If there is no nextPutAllBlock, individual nextPuts will be used (as inherited). |
145 |
Answer the number of elements that were appended." |
|
3531 | 146 |
|
147 |
nextPutAllBlock notNil ifTrue:[ |
|
148 |
nextPutAllBlock value:(buffer copyFrom:start to:start+count-1). |
|
149 |
^ count |
|
150 |
]. |
|
151 |
^ super nextPutAll:count from:buffer startingAt:start |
|
152 |
! |
|
153 |
||
3469 | 154 |
nextPutAll:something startingAt:start to:stop |
155 |
"put some elements of something onto the stream by evaluating |
|
156 |
the nextPutAllBlock with something as argument. |
|
157 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited)" |
|
158 |
||
159 |
nextPutAllBlock notNil ifTrue:[ |
|
4790 | 160 |
nextPutAllBlock value:(something copyFrom:start to:stop). |
161 |
^ self. |
|
3469 | 162 |
]. |
163 |
super nextPutAll:something startingAt:start to:stop |
|
4768 | 164 |
|
165 |
"Modified: / 22-11-2018 / 12:52:51 / Stefan Vogel" |
|
4790 | 166 |
"Modified: / 14-01-2019 / 18:23:40 / Claus Gittinger" |
3469 | 167 |
! |
168 |
||
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
169 |
nextPutByte:something |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
170 |
"put something onto the stream by evaluating the nextPutBlock with |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
171 |
something as argument" |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
172 |
|
5153 | 173 |
self nextPut:(something asCharacter). |
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
174 |
|
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
175 |
"Created: 6.8.1997 / 00:44:32 / cg" |
987 | 176 |
! |
177 |
||
178 |
nextPutLine:something |
|
179 |
"put the line onto the stream by evaluating |
|
180 |
the nextPutLineBlock with something as argument. |
|
181 |
If there is no nextPutLineBlock, nextPutAll/nextPut will be used (as inherited)" |
|
182 |
||
183 |
nextPutLineBlock notNil ifTrue:[ |
|
184 |
^ nextPutLineBlock value:something |
|
185 |
]. |
|
186 |
super nextPutLine:something |
|
1621 | 187 |
! |
188 |
||
189 |
peek |
|
190 |
"peek ahead for return the next element from the stream by evaluating the peekBlock" |
|
191 |
||
192 |
peekBlock notNil ifTrue:[ |
|
193 |
^ peekBlock value |
|
194 |
]. |
|
195 |
self error:'action for peek is undefined' |
|
196 |
! ! |
|
197 |
||
198 |
!ActorStream methodsFor:'converting'! |
|
199 |
||
200 |
readStream |
|
201 |
"return a readStream from the receiver. |
|
202 |
Since this is (hopefully) already a readStream, return self." |
|
203 |
||
204 |
^ self |
|
166 | 205 |
! ! |
206 |
||
207 |
!ActorStream methodsFor:'defining actions'! |
|
208 |
||
936 | 209 |
atEndBlock:aBlock |
210 |
"define the block to be evaluated for every atEnd-message" |
|
211 |
||
212 |
atEndBlock := aBlock |
|
213 |
! |
|
214 |
||
166 | 215 |
nextBlock:aBlock |
216 |
"define the block to be evaluated for every next-message" |
|
217 |
||
218 |
nextBlock := aBlock |
|
219 |
! |
|
220 |
||
221 |
nextPutAllBlock:aBlock |
|
987 | 222 |
"define the block to be evaluated for every nextPutAll-message. |
223 |
If undefined, nextPuts will be used (as inherited)" |
|
166 | 224 |
|
225 |
nextPutAllBlock := aBlock |
|
226 |
! |
|
227 |
||
228 |
nextPutBlock:aBlock |
|
229 |
"define the block to be evaluated for every nextPut-message" |
|
230 |
||
231 |
nextPutBlock := aBlock |
|
987 | 232 |
! |
233 |
||
234 |
nextPutLineBlock:aBlock |
|
235 |
"define the block to be evaluated for every nextPutLineBlock-message. |
|
236 |
If undefined, nextPutAll/nextPut will be used (as inherited)" |
|
237 |
||
238 |
nextPutLineBlock := aBlock |
|
1621 | 239 |
! |
240 |
||
241 |
peekBlock:aBlock |
|
242 |
"define the block to be evaluated for every peek-message" |
|
243 |
||
244 |
peekBlock := aBlock |
|
166 | 245 |
! ! |
246 |
||
247 |
!ActorStream methodsFor:'queries'! |
|
248 |
||
249 |
atEnd |
|
250 |
"return true, if at the end - actorStreams are never" |
|
251 |
||
936 | 252 |
atEndBlock isNil ifTrue:[ |
253 |
^ false |
|
254 |
]. |
|
255 |
^ atEndBlock value |
|
166 | 256 |
! ! |
257 |
||
258 |
!ActorStream class methodsFor:'documentation'! |
|
259 |
||
260 |
version |
|
4768 | 261 |
^ '$Header$' |
5153 | 262 |
! |
263 |
||
264 |
version_CVS |
|
265 |
^ '$Header$' |
|
166 | 266 |
! ! |
3089 | 267 |