SynchronizedCollection.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // SynchronizedCollection.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  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. namespace System.Collections.Generic
  34. {
  35. #if MOONLIGHT
  36. public class SynchronizedCollection<T> : Collection<T>
  37. {
  38. public object SyncRoot;
  39. public SynchronizedCollection ()
  40. {
  41. SyncRoot = new object ();
  42. }
  43. internal SynchronizedCollection (object syncRoot)
  44. {
  45. SyncRoot = syncRoot;
  46. }
  47. }
  48. #else
  49. [ComVisibleAttribute (false)]
  50. public class SynchronizedCollection<T> : IList<T>, ICollection<T>,
  51. IEnumerable<T>, IList, ICollection, IEnumerable
  52. {
  53. object root;
  54. List<T> list;
  55. public SynchronizedCollection ()
  56. : this (new object (), null, false)
  57. {
  58. }
  59. public SynchronizedCollection (object syncRoot)
  60. : this (syncRoot, null, false)
  61. {
  62. }
  63. public SynchronizedCollection (object syncRoot,
  64. IEnumerable<T> list)
  65. : this (syncRoot, new List<T> (list), false)
  66. {
  67. }
  68. public SynchronizedCollection (object syncRoot,
  69. params T [] list)
  70. : this (syncRoot, new List<T> (list), false)
  71. {
  72. }
  73. public SynchronizedCollection (object syncRoot,
  74. List<T> list, bool makeCopy)
  75. {
  76. if (syncRoot == null)
  77. syncRoot = new object ();
  78. root = syncRoot;
  79. if (list == null)
  80. this.list = new List<T> ();
  81. else if (makeCopy)
  82. this.list = new List<T> (list);
  83. else
  84. this.list = list;
  85. }
  86. public int Count {
  87. get {
  88. lock (root) {
  89. return list.Count;
  90. }
  91. }
  92. }
  93. public T this [int index] {
  94. get {
  95. lock (root) {
  96. return list [index];
  97. }
  98. }
  99. set {
  100. SetItem (index, value);
  101. }
  102. }
  103. public object SyncRoot {
  104. get { return root; }
  105. }
  106. protected List<T> Items {
  107. get { return list; }
  108. }
  109. public void Add (T item)
  110. {
  111. InsertItem (list.Count, item);
  112. }
  113. public void Clear ()
  114. {
  115. ClearItems ();
  116. }
  117. public bool Contains (T item)
  118. {
  119. lock (root) {
  120. return list.Contains (item);
  121. }
  122. }
  123. public void CopyTo (T [] array, int index)
  124. {
  125. lock (root) {
  126. list.CopyTo (array, index);
  127. }
  128. }
  129. [MonoTODO ("Should be synchronized enumerator?")]
  130. public IEnumerator<T> GetEnumerator ()
  131. {
  132. lock (root) {
  133. return list.GetEnumerator ();
  134. }
  135. }
  136. public int IndexOf (T item)
  137. {
  138. lock (root) {
  139. return list.IndexOf (item);
  140. }
  141. }
  142. public void Insert (int index, T item)
  143. {
  144. InsertItem (index, item);
  145. }
  146. [MonoTODO ("should we lock and remove item without invoking RemoveItem() instead?")]
  147. public bool Remove (T item)
  148. {
  149. int index = IndexOf (item);
  150. if (index < 0)
  151. return false;
  152. RemoveAt (index);
  153. return true;
  154. }
  155. public void RemoveAt (int index)
  156. {
  157. RemoveItem (index);
  158. }
  159. protected virtual void ClearItems ()
  160. {
  161. lock (root) {
  162. list.Clear ();
  163. }
  164. }
  165. protected virtual void InsertItem (int index, T item)
  166. {
  167. lock (root) {
  168. list.Insert (index, item);
  169. }
  170. }
  171. protected virtual void RemoveItem (int index)
  172. {
  173. lock (root) {
  174. list.RemoveAt (index);
  175. }
  176. }
  177. protected virtual void SetItem (int index, T item)
  178. {
  179. lock (root) {
  180. list [index] = item;
  181. }
  182. }
  183. #region Explicit interface implementations
  184. void ICollection.CopyTo (Array array, int index)
  185. {
  186. CopyTo ((T []) array, index);
  187. }
  188. IEnumerator IEnumerable.GetEnumerator ()
  189. {
  190. return GetEnumerator ();
  191. }
  192. int IList.Add (object value)
  193. {
  194. lock (root) {
  195. Add ((T) value);
  196. return list.Count - 1;
  197. }
  198. }
  199. bool IList.Contains (object value)
  200. {
  201. return Contains ((T) value);
  202. }
  203. int IList.IndexOf (object value)
  204. {
  205. return IndexOf ((T) value);
  206. }
  207. void IList.Insert (int index, object value)
  208. {
  209. Insert (index, (T) value);
  210. }
  211. void IList.Remove (object value)
  212. {
  213. Remove ((T) value);
  214. }
  215. bool ICollection<T>.IsReadOnly {
  216. get { return false; }
  217. }
  218. bool ICollection.IsSynchronized {
  219. get { return true; }
  220. }
  221. object ICollection.SyncRoot {
  222. get { return root; }
  223. }
  224. bool IList.IsFixedSize {
  225. get { return false; }
  226. }
  227. bool IList.IsReadOnly {
  228. get { return false; }
  229. }
  230. object IList.this [int index] {
  231. get { return this [index]; }
  232. set { this [index] = (T) value; }
  233. }
  234. #endregion
  235. }
  236. #endif
  237. }