/*
* TclInteger.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: TclInteger.java,v 1.5 2000/10/29 06:00:42 mdejong Exp $
*
*/
using System;
namespace tcl.lang
{
  ///  This class implements the integer object type in Tcl.
  public class TclInteger : InternalRep
  {
    ///  Internal representation of a integer value.
    private int value;
    ///  Construct a TclInteger representation with the given integer value.
    private TclInteger( int i )
    {
      value = i;
    }
    ///  Construct a TclInteger representation with the initial value taken
    /// from the given string.
    /// 
    /// 
    /// current interpreter.
    /// 
    /// string rep of the integer.
    /// 
    ///  TclException if the string is not a well-formed Tcl integer
    /// value.
    /// 
    private TclInteger( Interp interp, string str )
    {
      value = Util.getInt( interp, str );
    }
    ///  Returns a dupilcate of the current object.
    /// the TclObject that contains this internalRep.
    /// 
    public InternalRep duplicate()
    {
      return new TclInteger( 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()
    {
      return value.ToString();
    }
    ///  TCL.Tcl_NewIntObj -> TclInteger.newInstance
    /// 
    /// Creates a new instance of a TclObject with a TclInteger internal
    /// representation.
    /// 
    /// 
    /// initial value of the integer object.
    /// 
    ///  the TclObject with the given integer value.
    /// 
    public static TclObject newInstance( int i )
    {
      return new TclObject( new TclInteger( i ) );
    }
    ///  SetIntFromAny -> TclInteger.setIntegerFromAny
    /// 
    /// Called to convert the other object's internal rep to this type.
    /// 
    /// 
    /// current interpreter.
    /// 
    /// true if this methid is called by getForIndex.
    /// 
    /// the TclObject to convert to use the
    /// representation provided by this class.
    /// 
    private static void setIntegerFromAny( Interp interp, TclObject tobj )
    {
      InternalRep rep = tobj.InternalRep;
      if ( rep is TclInteger )
      {
        // Do nothing.
      }
      else if ( rep is TclBoolean )
      {
        bool b = TclBoolean.get( interp, tobj );
        if ( b )
        {
          tobj.InternalRep = new TclInteger( 1 );
        }
        else
        {
          tobj.InternalRep = new TclInteger( 0 );
        }
      }
      else
      {
        // (ToDo) other short-cuts
        tobj.InternalRep = new TclInteger( interp, tobj.ToString() );
      }
    }
    ///  TCL.Tcl_GetIntFromObj -> TclInteger.get
    /// 
    /// Returns the integer value of the object.
    /// 
    /// 
    /// current interpreter.
    /// 
    /// the object to operate on.
    /// 
    ///  the integer value of the object.
    /// 
    public static int get( Interp interp, TclObject tobj )
    {
      setIntegerFromAny( interp, tobj );
      TclInteger tint = (TclInteger)tobj.InternalRep;
      return tint.value;
    }
    ///  Changes the integer value of the object.
    /// 
    /// 
    /// current interpreter.
    /// 
    /// the object to operate on.
    /// @paran i the new integer value.
    /// 
    public static void set( TclObject tobj, int i )
    {
      tobj.invalidateStringRep();
      InternalRep rep = tobj.InternalRep;
      TclInteger tint;
      if ( rep is TclInteger )
      {
        tint = (TclInteger)rep;
        tint.value = i;
      }
      else
      {
        tobj.InternalRep = new TclInteger( i );
      }
    }
  }
}