PageRenderTime 47ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/2.x/Dorian/Gorgon/Gorgon.Common/Collections/Specialized/GorgonTrackedObjectCollection.cs

http://gorgonlib.googlecode.com/
C# | 242 lines | 114 code | 19 blank | 109 comment | 2 complexity | b4dfe8ef85fd70b10c77c0f29ed6a30a MD5 | raw file
Possible License(s): LGPL-2.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace GorgonLibrary.Collections.Specialized
  6. {
  7. /// <summary>
  8. /// A collection of custom objects that are managed by the another interface.
  9. /// </summary>
  10. /// <remarks>This collection is intended for use by objects that manage the lifetimes of any child objects created from it.</remarks>
  11. public class GorgonTrackedObjectCollection
  12. : IList<IDisposable>
  13. {
  14. #region Variables.
  15. private IList<IDisposable> _objects = null; // List of tracked objects.
  16. #endregion
  17. #region Methods.
  18. /// <summary>
  19. /// Function to clean up all objects in the collection.
  20. /// </summary>
  21. public void ReleaseAll()
  22. {
  23. var items = _objects.ToArray();
  24. foreach (var item in items)
  25. item.Dispose();
  26. _objects.Clear();
  27. }
  28. #endregion
  29. #region Constructor.
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="GorgonTrackedObjectCollection"/> class.
  32. /// </summary>
  33. public GorgonTrackedObjectCollection()
  34. {
  35. _objects = new List<IDisposable>();
  36. }
  37. #endregion
  38. #region IList<IDisposable> Members
  39. #region Properties.
  40. /// <summary>
  41. /// Gets or sets the element at the specified index.
  42. /// </summary>
  43. /// <returns>
  44. /// The element at the specified index.
  45. /// </returns>
  46. ///
  47. /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
  48. /// </exception>
  49. ///
  50. /// <exception cref="T:System.NotSupportedException">
  51. /// The property is set and the <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
  52. /// </exception>
  53. public IDisposable this[int index]
  54. {
  55. get
  56. {
  57. return _objects[index];
  58. }
  59. set
  60. {
  61. throw new NotSupportedException();
  62. }
  63. }
  64. #endregion
  65. #region Methods.
  66. /// <summary>
  67. /// Determines the index of a specific item in the <see cref="T:System.Collections.Generic.IList`1"/>.
  68. /// </summary>
  69. /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
  70. /// <returns>
  71. /// The index of <paramref name="item"/> if found in the list; otherwise, -1.
  72. /// </returns>
  73. public int IndexOf(IDisposable item)
  74. {
  75. return _objects.IndexOf(item);
  76. }
  77. /// <summary>
  78. /// Inserts an item to the <see cref="T:System.Collections.Generic.IList`1"/> at the specified index.
  79. /// </summary>
  80. /// <param name="index">The zero-based index at which <paramref name="item"/> should be inserted.</param>
  81. /// <param name="item">The object to insert into the <see cref="T:System.Collections.Generic.IList`1"/>.</param>
  82. /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
  83. /// </exception>
  84. ///
  85. /// <exception cref="T:System.NotSupportedException">
  86. /// The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
  87. /// </exception>
  88. void IList<IDisposable>.Insert(int index, IDisposable item)
  89. {
  90. throw new NotSupportedException();
  91. }
  92. /// <summary>
  93. /// Removes the <see cref="T:System.Collections.Generic.IList`1"/> item at the specified index.
  94. /// </summary>
  95. /// <param name="index">The zero-based index of the item to remove.</param>
  96. /// <exception cref="T:System.ArgumentOutOfRangeException"><paramref name="index"/> is not a valid index in the <see cref="T:System.Collections.Generic.IList`1"/>.
  97. /// </exception>
  98. ///
  99. /// <exception cref="T:System.NotSupportedException">
  100. /// The <see cref="T:System.Collections.Generic.IList`1"/> is read-only.
  101. /// </exception>
  102. void IList<IDisposable>.RemoveAt(int index)
  103. {
  104. throw new NotSupportedException();
  105. }
  106. #endregion
  107. #endregion
  108. #region ICollection<IDisposable> Members
  109. #region Properties.
  110. /// <summary>
  111. /// Gets the number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  112. /// </summary>
  113. /// <returns>
  114. /// The number of elements contained in the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  115. /// </returns>
  116. public int Count
  117. {
  118. get
  119. {
  120. return _objects.Count;
  121. }
  122. }
  123. /// <summary>
  124. /// Gets a value indicating whether the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  125. /// </summary>
  126. /// <returns>true if the <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only; otherwise, false.
  127. /// </returns>
  128. public bool IsReadOnly
  129. {
  130. get
  131. {
  132. return false;
  133. }
  134. }
  135. #endregion
  136. #region Methods.
  137. /// <summary>
  138. /// Adds an item to the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  139. /// </summary>
  140. /// <param name="item">The object to add to the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  141. /// <exception cref="T:System.NotSupportedException">
  142. /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  143. /// </exception>
  144. public void Add(IDisposable item)
  145. {
  146. if (!_objects.Contains(item))
  147. _objects.Add(item);
  148. }
  149. /// <summary>
  150. /// Removes all items from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  151. /// </summary>
  152. /// <exception cref="T:System.NotSupportedException">
  153. /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  154. /// </exception>
  155. void ICollection<IDisposable>.Clear()
  156. {
  157. throw new NotSupportedException();
  158. }
  159. /// <summary>
  160. /// Determines whether the <see cref="T:System.Collections.Generic.ICollection`1"/> contains a specific value.
  161. /// </summary>
  162. /// <param name="item">The object to locate in the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  163. /// <returns>
  164. /// true if <paramref name="item"/> is found in the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false.
  165. /// </returns>
  166. public bool Contains(IDisposable item)
  167. {
  168. return _objects.Contains(item);
  169. }
  170. /// <summary>
  171. /// Copies to.
  172. /// </summary>
  173. /// <param name="array">The array.</param>
  174. /// <param name="arrayIndex">Index of the array.</param>
  175. void ICollection<IDisposable>.CopyTo(IDisposable[] array, int arrayIndex)
  176. {
  177. throw new NotSupportedException();
  178. }
  179. /// <summary>
  180. /// Removes the first occurrence of a specific object from the <see cref="T:System.Collections.Generic.ICollection`1"/>.
  181. /// </summary>
  182. /// <param name="item">The object to remove from the <see cref="T:System.Collections.Generic.ICollection`1"/>.</param>
  183. /// <returns>
  184. /// true if <paramref name="item"/> was successfully removed from the <see cref="T:System.Collections.Generic.ICollection`1"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="T:System.Collections.Generic.ICollection`1"/>.
  185. /// </returns>
  186. /// <exception cref="T:System.NotSupportedException">
  187. /// The <see cref="T:System.Collections.Generic.ICollection`1"/> is read-only.
  188. /// </exception>
  189. public bool Remove(IDisposable item)
  190. {
  191. if (!_objects.Contains(item))
  192. return false;
  193. return _objects.Remove(item);
  194. }
  195. #endregion
  196. #endregion
  197. #region IEnumerable<IDisposable> Members
  198. /// <summary>
  199. /// Returns an enumerator that iterates through the collection.
  200. /// </summary>
  201. /// <returns>
  202. /// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
  203. /// </returns>
  204. public IEnumerator<IDisposable> GetEnumerator()
  205. {
  206. foreach (IDisposable item in _objects)
  207. yield return item;
  208. }
  209. #endregion
  210. #region IEnumerable Members
  211. /// <summary>
  212. /// Returns an enumerator that iterates through a collection.
  213. /// </summary>
  214. /// <returns>
  215. /// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
  216. /// </returns>
  217. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  218. {
  219. return GetEnumerator();
  220. }
  221. #endregion
  222. }
  223. }