PageRenderTime 29ms CodeModel.GetById 13ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/IronPython_Main/Languages/IronPython/IronPython/Runtime/ConversionWrappers.cs

#
C# | 280 lines | 194 code | 72 blank | 14 comment | 2 complexity | 9e7a7e246337e3b8c5eacd8fb62012c2 MD5 | raw file
  1/* ****************************************************************************
  2 *
  3 * Copyright (c) Microsoft Corporation. 
  4 *
  5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A 
  6 * copy of the license can be found in the License.html file at the root of this distribution. If 
  7 * you cannot locate the  Apache License, Version 2.0, please send an email to 
  8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
  9 * by the terms of the Apache License, Version 2.0.
 10 *
 11 * You must not remove this notice, or any other, from this software.
 12 *
 13 *
 14 * ***************************************************************************/
 15
 16using System.Collections;
 17using System.Collections.Generic;
 18
 19namespace IronPython.Runtime {
 20
 21    public class ListGenericWrapper<T> : IList<T> {
 22        private IList<object> _value;
 23
 24        public ListGenericWrapper(IList<object> value) { this._value = value; }
 25
 26        #region IList<T> Members
 27
 28        public int IndexOf(T item) {
 29            return _value.IndexOf(item);
 30        }
 31
 32        public void Insert(int index, T item) {
 33            _value.Insert(index, item);
 34        }
 35
 36        public void RemoveAt(int index) {
 37            _value.RemoveAt(index);
 38        }
 39
 40        public T this[int index] {
 41            get {
 42                return (T)_value[index];
 43            }
 44            set {
 45                this._value[index] = value;
 46            }
 47        }
 48
 49        #endregion
 50
 51        #region ICollection<T> Members
 52
 53        public void Add(T item) {
 54            _value.Add(item);
 55        }
 56
 57        public void Clear() {
 58            _value.Clear();
 59        }
 60
 61        public bool Contains(T item) {
 62            return _value.Contains(item);
 63        }
 64
 65        public void CopyTo(T[] array, int arrayIndex) {
 66            for (int i = 0; i < _value.Count; i++) {
 67                array[arrayIndex + i] = (T)_value[i];
 68            }
 69        }
 70
 71        public int Count {
 72            get { return _value.Count; }
 73        }
 74
 75        public bool IsReadOnly {
 76            get { return _value.IsReadOnly; }
 77        }
 78
 79        public bool Remove(T item) {
 80            return _value.Remove(item);
 81        }
 82
 83        #endregion
 84
 85        #region IEnumerable<T> Members
 86
 87        public IEnumerator<T> GetEnumerator() {
 88            return new IEnumeratorOfTWrapper<T>(_value.GetEnumerator());
 89        }
 90
 91        #endregion
 92
 93        #region IEnumerable Members
 94
 95        IEnumerator IEnumerable.GetEnumerator() {
 96            return _value.GetEnumerator();
 97        }
 98
 99        #endregion
100    }
101
102
103    public class DictionaryGenericWrapper<K, V> : IDictionary<K, V> {
104        private IDictionary<object, object> self;
105
106        public DictionaryGenericWrapper(IDictionary<object, object> self) {
107            this.self = self;
108        }
109
110        #region IDictionary<K,V> Members
111
112        public void Add(K key, V value) {
113            self.Add(key, value);
114        }
115
116        public bool ContainsKey(K key) {
117            return self.ContainsKey(key);
118        }
119
120        public ICollection<K> Keys {
121            get {
122                List<K> res = new List<K>();
123                foreach (object o in self.Keys) {
124                    res.Add((K)o);
125                }
126                return res;
127            }
128        }
129
130        public bool Remove(K key) {
131            return self.Remove(key);
132        }
133
134        public bool TryGetValue(K key, out V value) {
135            object outValue;
136            if (self.TryGetValue(key, out outValue)) {
137                value = (V)outValue;
138                return true;
139            }
140            value = default(V);
141            return false;
142        }
143
144        public ICollection<V> Values {
145            get {
146                List<V> res = new List<V>();
147                foreach (object o in self.Values) {
148                    res.Add((V)o);
149                }
150                return res;
151            }
152        }
153
154        public V this[K key] {
155            get {
156                return (V)self[key];
157            }
158            set {
159                self[key] = value;
160            }
161        }
162
163        #endregion
164
165        #region ICollection<KeyValuePair<K,V>> Members
166
167        public void Add(KeyValuePair<K, V> item) {
168            self.Add(new KeyValuePair<object, object>(item.Key, item.Value));
169        }
170
171        public void Clear() {
172            self.Clear();
173        }
174
175        public bool Contains(KeyValuePair<K, V> item) {
176            return self.Contains(new KeyValuePair<object, object>(item.Key, item.Value));
177        }
178
179        public void CopyTo(KeyValuePair<K, V>[] array, int arrayIndex) {
180            foreach (KeyValuePair<K, V> kvp in this) {
181                array[arrayIndex++] = kvp;
182            }
183        }
184
185        public int Count {
186            get { return self.Count; }
187        }
188
189        public bool IsReadOnly {
190            get { return self.IsReadOnly; }
191        }
192
193        public bool Remove(KeyValuePair<K, V> item) {
194            return self.Remove(new KeyValuePair<object, object>(item.Key, item.Value));
195        }
196
197        #endregion
198
199        #region IEnumerable<KeyValuePair<K,V>> Members
200
201        public IEnumerator<KeyValuePair<K, V>> GetEnumerator() {
202            foreach (KeyValuePair<object, object> kv in self) {
203                yield return new KeyValuePair<K, V>((K)kv.Key, (V)kv.Value);
204            }
205        }
206
207        #endregion
208
209        #region IEnumerable Members
210
211        IEnumerator IEnumerable.GetEnumerator() {
212            return self.GetEnumerator();
213        }
214
215        #endregion
216    }
217
218    public class IEnumeratorOfTWrapper<T> : IEnumerator<T> {
219        IEnumerator enumerable;
220        public IEnumeratorOfTWrapper(IEnumerator enumerable) {
221            this.enumerable = enumerable;
222        }
223
224        #region IEnumerator<T> Members
225
226        public T Current {
227            get { return (T)enumerable.Current; }
228        }
229
230        #endregion
231
232        #region IDisposable Members
233
234        public void Dispose() {
235        }
236
237        #endregion
238
239        #region IEnumerator Members
240
241        object IEnumerator.Current {
242            get { return enumerable.Current; }
243        }
244
245        public bool MoveNext() {
246            return enumerable.MoveNext();
247        }
248
249        public void Reset() {
250            enumerable.Reset();
251        }
252
253        #endregion
254    }
255
256    [PythonType("enumerable_wrapper")]
257    public class IEnumerableOfTWrapper<T> : IEnumerable<T>, IEnumerable {
258        IEnumerable enumerable;
259
260        public IEnumerableOfTWrapper(IEnumerable enumerable) {
261            this.enumerable = enumerable;
262        }
263
264        #region IEnumerable<T> Members
265
266        public IEnumerator<T> GetEnumerator() {
267            return new IEnumeratorOfTWrapper<T>(enumerable.GetEnumerator());
268        }
269
270        #endregion
271
272        #region IEnumerable Members
273
274        IEnumerator IEnumerable.GetEnumerator() {
275            return GetEnumerator();
276        }
277
278        #endregion
279    }
280}