1 | #region MIT License |
---|
2 | /* |
---|
3 | * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/ |
---|
4 | * |
---|
5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
6 | * of this software and associated documentation files (the "Software"), to deal |
---|
7 | * in the Software without restriction, including without limitation the rights to |
---|
8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
---|
9 | * the Software, and to permit persons to whom the Software is furnished to do so, |
---|
10 | * subject to the following conditions: |
---|
11 | * |
---|
12 | * The above copyright notice and this permission notice shall be |
---|
13 | * included in all copies or substantial portions of the Software. |
---|
14 | * |
---|
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
16 | * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
---|
17 | * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
---|
18 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
---|
20 | * OTHER DEALINGS IN THE SOFTWARE. |
---|
21 | */ |
---|
22 | #endregion |
---|
23 | |
---|
24 | |
---|
25 | |
---|
26 | using System; |
---|
27 | using System.Collections.ObjectModel; |
---|
28 | using System.Collections.Generic; |
---|
29 | using System.Threading; |
---|
30 | namespace Physics2DDotNet.Collections |
---|
31 | { |
---|
32 | |
---|
33 | public class ImplicitCastCollection<TBase, TParent> : IList<TBase> |
---|
34 | where TParent : TBase |
---|
35 | { |
---|
36 | private struct ImplicitCastEnumerator : IEnumerator<TBase> |
---|
37 | { |
---|
38 | IEnumerator<TParent> self; |
---|
39 | public ImplicitCastEnumerator(IList<TParent> parent) |
---|
40 | { |
---|
41 | this.self = parent.GetEnumerator(); |
---|
42 | } |
---|
43 | public TBase Current |
---|
44 | { |
---|
45 | get { return self.Current; } |
---|
46 | } |
---|
47 | object System.Collections.IEnumerator.Current |
---|
48 | { |
---|
49 | get { return self.Current; } |
---|
50 | } |
---|
51 | public void Dispose() |
---|
52 | { |
---|
53 | self.Dispose(); |
---|
54 | } |
---|
55 | public bool MoveNext() |
---|
56 | { |
---|
57 | return self.MoveNext(); |
---|
58 | } |
---|
59 | public void Reset() |
---|
60 | { |
---|
61 | self.Reset(); |
---|
62 | } |
---|
63 | } |
---|
64 | private IList<TParent> self; |
---|
65 | public ImplicitCastCollection(IList<TParent> self) |
---|
66 | { |
---|
67 | this.self = self; |
---|
68 | } |
---|
69 | public int IndexOf(TBase item) |
---|
70 | { |
---|
71 | return self.IndexOf((TParent)item); |
---|
72 | } |
---|
73 | public void Insert(int index, TBase item) |
---|
74 | { |
---|
75 | self.Insert(index, (TParent)item); |
---|
76 | } |
---|
77 | public void RemoveAt(int index) |
---|
78 | { |
---|
79 | self.RemoveAt(index); |
---|
80 | } |
---|
81 | public TBase this[int index] |
---|
82 | { |
---|
83 | get |
---|
84 | { |
---|
85 | return self[index]; |
---|
86 | } |
---|
87 | set |
---|
88 | { |
---|
89 | self[index] = (TParent)value; |
---|
90 | } |
---|
91 | } |
---|
92 | public void Add(TBase item) |
---|
93 | { |
---|
94 | self.Add((TParent)item); |
---|
95 | } |
---|
96 | public void Clear() |
---|
97 | { |
---|
98 | self.Clear(); |
---|
99 | } |
---|
100 | public bool Contains(TBase item) |
---|
101 | { |
---|
102 | return self.Contains((TParent)item); |
---|
103 | } |
---|
104 | public void CopyTo(TBase[] array, int arrayIndex) |
---|
105 | { |
---|
106 | if (array == null) { throw new ArgumentNullException("array"); } |
---|
107 | if ((arrayIndex < 0)) { throw new ArgumentOutOfRangeException("arrayIndex"); } |
---|
108 | if ((array.Length - arrayIndex) < self.Count) { throw new ArgumentOutOfRangeException("arrayIndex"); } |
---|
109 | for (int index = 0; index < self.Count; ++index, ++arrayIndex) |
---|
110 | { |
---|
111 | array[arrayIndex] = self[index]; |
---|
112 | } |
---|
113 | } |
---|
114 | public int Count |
---|
115 | { |
---|
116 | get { return self.Count; } |
---|
117 | } |
---|
118 | public bool IsReadOnly |
---|
119 | { |
---|
120 | get { return self.IsReadOnly; } |
---|
121 | } |
---|
122 | public bool Remove(TBase item) |
---|
123 | { |
---|
124 | return self.Remove((TParent)item); |
---|
125 | } |
---|
126 | public IEnumerator<TBase> GetEnumerator() |
---|
127 | { |
---|
128 | return new ImplicitCastEnumerator(self); |
---|
129 | } |
---|
130 | System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
---|
131 | { |
---|
132 | return GetEnumerator(); |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|