Merge branch 'master' into msbuilddll2
[mono.git] / mcs / class / Mono.Debugger.Soft / Mono.Debugger.Soft / ArrayMirror.cs
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4
5 namespace Mono.Debugger.Soft
6 {
7         public class ArrayMirror : ObjectMirror, IEnumerable {
8
9                 public int[] lengths;
10                 public int[] lower_bounds;
11                 public int rank;
12
13                 internal ArrayMirror (VirtualMachine vm, long id) : base (vm, id) {
14                 }
15
16                 internal ArrayMirror (VirtualMachine vm, long id, TypeMirror type, AppDomainMirror domain) : base (vm, id, type, domain) {
17                 }
18
19                 public int Length {
20                         get {
21                                 GetLengths ();
22
23                                 int length = lengths [0];
24
25                                 for (int i = 1; i < Rank; i++) {
26                                         length *= lengths [i];
27                                 }
28
29                                 return length;
30                         }
31                 }
32
33                 public int Rank {
34                         get {
35                                 GetLengths ();
36
37                                 return rank;
38                         }
39                 }
40
41                 public int GetLength (int dimension) {
42                         GetLengths ();
43
44                         if (dimension < 0 || dimension >= Rank)
45                                 throw new ArgumentOutOfRangeException ("dimension");
46
47                         return lengths [dimension];
48                 }
49
50                 public int GetLowerBound (int dimension) {
51                         GetLengths ();
52
53                         if (dimension < 0 || dimension >= Rank)
54                                 throw new ArgumentOutOfRangeException ("dimension");
55
56                         return lower_bounds [dimension];
57                 }
58
59                 void GetLengths () {
60                         if (lengths == null)
61                                 lengths = vm.conn.Array_GetLength (id, out this.rank, out this.lower_bounds);
62                 }
63
64                 public Value this [int index] {
65                         get {
66                                 // FIXME: Multiple dimensions
67                                 if (index < 0 || index > Length - 1)
68                                         throw new IndexOutOfRangeException ();
69                                 return vm.DecodeValue (vm.conn.Array_GetValues (id, index, 1) [0]);
70                         }
71                         set {
72                                 // FIXME: Multiple dimensions
73                                 if (index < 0 || index > Length - 1)
74                                         throw new IndexOutOfRangeException ();
75                                 vm.conn.Array_SetValues (id, index, new ValueImpl [] { vm.EncodeValue (value) });
76                         }
77                 }
78
79                 public IList<Value> GetValues (int index, int length) {
80                         // FIXME: Multiple dimensions
81                                 if (index < 0 || index > Length - length)
82                                         throw new IndexOutOfRangeException ();
83                         return vm.DecodeValues (vm.conn.Array_GetValues (id, index, length));
84                 }
85
86                 public void SetValues (int index, Value[] values) {
87                         if (values == null)
88                                 throw new ArgumentNullException ("values");
89                         // FIXME: Multiple dimensions
90                         if (index < 0 || index > Length - values.Length)
91                                 throw new IndexOutOfRangeException ();
92                         vm.conn.Array_SetValues (id, index, vm.EncodeValues (values));
93                 }
94
95                 IEnumerator IEnumerable.GetEnumerator ()
96                 {
97                         return new SimpleEnumerator (this);
98                 }
99
100                 internal class SimpleEnumerator : IEnumerator, ICloneable
101                 {
102                         ArrayMirror arr;
103                         int pos, length;
104
105                         public SimpleEnumerator (ArrayMirror arr)
106                         {
107                                 this.arr = arr;
108                                 this.pos = -1;
109                                 this.length = arr.Length;
110                         }
111
112                         public object Current {
113                                 get {
114                                         if (pos < 0 )
115                                                 throw new InvalidOperationException ("Enumeration has not started.");
116                                         if  (pos >= length)
117                                                 throw new InvalidOperationException ("Enumeration has already ended");
118                                         return arr [pos];
119                                 }
120                         }
121
122                         public bool MoveNext()
123                         {
124                                 if (pos < length)
125                                         pos++;
126                                 if(pos < length)
127                                         return true;
128                                 else
129                                         return false;
130                         }
131
132                         public void Reset()
133                         {
134                                 pos = -1;
135                         }
136
137                         public object Clone ()
138                         {
139                                 return MemberwiseClone ();
140                         }
141                 }
142         }
143 }