terminals/Xtreams__BlockClosureWriteStream.st
changeset 9 6c90659cf105
child 25 02e7c3b6f63c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/terminals/Xtreams__BlockClosureWriteStream.st	Mon Aug 22 16:04:00 2011 +0000
@@ -0,0 +1,77 @@
+"{ Package: 'stx:goodies/xtreams/terminals' }"
+
+"{ NameSpace: Xtreams }"
+
+WriteStream subclass:#BlockClosureWriteStream
+	instanceVariableNames:'contentsSpecies closeBlock'
+	classVariableNames:''
+	poolDictionaries:''
+	category:'My Classes'
+!
+
+BlockClosureWriteStream comment:'Write stream on a single argument block, the block is evaluated for each element written with the element assigned as the argument of the evaluation.
+{{{
+	"Transcript as an xtream"
+	[ :x | Transcript nextPut: x ] writing write: ''Hello World!!''
+}}}
+{{{
+	"/dev/null"
+	[ :x | ] writing write: ''Hello World!!''
+}}}
+
+Instance Variables
+	contentsSpecies	<Class> species for collections of elements of this stream
+	closeBlock	<BlockClosure> invoked in response to the #close message, allows customizing the close behavior
+
+'
+!
+
+
+!BlockClosureWriteStream methodsFor:'accessing'!
+
+write: anInteger from: aSequenceableCollection at: startIndex
+	| count |
+	count := 0.
+	[[count < anInteger] whileTrue:
+		[destination value: (aSequenceableCollection at: startIndex + count).
+		count := count + 1]]
+			on: Incomplete do: [(Incomplete on: aSequenceableCollection count: count at: startIndex) raise].
+	^anInteger
+! !
+
+!BlockClosureWriteStream methodsFor:'initialize-release'!
+
+close
+	closeBlock cull: self
+!
+
+closeBlock
+	^closeBlock
+!
+
+closeBlock: aBlock
+	closeBlock := aBlock
+!
+
+contentsSpecies
+	^contentsSpecies
+!
+
+contentsSpecies: anObject
+	contentsSpecies := anObject
+!
+
+flush
+!
+
+on: aBlockClosure
+	super on: aBlockClosure.
+	contentsSpecies := Array.
+	closeBlock := []
+! !
+
+!BlockClosureWriteStream class methodsFor:'documentation'!
+
+version_SVN
+    ^ '$Id$'
+! !