How to open files with extended TStream

BlitzMax Forums/BlitzMax Beginners Area/How to open files with extended TStream

Klaas(Posted 2005) [#1]
Hi,
i want to extend TStream to support some special Datatypes.

I've created a Class:
Type my_stream Extends TStream
	Method read_ul8:Int()

		Return ReadByte()
	End Method
End Type


normaly i would
stream:Tstream = OpenStream("lalala",1,0)

now i wonder, how can i use my new class. How can i load a file with this extended stream class ?


Perturbatio(Posted 2005) [#2]
presumably:
myStream:my_stream = OpenStream("lalala",1,0)



Klaas(Posted 2005) [#3]
but this gives me a "Compile Error - Unable to Convert form 'TStream' to 'my_stream'"


Dreamora(Posted 2005) [#4]
btw: if you return ReadByte, why don't you do :Byte instead of :Int? the return will be wrong if you assign it to a variable of type byte.


Klaas(Posted 2005) [#5]
yes, i know ... i've simplified the problem and didnt take care of that


Perturbatio(Posted 2005) [#6]
Type TMyStream Extends TStream
	Method read_ul8:Int()

		Return ReadByte()
	End Method
End Type

myStream:TMyStream = New TMyStream

myStream = TMyStream(OpenStream("http::www.google.com/",1,0))


Try this. (It compiles at least).


Klaas(Posted 2005) [#7]
thx a lot


Klaas(Posted 2005) [#8]
uh ... damned it does not work

Type TMyStream Extends TStream
	Method read_ul8:Int()

		Return ReadByte()
	End Method
End Type

myStream:TMyStream = New TMyStream
myStream = TMyStream(OpenStream("http::www.google.com/",1,0))
If mystream <> Null Then 
	Print "got it"
Else
	Print "no way"
End If

myStream2:TStream = New TStream
myStream2 = tstream(OpenStream("http::www.google.com/",1,0))
If mystream2 <> Null Then 
	Print "got it"
Else
	Print "no way"
End If


is my idea to extend TStream stupid? should i write a class that doesnt extend TSream but got a TStream to do IO ?

I've just read "To create your own stream types, you should extend TStream and implement at least these methods. " in the command reference.


Michael Reitzenstein(Posted 2005) [#9]
!!!

OpenStream doesn't return a my_stream object downcast to TStream - it returns a TStream! You can't cast it into my_stream. You can attempt to cast TStream to my_stream - that's why it compiles - but it won't work unless that TStream is a downcast my_stream.

There's a stream factory list in OpenStream (check the code), but if you're only extending TStream then that's useless, because it'll return a TStream before anything else.

I'm not really sure at all how to have access to an extended stream object without creating it yourself.