author | Claus Gittinger <cg@exept.de> |
Sat, 02 May 2020 21:40:13 +0200 | |
changeset 5476 | 7355a4b11cb6 |
parent 5461 | 2bf7d49fb682 |
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 |
" |
5239 | 65 |
! |
66 |
||
67 |
example |
|
68 |
" |
|
69 |
|s i| |
|
70 |
||
71 |
i := 0. |
|
72 |
s := ActorStream new. |
|
73 |
s contentsSpecies:OrderedCollection. |
|
74 |
s nextBlock:[i := i + 1. i]. |
|
75 |
s next. |
|
76 |
s next:10. |
|
77 |
" |
|
166 | 78 |
! ! |
79 |
||
80 |
!ActorStream class methodsFor:'instance creation'! |
|
81 |
||
82 |
new |
|
83 |
"have to re-allow new - it was disabled in Stream" |
|
84 |
^ self basicNew |
|
85 |
! ! |
|
86 |
||
1624 | 87 |
!ActorStream methodsFor:'accessing'! |
88 |
||
89 |
contentsSpecies |
|
3089 | 90 |
"return a class of which instances will be returned, when |
91 |
parts of the collection are asked for. |
|
92 |
(see upTo-kind of methods in Stream)" |
|
93 |
||
1624 | 94 |
^ contentsSpecies ? String |
95 |
! |
|
96 |
||
5254 | 97 |
contentsSpecies:aCollectionClass |
98 |
contentsSpecies := aCollectionClass. |
|
1624 | 99 |
! ! |
100 |
||
5239 | 101 |
!ActorStream methodsFor:'converting'! |
102 |
||
103 |
readStream |
|
104 |
"return a readStream from the receiver. |
|
105 |
Since this is (hopefully) already a readStream, return self." |
|
106 |
||
107 |
^ self |
|
108 |
! ! |
|
109 |
||
110 |
!ActorStream methodsFor:'defining actions'! |
|
111 |
||
112 |
atEndBlock:aBlock |
|
113 |
"define the block to be evaluated for every atEnd-message" |
|
114 |
||
115 |
atEndBlock := aBlock |
|
116 |
! |
|
117 |
||
118 |
nextBlock:aBlock |
|
119 |
"define the block to be evaluated for every next-message" |
|
120 |
||
121 |
nextBlock := aBlock |
|
122 |
! |
|
123 |
||
124 |
nextPutAllBlock:aBlock |
|
125 |
"define the block to be evaluated for every nextPutAll-message. |
|
126 |
If undefined, nextPuts will be used (as inherited)" |
|
127 |
||
128 |
nextPutAllBlock := aBlock |
|
129 |
! |
|
130 |
||
131 |
nextPutBlock:aBlock |
|
132 |
"define the block to be evaluated for every nextPut-message" |
|
133 |
||
134 |
nextPutBlock := aBlock |
|
135 |
! |
|
136 |
||
137 |
nextPutLineBlock:aBlock |
|
138 |
"define the block to be evaluated for every nextPutLineBlock-message. |
|
139 |
If undefined, nextPutAll/nextPut will be used (as inherited)" |
|
140 |
||
141 |
nextPutLineBlock := aBlock |
|
142 |
! |
|
143 |
||
144 |
peekBlock:aBlock |
|
145 |
"define the block to be evaluated for every peek-message" |
|
146 |
||
147 |
peekBlock := aBlock |
|
148 |
! ! |
|
149 |
||
150 |
!ActorStream methodsFor:'queries'! |
|
151 |
||
152 |
atEnd |
|
153 |
"return true, if at the end - actorStreams are never" |
|
154 |
||
155 |
atEndBlock isNil ifTrue:[ |
|
156 |
^ false |
|
157 |
]. |
|
158 |
^ atEndBlock value |
|
159 |
! ! |
|
160 |
||
161 |
!ActorStream methodsFor:'reading'! |
|
166 | 162 |
|
163 |
next |
|
164 |
"return the next element from the stream by evaluating the nextBlock" |
|
165 |
||
166 |
nextBlock notNil ifTrue:[ |
|
1624 | 167 |
^ nextBlock value |
166 | 168 |
]. |
169 |
self error:'action for next is undefined' |
|
170 |
! |
|
171 |
||
5239 | 172 |
peek |
173 |
"peek ahead for return the next element from the stream by evaluating the peekBlock" |
|
174 |
||
175 |
peekBlock notNil ifTrue:[ |
|
176 |
^ peekBlock value |
|
177 |
]. |
|
178 |
self error:'action for peek is undefined' |
|
179 |
! ! |
|
180 |
||
181 |
!ActorStream methodsFor:'writing'! |
|
182 |
||
5461 | 183 |
flush |
184 |
"any buffered, but not yet written partial line? |
|
185 |
This only happens if the actorStream has only a nextPutLine action, |
|
186 |
but no nextPutAll/nextPut action, |
|
187 |
and no linefeed appeared. |
|
188 |
Useful to ensure that any remaining data is processed." |
|
189 |
||
190 |
bufferedLine isEmptyOrNil ifTrue:[ |
|
191 |
^ self. |
|
192 |
]. |
|
193 |
self nextPutLine:bufferedLine. |
|
194 |
bufferedLine := ''. |
|
195 |
! |
|
196 |
||
4968 | 197 |
nextPut:aCharacter |
5165 | 198 |
"put aCharacter onto the stream by evaluating the nextPutBlock with aCharacter as argument. |
199 |
Answer anObject" |
|
166 | 200 |
|
987 | 201 |
nextPutBlock notNil ifTrue:[ |
5151 | 202 |
nextPutBlock value:aCharacter. |
5165 | 203 |
^ aCharacter. |
166 | 204 |
]. |
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
205 |
nextPutAllBlock notNil ifTrue:[ |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
206 |
"/ fallBack to nextPutAll. |
4968 | 207 |
self nextPutAll:(aCharacter asString). |
5165 | 208 |
^ aCharacter |
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
209 |
]. |
4968 | 210 |
"/ if there is a nextPutLineBlock, then collect the line in a buffer and wait for the cr |
211 |
nextPutLineBlock notNil ifTrue:[ |
|
212 |
aCharacter == Character cr ifFalse:[ |
|
213 |
bufferedLine := (bufferedLine ? '') , aCharacter. |
|
214 |
] ifTrue:[ |
|
215 |
self nextPutLine:(bufferedLine ? ''). |
|
216 |
bufferedLine := ''. |
|
217 |
]. |
|
5165 | 218 |
^ aCharacter |
4968 | 219 |
]. |
220 |
||
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
221 |
self error:'action for nextPut:/nextPutAll: are undefined' |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
222 |
|
4968 | 223 |
"Modified: / 28-01-2002 / 20:59:32 / micha" |
224 |
"Modified: / 29-05-2019 / 23:28:34 / Claus Gittinger" |
|
166 | 225 |
! |
226 |
||
227 |
nextPutAll:something |
|
228 |
"put all elements of something onto the stream by evaluating |
|
987 | 229 |
the nextPutAllBlock with something as argument. |
5157 | 230 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited). |
231 |
Answer the receiver." |
|
166 | 232 |
|
987 | 233 |
nextPutAllBlock notNil ifTrue:[ |
5151 | 234 |
nextPutAllBlock value:something. |
235 |
] ifFalse:[ |
|
236 |
super nextPutAll:something |
|
166 | 237 |
]. |
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
238 |
! |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
239 |
|
3531 | 240 |
nextPutAll:count from:buffer startingAt:start |
241 |
"put some elements of something onto the stream by evaluating |
|
242 |
the nextPutAllBlock with something as argument. |
|
5151 | 243 |
If there is no nextPutAllBlock, individual nextPuts will be used (as inherited). |
244 |
Answer the number of elements that were appended." |
|
3531 | 245 |
|
246 |
nextPutAllBlock notNil ifTrue:[ |
|
247 |
nextPutAllBlock value:(buffer copyFrom:start to:start+count-1). |
|
248 |
^ count |
|
249 |
]. |
|
250 |
^ super nextPutAll:count from:buffer startingAt:start |
|
251 |
! |
|
252 |
||
3469 | 253 |
nextPutAll:something startingAt:start to:stop |
254 |
"put some elements of something onto the stream by evaluating |
|
255 |
the nextPutAllBlock with something as argument. |
|
256 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited)" |
|
257 |
||
258 |
nextPutAllBlock notNil ifTrue:[ |
|
4790 | 259 |
nextPutAllBlock value:(something copyFrom:start to:stop). |
260 |
^ self. |
|
3469 | 261 |
]. |
262 |
super nextPutAll:something startingAt:start to:stop |
|
4768 | 263 |
|
264 |
"Modified: / 22-11-2018 / 12:52:51 / Stefan Vogel" |
|
4790 | 265 |
"Modified: / 14-01-2019 / 18:23:40 / Claus Gittinger" |
3469 | 266 |
! |
267 |
||
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
268 |
nextPutByte:something |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
269 |
"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
|
270 |
something as argument" |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
271 |
|
5153 | 272 |
self nextPut:(something asCharacter). |
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
273 |
|
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
274 |
"Created: 6.8.1997 / 00:44:32 / cg" |
987 | 275 |
! |
276 |
||
277 |
nextPutLine:something |
|
278 |
"put the line onto the stream by evaluating |
|
279 |
the nextPutLineBlock with something as argument. |
|
280 |
If there is no nextPutLineBlock, nextPutAll/nextPut will be used (as inherited)" |
|
281 |
||
282 |
nextPutLineBlock notNil ifTrue:[ |
|
283 |
^ nextPutLineBlock value:something |
|
284 |
]. |
|
285 |
super nextPutLine:something |
|
166 | 286 |
! ! |
287 |
||
288 |
!ActorStream class methodsFor:'documentation'! |
|
289 |
||
290 |
version |
|
4768 | 291 |
^ '$Header$' |
5153 | 292 |
! |
293 |
||
294 |
version_CVS |
|
295 |
^ '$Header$' |
|
166 | 296 |
! ! |
3089 | 297 |