2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / Mono.Debugger.Soft / Mono.Debugger.Soft / ArrayMirror.cs
blobc58b2f0192c57d36feed931a2b61fdb28988ce68
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
5 namespace Mono.Debugger.Soft
7 public class ArrayMirror : ObjectMirror, IEnumerable {
9 public int[] lengths;
10 public int[] lower_bounds;
11 public int rank;
13 internal ArrayMirror (VirtualMachine vm, long id) : base (vm, id) {
16 public int Length {
17 get {
18 GetLengths ();
20 int length = lengths [0];
22 for (int i = 1; i < Rank; i++) {
23 length *= lengths [i];
26 return length;
30 public int Rank {
31 get {
32 GetLengths ();
34 return rank;
38 public int GetLength (int dimension) {
39 GetLengths ();
41 if (dimension < 0 || dimension >= Rank)
42 throw new ArgumentOutOfRangeException ("dimension");
44 return lengths [dimension];
47 public int GetLowerBound (int dimension) {
48 GetLengths ();
50 if (dimension < 0 || dimension >= Rank)
51 throw new ArgumentOutOfRangeException ("dimension");
53 return lower_bounds [dimension];
56 void GetLengths () {
57 if (lengths == null)
58 lengths = vm.conn.Array_GetLength (id, out this.rank, out this.lower_bounds);
61 public Value this [int index] {
62 get {
63 // FIXME: Multiple dimensions
64 if (index < 0 || index > Length - 1)
65 throw new IndexOutOfRangeException ();
66 return vm.DecodeValue (vm.conn.Array_GetValues (id, index, 1) [0]);
68 set {
69 // FIXME: Multiple dimensions
70 if (index < 0 || index > Length - 1)
71 throw new IndexOutOfRangeException ();
72 vm.conn.Array_SetValues (id, index, new ValueImpl [] { vm.EncodeValue (value) });
76 public IList<Value> GetValues (int index, int length) {
77 // FIXME: Multiple dimensions
78 if (index < 0 || index > Length - length)
79 throw new IndexOutOfRangeException ();
80 return vm.DecodeValues (vm.conn.Array_GetValues (id, index, length));
83 public void SetValues (int index, Value[] values) {
84 if (values == null)
85 throw new ArgumentNullException ("values");
86 // FIXME: Multiple dimensions
87 if (index < 0 || index > Length - values.Length)
88 throw new IndexOutOfRangeException ();
89 vm.conn.Array_SetValues (id, index, vm.EncodeValues (values));
92 IEnumerator IEnumerable.GetEnumerator ()
94 return new SimpleEnumerator (this);
97 internal class SimpleEnumerator : IEnumerator, ICloneable
99 ArrayMirror arr;
100 int pos, length;
102 public SimpleEnumerator (ArrayMirror arr)
104 this.arr = arr;
105 this.pos = -1;
106 this.length = arr.Length;
109 public object Current {
110 get {
111 if (pos < 0 )
112 throw new InvalidOperationException ("Enumeration has not started.");
113 if (pos >= length)
114 throw new InvalidOperationException ("Enumeration has already ended");
115 return arr [pos];
119 public bool MoveNext()
121 if (pos < length)
122 pos++;
123 if(pos < length)
124 return true;
125 else
126 return false;
129 public void Reset()
131 pos = -1;
134 public object Clone ()
136 return MemberwiseClone ();