Stream Read/Write Object

BlitzMax Forums/BlitzMax Programming/Stream Read/Write Object

Brendane(Posted 2006) [#1]
How come stream ReadObject() and WriteObject() aren't implemented yet?


Dreamora(Posted 2006) [#2]
Don't know the answer to your exact question, but I've something to mentioned:

Thats something thats better implemented on the object side, not the stream one.

Everything that extends TData is capable of writting and reading itself from a stream.
See TList for example.


Brendane(Posted 2006) [#3]
Yes that's exactly what I'm referring to. I've just extended my types to extend TData and written a TDataType object for them to read/write... but.. TStream doesn't have any code in the ReadObject/WriteObject() functions - it just throws exceptions - and it doesn't appear that any streams override these.

So.....?


Brendane(Posted 2006) [#4]
Also.. this must be a bug in TList since it doesn't even return the object after reading it :-

	Method ReadObject:Object( stream:TStream )
		Local t:TList=New TList
		Local count=stream.ReadInt()
		For Local i=0 Until count
			t.AddLast stream.ReadObject()
		Next
	End Method



Brendane(Posted 2006) [#5]
I am bearing in mind that these functions are still undocumented, but then, so is alot of other stuff that IS meant to be used.

Here's a thought, how about some proper documentation?


Yan(Posted 2006) [#6]
TStream doesn't have any code in the ReadObject/WriteObject() functions - it just throws exceptions - and it doesn't appear that any streams override these.

There's a TDataStream - "data::url".


Brendane(Posted 2006) [#7]
Aha! Thanks Yan.

In that case there is a bug in TList.ReadObject() - I'll post it in the bug section.


H&K(Posted 2006) [#8]
I had thought that these ( .ReadObject() etc) need to be overridden in all the extended types,

That is, it cannot read an "Object" because an "Object" is zero bytes long.


Brendane(Posted 2006) [#9]
H&K: No, your type need to extend TData.

TData is an interface to force your object to provide a DataType() function. This function should return an instance of a TDataType object (which you yourself write as a type which extends TDataType) - it is this object which provides the ReadObject/WriteObject() functions for the stream.

Example :-
Global myDataType:TMyDataType=New TMyDataType

Type TMyDataType Extends TDataType

	Method Tag$()
		Return "MyDataType"
	End Method
	
	Method ReadObject:Object( stream:TStream )
		Local t:MyType=New MyType
		t.i=stream.ReadInt()
		Return t
	End Method
	
	Method WriteObject( o:Object,stream:TStream )
		Local t:MyType=MyType(o)
		stream.WriteInt t.i
	End Method
End Type

Type MyType Extends TData
	Field i:Int
	
	Method DataType:TMyDataType()
		Return myDataType
	End Method	
EndType



H&K(Posted 2006) [#10]
Ahhh, yet somethig else I dont understand ;(

I was of the understanding that every Type was unltematly extended from "Object". But oh well, at least it you who are stuck ;) (As apposed to me). I still dont get how readobject is supposed to know how big an object is, unless its overridden in the extended types. (I can see your code, and you have overridden it)

But your code is clear, so I dont think I am going to understand it, until hopefuly I need it


Dreamora(Posted 2006) [#11]
How does it know: You have to write the reading and writting functionality manually yourself. if you do something wrong it will end wrong. it does not know something itself.
Its up to you what you write and do not write ultimately.


H&K(Posted 2006) [#12]
@Dream,

Does that mean that I was in fact right? Brill.