transforms/Xtreams__ObjectWriteStream.st
author Martin Kobetic
Sun, 17 Nov 2013 00:23:18 -0500
changeset 147 bd6be28aa924
parent 111 44ac233b2f83
permissions -rw-r--r--
merging

"{ Package: 'stx:goodies/xtreams/transforms' }"

"{ NameSpace: Xtreams }"

WriteStream subclass:#ObjectWriteStream
	instanceVariableNames:'marshaler objects nothing int8 int_16 int_32 int_64 uint_16
		uint_32 uint_64 float_ double_'
	classVariableNames:''
	poolDictionaries:''
	category:'Xtreams-Transforms'
!

ObjectWriteStream comment:'Marshals objects into a binary destination using the associated ObjectMarshaler. The marshaler version ID is emited at the beginning.

Instance Variables
	marshaler	<ObjectMarshaler> performs marshaling of object bodies
	objects	<Array> retains unmarshaled objects for resolution of backward references (to preserve identity)
	longs	<InterpretedReadStream> (internal) for reading of longs
	longlongs	<InterpretedReadStream> (internal) for reading of longlongs
	floats	<InterpretedReadStream> (internal) for reading of floats
	doubles	<InterpretedReadStream> (internal) for reading of doubles
	nothing	<Object> (internal) an object used to occupy empty slots in objects (since nil is potentially valid content)

'
!


!ObjectWriteStream class methodsFor:'instance creation'!

on: aDestination
	^self on: aDestination marshaler: ObjectMarshaler new
!

on: aDestination marshaler: aMarshaler
	^self new on: aDestination marshaler: aMarshaler
! !

!ObjectWriteStream methodsFor:'accessing'!

put: object
	marshaler marshal: self object: object.
	self complete.
	^object
!

write: anInteger from: aSequenceableCollection at: startIndex
	startIndex to: anInteger + startIndex - 1 do: [:index |
		marshaler marshal: self object: (aSequenceableCollection at: index).
		self complete].
	^anInteger
! !

!ObjectWriteStream methodsFor:'initialize-release'!

contentsSpecies
	^Array
!

on: aDestination marshaler: aMarshaler
	super on: aDestination.
	marshaler := aMarshaler.
	objects := Array with: Transcript.
	nothing := Object new.

	int8 := aDestination interpreting: #signedChar.
	(marshaler configureMarshal: aDestination)
		ifTrue: [
			int_16 := aDestination interpreting: #signedShort_be.
			int_32 := aDestination interpreting: #signedLong_be.
			int_64 := aDestination interpreting: #signedLonglong_be.
			uint_16 := aDestination interpreting: #unsignedShort_be.
			uint_32 := aDestination interpreting: #unsignedLong_be.
			uint_64 := aDestination interpreting: #unsignedLonglong_be]
		ifFalse: [
			int_16 := aDestination interpreting: #signedShort_le.
			int_32 := aDestination interpreting: #signedLong_le.
			int_64 := aDestination interpreting: #signedLonglong_le.
			uint_16 := aDestination interpreting: #unsignedShort_le.
			uint_32 := aDestination interpreting: #unsignedLong_le.
			uint_64 := aDestination interpreting: #unsignedLonglong_le].
	float_ := aDestination interpreting: #float.
	double_ := aDestination interpreting: #double
! !

!ObjectWriteStream methodsFor:'primitives'!

double
	^double_
!

float
	^float_
!

int16
	^int_16
!

int32
	^int_32
!

int64
	^int_64
!

int8
	^int8
!

uint16
	^uint_16
!

uint32
	^uint_32
!

uint64
	^uint_64
!

uint8
	^destination
! !

!ObjectWriteStream methodsFor:'private'!

complete
	objects atAllPut: nothing.
	objects at: 1 put: Transcript
!

grow
	objects := objects copyWith: nothing
!

nothing
	^nothing
!

objects
	^objects
! !

!ObjectWriteStream class methodsFor:'documentation'!

version_HG

    ^ '$Changeset: <not expanded> $'
!

version_SVN
    ^ '$Id$'
! !