'Classes/System/Console.ab

Namespace System

/*
@brief コンソール入出力・ウィンドウなどのクラス
@date 2008/02/26
@auther Egtra
*/
Class Console
Public
	/*
	@brief 標準出力を設定する
	@date 2008/06/21
	@auther overtaker
	*/
	Static Sub SetOut(newOut As IO.TextWriter)
		If ActiveBasic.IsNothing(newOut) Then
			Throw New ArgumentNullException("newOut")
		End If
		out = newOut
	End Sub

	/*
	@brief 標準出力を取得する
	@date 2008/06/21
	@auther overtaker
	*/
	Static Function Out() As IO.TextWriter
		Out = out
	End Function

	/*
	@brief 標準出力に1行書き込む
	@date 2008/06/21
	@auther overtaker
	*/
	Static Sub WriteLine(value As String)
		out.WriteLine(value)
		out.Flush()
	End Sub

	Static Sub WriteLine()
		out.WriteLine()
		out.Flush()
	End Sub

	Static Sub WriteLine(x As Boolean)
		WriteLine(Str$(x))
	End Sub
	
	Static Sub WriteLine(x As Char)
		WriteLine(Chr$(x))
	End Sub

	Static Sub WriteLine(x As Byte)
		WriteLine(Str$(x))
	End Sub
#ifdef UNICODE
	Static Sub WriteLine(x As SByte)
		WriteLine(Str$(x))
	End Sub
#else
	Static Sub WriteLine(x As Word)
		WriteLine(Str$(x))
	End Sub
#endif
	Static Sub WriteLine(x As Integer)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As DWord)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As Long)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As QWord)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As Int64)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As Single)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As Double)
		WriteLine(Str$(x))
	End Sub

	Static Sub WriteLine(x As Object)
		WriteLine(x.ToString)
	End Sub

	/*
	@brief 標準出力に書き込む
	@date 2008/06/21
	@auther overtaker
	*/
	Static Sub Write(s As String)
		out.Write(s)
		out.Flush()
	End Sub

	Static Sub Write(x As Boolean)
		Write(Str$(x))
	End Sub

	Static Sub Write(x As Char)
		Write(Chr$(x))
	End Sub

	Static Sub Write(x As Byte)
		Write(Str$(x))
	End Sub
#ifdef UNICODE
	Static Sub Write(x As SByte)
		Write(Str$(x))
	End Sub
#else
	Static Sub Write(x As Word)
		Write(Str$(x))
	End Sub
#endif
	Static Sub Write(x As Integer)
		Write(Str$(x))
	End Sub

	Static Sub Write(x As DWord)
		Write(Str$(x))
	End Sub

	Static Sub Write(x As Long)
		Write(Str$(x))
	End Sub

	Static Sub Write(x As QWord)
		Write(Str$(x))
	End Sub

	Static Sub Write(x As Int64)
		Write(Str$(x))
	End Sub

	Static Sub Write(x As Object)
		Write(x.ToString)
	End Sub

	/*
	@brief 標準入力を設定する
	@date 2008/02/26
	@auther Egtra
	*/
	Static Sub SetIn(newIn As IO.TextReader)
		If ActiveBasic.IsNothing(newIn) Then
			Throw New ArgumentNullException("newIn")
		End If
		in = newIn
	End Sub

	/*
	@brief 標準入力を取得する
	@date 2008/02/26
	@auther Egtra
	*/
	Static Function In() As IO.TextReader
		In = in
	End Function

	/*
	@brief 標準入力から1行読み込む
	@date 2008/02/26
	@auther Egtra
	*/
	Static Function ReadLine() As String
		ReadLine = in.ReadLine()
	End Function

	/*
	@brief 標準入力から1行読み込む
	@date 2008/02/26
	@auther Egtra
	*/
	Static Function Read() As Long
		Read = in.Read()
	End Function

Private
	Function enter() As ActiveBasic.Windows.CriticalSectionLock
		Imports ActiveBasic.Windows
		If ActiveBasic.IsNothing(cs) Then
			Dim lock = New CriticalSectionLock(_System_CriticalSection)
			Try
				If ActiveBasic.IsNothing(cs) Then
					cs = New CriticalSection
				End If
			Finally
				lock.Dispose()
			End Try
		End If
		enter = cs.Enter
	End Function

	Static in = Nothing As IO.TextReader
	Static out = Nothing As IO.TextWriter
	Static cs = Nothing As ActiveBasic.Windows.CriticalSection
End Class

End Namespace 'System
