author | Claus Gittinger <cg@exept.de> |
Mon, 15 Jul 2019 15:39:26 +0200 | |
changeset 5054 | b01df68cba0a |
parent 4968 | 99de1b56d09f |
child 5151 | bfed222dcaad |
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:[ |
4968 | 104 |
^ nextPutBlock value:aCharacter |
166 | 105 |
]. |
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
106 |
nextPutAllBlock notNil ifTrue:[ |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
107 |
"/ fallBack to nextPutAll. |
4968 | 108 |
self nextPutAll:(aCharacter asString). |
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
109 |
^ self |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
110 |
]. |
4968 | 111 |
"/ if there is a nextPutLineBlock, then collect the line in a buffer and wait for the cr |
112 |
nextPutLineBlock notNil ifTrue:[ |
|
113 |
aCharacter == Character cr ifFalse:[ |
|
114 |
bufferedLine := (bufferedLine ? '') , aCharacter. |
|
115 |
] ifTrue:[ |
|
116 |
self nextPutLine:(bufferedLine ? ''). |
|
117 |
bufferedLine := ''. |
|
118 |
]. |
|
119 |
^ self |
|
120 |
]. |
|
121 |
||
1008
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
122 |
self error:'action for nextPut:/nextPutAll: are undefined' |
67f75385259a
allow for single nextPutAll block
Michael Beyl <mb@exept.de>
parents:
987
diff
changeset
|
123 |
|
4968 | 124 |
"Modified: / 28-01-2002 / 20:59:32 / micha" |
125 |
"Modified: / 29-05-2019 / 23:28:34 / Claus Gittinger" |
|
166 | 126 |
! |
127 |
||
128 |
nextPutAll:something |
|
129 |
"put all elements of something onto the stream by evaluating |
|
987 | 130 |
the nextPutAllBlock with something as argument. |
131 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited)" |
|
166 | 132 |
|
987 | 133 |
nextPutAllBlock notNil ifTrue:[ |
134 |
^ nextPutAllBlock value:something |
|
166 | 135 |
]. |
136 |
super nextPutAll:something |
|
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
137 |
! |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
138 |
|
3531 | 139 |
nextPutAll:count from:buffer startingAt:start |
140 |
"put some elements of something onto the stream by evaluating |
|
141 |
the nextPutAllBlock with something as argument. |
|
142 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited)" |
|
143 |
||
144 |
nextPutAllBlock notNil ifTrue:[ |
|
145 |
nextPutAllBlock value:(buffer copyFrom:start to:start+count-1). |
|
146 |
^ count |
|
147 |
]. |
|
148 |
^ super nextPutAll:count from:buffer startingAt:start |
|
149 |
! |
|
150 |
||
3469 | 151 |
nextPutAll:something startingAt:start to:stop |
152 |
"put some elements of something onto the stream by evaluating |
|
153 |
the nextPutAllBlock with something as argument. |
|
154 |
If there is no nextPutAllBlock, nextPuts will be used (as inherited)" |
|
155 |
||
156 |
nextPutAllBlock notNil ifTrue:[ |
|
4790 | 157 |
nextPutAllBlock value:(something copyFrom:start to:stop). |
158 |
^ self. |
|
3469 | 159 |
]. |
160 |
super nextPutAll:something startingAt:start to:stop |
|
4768 | 161 |
|
162 |
"Modified: / 22-11-2018 / 12:52:51 / Stefan Vogel" |
|
4790 | 163 |
"Modified: / 14-01-2019 / 18:23:40 / Claus Gittinger" |
3469 | 164 |
! |
165 |
||
554
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
166 |
nextPutByte:something |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
167 |
"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
|
168 |
something as argument" |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
169 |
|
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
170 |
self nextPut:(something asCharacter) |
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
171 |
|
84eb4d58dd04
allow binary data to be sent via #nextPutByte:.
Claus Gittinger <cg@exept.de>
parents:
446
diff
changeset
|
172 |
"Created: 6.8.1997 / 00:44:32 / cg" |
987 | 173 |
! |
174 |
||
175 |
nextPutLine:something |
|
176 |
"put the line onto the stream by evaluating |
|
177 |
the nextPutLineBlock with something as argument. |
|
178 |
If there is no nextPutLineBlock, nextPutAll/nextPut will be used (as inherited)" |
|
179 |
||
180 |
nextPutLineBlock notNil ifTrue:[ |
|
181 |
^ nextPutLineBlock value:something |
|
182 |
]. |
|
183 |
super nextPutLine:something |
|
1621 | 184 |
! |
185 |
||
186 |
peek |
|
187 |
"peek ahead for return the next element from the stream by evaluating the peekBlock" |
|
188 |
||
189 |
peekBlock notNil ifTrue:[ |
|
190 |
^ peekBlock value |
|
191 |
]. |
|
192 |
self error:'action for peek is undefined' |
|
193 |
! ! |
|
194 |
||
195 |
!ActorStream methodsFor:'converting'! |
|
196 |
||
197 |
readStream |
|
198 |
"return a readStream from the receiver. |
|
199 |
Since this is (hopefully) already a readStream, return self." |
|
200 |
||
201 |
^ self |
|
166 | 202 |
! ! |
203 |
||
204 |
!ActorStream methodsFor:'defining actions'! |
|
205 |
||
936 | 206 |
atEndBlock:aBlock |
207 |
"define the block to be evaluated for every atEnd-message" |
|
208 |
||
209 |
atEndBlock := aBlock |
|
210 |
! |
|
211 |
||
166 | 212 |
nextBlock:aBlock |
213 |
"define the block to be evaluated for every next-message" |
|
214 |
||
215 |
nextBlock := aBlock |
|
216 |
! |
|
217 |
||
218 |
nextPutAllBlock:aBlock |
|
987 | 219 |
"define the block to be evaluated for every nextPutAll-message. |
220 |
If undefined, nextPuts will be used (as inherited)" |
|
166 | 221 |
|
222 |
nextPutAllBlock := aBlock |
|
223 |
! |
|
224 |
||
225 |
nextPutBlock:aBlock |
|
226 |
"define the block to be evaluated for every nextPut-message" |
|
227 |
||
228 |
nextPutBlock := aBlock |
|
987 | 229 |
! |
230 |
||
231 |
nextPutLineBlock:aBlock |
|
232 |
"define the block to be evaluated for every nextPutLineBlock-message. |
|
233 |
If undefined, nextPutAll/nextPut will be used (as inherited)" |
|
234 |
||
235 |
nextPutLineBlock := aBlock |
|
1621 | 236 |
! |
237 |
||
238 |
peekBlock:aBlock |
|
239 |
"define the block to be evaluated for every peek-message" |
|
240 |
||
241 |
peekBlock := aBlock |
|
166 | 242 |
! ! |
243 |
||
244 |
!ActorStream methodsFor:'queries'! |
|
245 |
||
246 |
atEnd |
|
247 |
"return true, if at the end - actorStreams are never" |
|
248 |
||
936 | 249 |
atEndBlock isNil ifTrue:[ |
250 |
^ false |
|
251 |
]. |
|
252 |
^ atEndBlock value |
|
166 | 253 |
! ! |
254 |
||
255 |
!ActorStream class methodsFor:'documentation'! |
|
256 |
||
257 |
version |
|
4768 | 258 |
^ '$Header$' |
166 | 259 |
! ! |
3089 | 260 |