[mono-config] use right type for result of strlen
[mono.git] / mcs / class / System.ServiceModel / System.Collections.Generic / SynchronizedCollection.cs
1 //
2 // SynchronizedCollection.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.Collections;
30 using System.Collections.Generic;
31 using System.Collections.ObjectModel;
32 using System.Runtime.InteropServices;
33
34 namespace System.Collections.Generic
35 {
36         [ComVisibleAttribute (false)] 
37         public class SynchronizedCollection<T> : IList<T>, ICollection<T>, 
38                 IEnumerable<T>, IList, ICollection, IEnumerable
39         {
40                 object root;
41                 List<T> list;
42
43                 public SynchronizedCollection ()
44                         : this (new object (), null, false)
45                 {
46                 }
47
48                 public SynchronizedCollection (object syncRoot)
49                         : this (syncRoot, null, false)
50                 {
51                 }
52
53                 public SynchronizedCollection (object syncRoot,
54                         IEnumerable<T> list)
55                         : this (syncRoot, new List<T> (list), false)
56                 {
57                 }
58
59                 public SynchronizedCollection (object syncRoot,
60                         params T [] list)
61                         : this (syncRoot, new List<T> (list), false)
62                 {
63                 }
64
65                 public SynchronizedCollection (object syncRoot,
66                         List<T> list, bool makeCopy)
67                 {
68                         if (syncRoot == null)
69                                 syncRoot = new object ();
70                         root = syncRoot;
71                         if (list == null)
72                                 this.list = new List<T> ();
73                         else if (makeCopy)
74                                 this.list = new List<T> (list);
75                         else
76                                 this.list = list;
77                 }
78
79                 public int Count {
80                         get {
81                                 lock (root) {
82                                         return list.Count;
83                                 }
84                         }
85                 }
86
87                 public T this [int index] {
88                         get {
89                                 lock (root) {
90                                         return list [index];
91                                 }
92                         }
93                         set {
94                                 SetItem (index, value);
95                         }
96                 }
97
98                 public object SyncRoot {
99                         get { return root; }
100                 }
101
102                 protected List<T> Items {
103                         get { return list; }
104                 }
105
106                 public void Add (T item)
107                 {
108                         InsertItem (list.Count, item);
109                 }
110
111                 public void Clear ()
112                 {
113                         ClearItems ();
114                 }
115
116                 public bool Contains (T item)
117                 {
118                         lock (root) {
119                                 return list.Contains (item);
120                         }
121                 }
122
123                 public void CopyTo (T [] array, int index)
124                 {
125                         lock (root) {
126                                 list.CopyTo (array, index);
127                         }
128                 }
129
130                 [MonoTODO ("Should be synchronized enumerator?")]
131                 public IEnumerator<T> GetEnumerator ()
132                 {
133                         lock (root) {
134                                 return list.GetEnumerator ();
135                         }
136                 }
137
138                 public int IndexOf (T item)
139                 {
140                         lock (root) {
141                                 return list.IndexOf (item);
142                         }
143                 }
144
145                 public void Insert (int index, T item)
146                 {
147                         InsertItem (index, item);
148                 }
149
150                 [MonoTODO ("should we lock and remove item without invoking RemoveItem() instead?")]
151                 public bool Remove (T item)
152                 {
153                         int index = IndexOf (item);
154                         if (index < 0)
155                                 return false;
156                         RemoveAt (index);
157                         return true;
158                 }
159
160                 public void RemoveAt (int index)
161                 {
162                         RemoveItem (index);
163                 }
164
165                 protected virtual void ClearItems ()
166                 {
167                         lock (root) {
168                                 list.Clear ();
169                         }
170                 }
171
172                 protected virtual void InsertItem (int index, T item)
173                 {
174                         lock (root) {
175                                 list.Insert (index, item);
176                         }
177                 }
178
179                 protected virtual void RemoveItem (int index)
180                 {
181                         lock (root) {
182                                 list.RemoveAt (index);
183                         }
184                 }
185
186                 protected virtual void SetItem (int index, T item)
187                 {
188                         lock (root) {
189                                 list [index] = item;
190                         }
191                 }
192
193                 #region Explicit interface implementations
194
195                 void ICollection.CopyTo (Array array, int index)
196                 {
197                         CopyTo ((T []) array, index);
198                 }
199
200                 IEnumerator IEnumerable.GetEnumerator ()
201                 {
202                         return GetEnumerator ();
203                 }
204
205                 int IList.Add (object value)
206                 {
207                         lock (root) {
208                                 Add ((T) value);
209                                 return list.Count - 1;
210                         }
211                 }
212
213                 bool IList.Contains (object value)
214                 {
215                         return Contains ((T) value);
216                 }
217
218                 int IList.IndexOf (object value)
219                 {
220                         return IndexOf ((T) value);
221                 }
222
223                 void IList.Insert (int index, object value)
224                 {
225                         Insert (index, (T) value);
226                 }
227
228                 void IList.Remove (object value)
229                 {
230                         Remove ((T) value);
231                 }
232
233                 bool ICollection<T>.IsReadOnly {
234                         get { return false; }
235                 }
236
237                 bool ICollection.IsSynchronized {
238                         get { return true; }
239                 }
240
241                 object ICollection.SyncRoot {
242                         get { return root; }
243                 }
244
245                 bool IList.IsFixedSize {
246                         get { return false; }
247                 }
248
249                 bool IList.IsReadOnly {
250                         get { return false; }
251                 }
252
253                 object IList.this [int index] {
254                         get { return this [index]; }
255                         set { this [index] = (T) value; }
256                 }
257
258                 #endregion
259         }
260 }