152
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4
|
|
5 namespace Implab.Components {
|
|
6 /// <summary>
|
153
|
7 /// Component container, used to store track components in multi-threaded environmment.
|
152
|
8 /// </summary>
|
|
9 /// <remarks>Instanses of this class are thread safe.</remarks>
|
|
10 public class ComponentContainer<T> : Disposable, ICollection<T> {
|
|
11 readonly HashSet<T> m_components = new HashSet<T>();
|
|
12
|
153
|
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>
|
152
|
19 public void Clear() {
|
|
20 T[] removed;
|
|
21
|
|
22 lock (m_components) {
|
|
23 removed = new T[m_components.Count];
|
|
24 m_components.CopyTo(removed);
|
|
25 m_components.Clear();
|
|
26 }
|
|
27
|
|
28 foreach (var item in removed.OfType<IDisposable>())
|
|
29 item.Dispose();
|
|
30 }
|
|
31
|
153
|
32 /// <summary>
|
|
33 /// Checks whether the specified item in the collection.
|
|
34 /// </summary>
|
|
35 /// <param name="item">The item to check.</param>
|
152
|
36 public bool Contains(T item) {
|
|
37 lock (m_components)
|
|
38 return m_components.Contains(item);
|
|
39 }
|
|
40
|
153
|
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>
|
152
|
46 public void CopyTo(T[] array, int arrayIndex) {
|
|
47 lock (m_components)
|
|
48 m_components.CopyTo(array, arrayIndex);
|
|
49 }
|
|
50
|
153
|
51 /// <summary>
|
|
52 /// Remove the specified item from the collection.
|
|
53 /// </summary>
|
|
54 /// <param name="item">The item to remove.</param>
|
152
|
55 public bool Remove(T item) {
|
|
56 lock (m_components)
|
|
57 return m_components.Remove(item);
|
|
58 }
|
|
59
|
153
|
60 /// <summary>
|
|
61 /// Gets the count of components in the collection.
|
|
62 /// </summary>
|
152
|
63 public int Count {
|
|
64 get {
|
|
65 lock (m_components)
|
|
66 return m_components.Count;
|
|
67 }
|
|
68 }
|
|
69
|
153
|
70 /// <summary>
|
|
71 /// Gets a value indicating whether this instance is read only.
|
|
72 /// </summary>
|
|
73 /// <remarks>
|
|
74 /// Always false.
|
|
75 /// </remarks>
|
152
|
76 public bool IsReadOnly {
|
|
77 get {
|
|
78 return false;
|
|
79 }
|
|
80 }
|
|
81
|
153
|
82 /// <summary>
|
|
83 /// Gets the enumerator for components in the collection.
|
|
84 /// </summary>
|
|
85 /// <returns>The enumerator.</returns>
|
152
|
86 public IEnumerator<T> GetEnumerator() {
|
|
87 T[] items;
|
|
88 lock (m_components) {
|
|
89 items = new T[m_components.Count];
|
|
90 m_components.CopyTo(items);
|
|
91 return (IEnumerator<T>)items.GetEnumerator();
|
|
92 }
|
|
93 }
|
|
94
|
|
95 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
|
|
96 return GetEnumerator();
|
|
97 }
|
|
98
|
153
|
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>
|
152
|
106 public void Add(T item) {
|
|
107 Safe.ArgumentNotNull(item, "item");
|
|
108
|
|
109 lock (m_components) {
|
|
110 if (IsDisposed)
|
|
111 Safe.Dispose(item);
|
|
112 else
|
|
113 m_components.Add(item);
|
|
114 }
|
|
115 }
|
|
116
|
153
|
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>
|
152
|
121 protected override void Dispose(bool disposing) {
|
|
122 base.Dispose(disposing);
|
|
123 Clear();
|
|
124 }
|
|
125 }
|
|
126 }
|
|
127
|