/*
* TclBoolean.java
*
* Copyright (c) 1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*
* Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
*
* RCS @(#) $Id: TclBoolean.java,v 1.2 2000/10/29 06:00:42 mdejong Exp $
*
*/
using System;
namespace tcl.lang
{
/// This class implements the boolean object type in Tcl.
public class TclBoolean : InternalRep
{
/// Internal representation of a boolean value.
private bool value;
/// Construct a TclBoolean representation with the given boolean value.
///
///
/// initial boolean value.
///
private TclBoolean( bool b )
{
value = b;
}
/// Construct a TclBoolean representation with the initial value taken
/// from the given string.
///
///
/// current interpreter.
///
/// TclException if the string is not a well-formed Tcl boolean
/// value.
///
private TclBoolean( Interp interp, string str )
{
value = Util.getBoolean( interp, str );
}
/// Returns a dupilcate of the current object.
///
///
/// the TclObject that contains this ObjType.
///
public InternalRep duplicate()
{
return new TclBoolean( value );
}
/// Implement this no-op for the InternalRep interface.
public void dispose()
{
}
/// Called to query the string representation of the Tcl object. This
/// method is called only by TclObject.toString() when
/// TclObject.stringRep is null.
///
///
/// the string representation of the Tcl object.
///
public override string ToString()
{
if ( value )
{
return "1";
}
else
{
return "0";
}
}
/// Creates a new instance of a TclObject with a TclBoolean internal
/// representation.
///
///
/// initial value of the boolean object.
///
/// the TclObject with the given boolean value.
///
public static TclObject newInstance( bool b )
{
return new TclObject( new TclBoolean( b ) );
}
/// Called to convert the other object's internal rep to boolean.
///
///
/// current interpreter.
///
/// the TclObject to convert to use the
/// representation provided by this class.
///
private static void setBooleanFromAny( Interp interp, TclObject tobj )
{
InternalRep rep = tobj.InternalRep;
if ( rep is TclBoolean )
{
/*
* Do nothing.
*/
}
else if ( rep is TclInteger )
{
int i = TclInteger.get( interp, tobj );
tobj.InternalRep = new TclBoolean( i != 0 );
}
else
{
/*
* (ToDo) other short-cuts
*/
tobj.InternalRep = new TclBoolean( interp, tobj.ToString() );
}
}
/// Returns the value of the object as an boolean.
///
///
/// current interpreter.
///
/// the TclObject to use as an boolean.
///
/// the boolean value of the object.
///
/// TclException if the object cannot be converted into a
/// boolean.
///
public static bool get( Interp interp, TclObject tobj )
{
setBooleanFromAny( interp, tobj );
TclBoolean tbool = (TclBoolean)( tobj.InternalRep );
return tbool.value;
}
}
}