浏览代码

Added SharedPath property to Stream for passing an exising stream as a path param.

Mark Sibly 7 年之前
父节点
当前提交
9aa20e815d
共有 1 个文件被更改,包括 42 次插入0 次删除
  1. 42 0
      modules/std/stream/stream.monkey2

+ 42 - 0
modules/std/stream/stream.monkey2

@@ -31,6 +31,14 @@ Class Stream Extends std.resource.Resource
 	#end
 	#end
 	Method Close:Void()
 	Method Close:Void()
 		
 		
+		If Not _open Return
+		
+		_open-=1
+		
+		If _open Return
+		
+		If _sharedPath _shared.Remove( _sharedPath )
+		
 		OnClose()
 		OnClose()
 	End
 	End
 
 
@@ -192,6 +200,30 @@ Class Stream Extends std.resource.Resource
 		Return n
 		Return n
 	End
 	End
 	
 	
+	Property SharedPath:String()
+		
+		If Not _sharedPath
+			
+			Global sharedId:=0
+			sharedId+=1
+			
+			Local path:="@"+sharedId
+			_sharedPath="stream::"+path
+			_shared[path]=Self
+		Endif
+		
+		Return _sharedPath
+	End
+	
+	#rem monkeydoc Reopens the stream.
+	#end
+	Method Reopen:Stream()
+		
+		_open+=1
+		
+		Return Self
+	End
+	
 	#rem monkeydoc Reads a byte from the stream.
 	#rem monkeydoc Reads a byte from the stream.
 	
 	
 	@return The byte read.
 	@return The byte read.
@@ -564,6 +596,8 @@ Class Stream Extends std.resource.Resource
 		Local proto:=path.Slice( 0,i )
 		Local proto:=path.Slice( 0,i )
 		Local ipath:=path.Slice( i+2 )
 		Local ipath:=path.Slice( i+2 )
 		
 		
+		If proto="stream" Return _shared[ipath].Reopen()
+		
 		Return OpenFuncs[proto]( proto,ipath,mode )
 		Return OpenFuncs[proto]( proto,ipath,mode )
 	End
 	End
 	
 	
@@ -580,6 +614,8 @@ Class Stream Extends std.resource.Resource
 	Method New()
 	Method New()
 		
 		
 		_swap=false
 		_swap=false
+		
+		_open=1
 	End
 	End
 	
 	
 	Method OnClose() Virtual
 	Method OnClose() Virtual
@@ -591,6 +627,12 @@ Class Stream Extends std.resource.Resource
 	
 	
 	Field _swap:Bool
 	Field _swap:Bool
 	
 	
+	Field _open:Int
+	
+	Field _sharedPath:String
+	
+	Global _shared:=New StringMap<Stream>
+	
 	Function Swap2( v:Void Ptr )
 	Function Swap2( v:Void Ptr )
 		Local t:=Cast<UShort Ptr>( v )[0]
 		Local t:=Cast<UShort Ptr>( v )[0]
 		Cast<UShort Ptr>( v )[0]=(t Shr 8 & $ff) | (t & $ff) Shl 8
 		Cast<UShort Ptr>( v )[0]=(t Shr 8 & $ff) | (t & $ff) Shl 8