//
//
//
//
// $Revision$
//
using System;
using System.Collections.Generic;
using Debugger.Expressions;
using Debugger.Wrappers.CorDebug;
// TODO: Test non-zero LowerBound
// TODO: Test very large arrays (Length > Int32.MaxValue)
namespace Debugger
{
// This part of the class provides support for arrays
public partial class Value
{
ICorDebugArrayValue CorArrayValue {
get {
if (IsNull) throw new GetValueException("Value is null");
if (!this.Type.IsArray) throw new DebuggerException("Value is not an array");
return this.CorReferenceValue.Dereference().CastTo();
}
}
///
/// Gets the number of elements in the array.
/// eg new object[4,5] returns 20
///
public int ArrayLength {
get {
return (int)CorArrayValue.Count;
}
}
///
/// Gets the number of dimensions of the array.
/// eg new object[4,5] returns 2
///
public int ArrayRank {
get {
return (int)CorArrayValue.Rank;
}
}
/// Gets the dimensions of the array
public ArrayDimensions ArrayDimensions {
get {
int rank = this.ArrayRank;
uint[] baseIndicies;
if (CorArrayValue.HasBaseIndicies() == 1) {
baseIndicies = CorArrayValue.BaseIndicies;
} else {
baseIndicies = new uint[this.ArrayRank];
}
uint[] dimensionCounts = CorArrayValue.Dimensions;
List dimensions = new List();
for(int i = 0; i < rank; i++) {
dimensions.Add(new ArrayDimension((int)baseIndicies[i], (int)baseIndicies[i] + (int)dimensionCounts[i] - 1));
}
return new ArrayDimensions(dimensions);
}
}
/// Returns an element of a single-dimensional array
public Value GetArrayElement(int index)
{
return GetArrayElement(new int[] {index});
}
/// Returns an element of an array
public Value GetArrayElement(int[] elementIndices)
{
int[] indices = (int[])elementIndices.Clone();
return new Value(Process, new ArrayIndexerExpression(this.Expression, indices), GetCorValueOfArrayElement(indices));
}
// May be called later
ICorDebugValue GetCorValueOfArrayElement(int[] indices)
{
if (indices.Length != ArrayRank) {
throw new GetValueException("Given indicies do not have the same dimension as array.");
}
if (!this.ArrayDimensions.IsIndexValid(indices)) {
throw new GetValueException("Given indices are out of range of the array");
}
return CorArrayValue.GetElement(indices);
}
/// Returns all elements in the array
public Value[] GetArrayElements()
{
List values = new List();
foreach(int[] indices in this.ArrayDimensions.Indices) {
values.Add(GetArrayElement(indices));
}
return values.ToArray();
}
}
}