comparison Implab/Components/ComponentContainer.cs @ 153:b933ec88446e v2

docs
author cin
date Fri, 12 Feb 2016 00:59:29 +0300
parents 240aa6994018
children 1e082fb67a46
comparison
equal deleted inserted replaced
152:240aa6994018 153:b933ec88446e
2 using System.Collections.Generic; 2 using System.Collections.Generic;
3 using System.Linq; 3 using System.Linq;
4 4
5 namespace Implab.Components { 5 namespace Implab.Components {
6 /// <summary> 6 /// <summary>
7 /// Component container. 7 /// Component container, used to store track components in multi-threaded environmment.
8 /// </summary> 8 /// </summary>
9 /// <remarks>Instanses of this class are thread safe.</remarks> 9 /// <remarks>Instanses of this class are thread safe.</remarks>
10 public class ComponentContainer<T> : Disposable, ICollection<T> { 10 public class ComponentContainer<T> : Disposable, ICollection<T> {
11 readonly HashSet<T> m_components = new HashSet<T>(); 11 readonly HashSet<T> m_components = new HashSet<T>();
12 12
13 /// <summary>
14 /// Removes currently stored compoenents from the container and disposes them if possible.
15 /// </summary>
16 /// <remarks>
17 /// A new components may be added before this method completes.
18 /// </remarks>
13 public void Clear() { 19 public void Clear() {
14 T[] removed; 20 T[] removed;
15 21
16 lock (m_components) { 22 lock (m_components) {
17 removed = new T[m_components.Count]; 23 removed = new T[m_components.Count];
21 27
22 foreach (var item in removed.OfType<IDisposable>()) 28 foreach (var item in removed.OfType<IDisposable>())
23 item.Dispose(); 29 item.Dispose();
24 } 30 }
25 31
32 /// <summary>
33 /// Checks whether the specified item in the collection.
34 /// </summary>
35 /// <param name="item">The item to check.</param>
26 public bool Contains(T item) { 36 public bool Contains(T item) {
27 lock (m_components) 37 lock (m_components)
28 return m_components.Contains(item); 38 return m_components.Contains(item);
29 } 39 }
30 40
41 /// <summary>
42 /// Copies currently stored components to the specified array.
43 /// </summary>
44 /// <param name="array">A destination array for components.</param>
45 /// <param name="arrayIndex">A starting index in the destination array.</param>
31 public void CopyTo(T[] array, int arrayIndex) { 46 public void CopyTo(T[] array, int arrayIndex) {
32 lock (m_components) 47 lock (m_components)
33 m_components.CopyTo(array, arrayIndex); 48 m_components.CopyTo(array, arrayIndex);
34 } 49 }
35 50
51 /// <summary>
52 /// Remove the specified item from the collection.
53 /// </summary>
54 /// <param name="item">The item to remove.</param>
36 public bool Remove(T item) { 55 public bool Remove(T item) {
37 lock (m_components) 56 lock (m_components)
38 return m_components.Remove(item); 57 return m_components.Remove(item);
39 } 58 }
40 59
60 /// <summary>
61 /// Gets the count of components in the collection.
62 /// </summary>
41 public int Count { 63 public int Count {
42 get { 64 get {
43 lock (m_components) 65 lock (m_components)
44 return m_components.Count; 66 return m_components.Count;
45 } 67 }
46 } 68 }
47 69
70 /// <summary>
71 /// Gets a value indicating whether this instance is read only.
72 /// </summary>
73 /// <remarks>
74 /// Always false.
75 /// </remarks>
48 public bool IsReadOnly { 76 public bool IsReadOnly {
49 get { 77 get {
50 return false; 78 return false;
51 } 79 }
52 } 80 }
53 81
82 /// <summary>
83 /// Gets the enumerator for components in the collection.
84 /// </summary>
85 /// <returns>The enumerator.</returns>
54 public IEnumerator<T> GetEnumerator() { 86 public IEnumerator<T> GetEnumerator() {
55 T[] items; 87 T[] items;
56 lock (m_components) { 88 lock (m_components) {
57 items = new T[m_components.Count]; 89 items = new T[m_components.Count];
58 m_components.CopyTo(items); 90 m_components.CopyTo(items);
62 94
63 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { 95 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
64 return GetEnumerator(); 96 return GetEnumerator();
65 } 97 }
66 98
99 /// <summary>
100 /// Add the specified item to the collection.
101 /// </summary>
102 /// <param name="item">The item to add.</param>
103 /// <remarks>
104 /// If the collection is alredy disposed, the item isn't added to the collection and disposed if possible.
105 /// </remarks>
67 public void Add(T item) { 106 public void Add(T item) {
68 Safe.ArgumentNotNull(item, "item"); 107 Safe.ArgumentNotNull(item, "item");
69 108
70 lock (m_components) { 109 lock (m_components) {
71 if (IsDisposed) 110 if (IsDisposed)
73 else 112 else
74 m_components.Add(item); 113 m_components.Add(item);
75 } 114 }
76 } 115 }
77 116
117 /// <summary>
118 /// Disposes the components stored in the collection.
119 /// </summary>
120 /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param>
78 protected override void Dispose(bool disposing) { 121 protected override void Dispose(bool disposing) {
79 base.Dispose(disposing); 122 base.Dispose(disposing);
80 Clear(); 123 Clear();
81 } 124 }
82 } 125 }