##// END OF EJS Templates
Fixed component container
cin -
r216:1e082fb67a46 v2
parent child
Show More
@@ -8,7 +8,8 namespace Implab.Components {
8 8 /// </summary>
9 9 /// <remarks>Instanses of this class are thread safe.</remarks>
10 10 public class ComponentContainer<T> : Disposable, ICollection<T> {
11 readonly HashSet<T> m_components = new HashSet<T>();
11 List<T> m_components = new List<T>();
12 readonly object m_lock = new object();
12 13
13 14 /// <summary>
14 15 /// Removes currently stored compoenents from the container and disposes them if possible.
@@ -17,12 +18,11 namespace Implab.Components {
17 18 /// A new components may be added before this method completes.
18 19 /// </remarks>
19 20 public void Clear() {
20 T[] removed;
21 List<T> removed;
21 22
22 lock (m_components) {
23 removed = new T[m_components.Count];
24 m_components.CopyTo(removed);
25 m_components.Clear();
23 lock (m_lock) {
24 removed = m_components;
25 m_components = new List<T>();
26 26 }
27 27
28 28 foreach (var item in removed.OfType<IDisposable>())
@@ -34,7 +34,7 namespace Implab.Components {
34 34 /// </summary>
35 35 /// <param name="item">The item to check.</param>
36 36 public bool Contains(T item) {
37 lock (m_components)
37 lock (m_lock)
38 38 return m_components.Contains(item);
39 39 }
40 40
@@ -44,7 +44,7 namespace Implab.Components {
44 44 /// <param name="array">A destination array for components.</param>
45 45 /// <param name="arrayIndex">A starting index in the destination array.</param>
46 46 public void CopyTo(T[] array, int arrayIndex) {
47 lock (m_components)
47 lock (m_lock)
48 48 m_components.CopyTo(array, arrayIndex);
49 49 }
50 50
@@ -53,7 +53,7 namespace Implab.Components {
53 53 /// </summary>
54 54 /// <param name="item">The item to remove.</param>
55 55 public bool Remove(T item) {
56 lock (m_components)
56 lock (m_lock)
57 57 return m_components.Remove(item);
58 58 }
59 59
@@ -62,7 +62,7 namespace Implab.Components {
62 62 /// </summary>
63 63 public int Count {
64 64 get {
65 lock (m_components)
65 lock (m_lock)
66 66 return m_components.Count;
67 67 }
68 68 }
@@ -84,13 +84,12 namespace Implab.Components {
84 84 /// </summary>
85 85 /// <returns>The enumerator.</returns>
86 86 public IEnumerator<T> GetEnumerator() {
87 T[] items;
88 lock (m_components) {
89 items = new T[m_components.Count];
87 T[] items = new T[m_components.Count];
88 lock (m_lock) {
90 89 m_components.CopyTo(items);
90 }
91 91 return (IEnumerator<T>)items.GetEnumerator();
92 92 }
93 }
94 93
95 94 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
96 95 return GetEnumerator();
@@ -105,13 +104,15 namespace Implab.Components {
105 104 /// </remarks>
106 105 public void Add(T item) {
107 106 Safe.ArgumentNotNull(item, "item");
108
109 lock (m_components) {
107 bool dispose = false;
108 lock (m_lock) {
110 109 if (IsDisposed)
111 Safe.Dispose(item);
110 dispose = true;
112 111 else
113 112 m_components.Add(item);
114 113 }
114 if (dispose)
115 Safe.Dispose(item);
115 116 }
116 117
117 118 /// <summary>
@@ -119,8 +120,10 namespace Implab.Components {
119 120 /// </summary>
120 121 /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param>
121 122 protected override void Dispose(bool disposing) {
123 if (disposing)
124 Clear();
125
122 126 base.Dispose(disposing);
123 Clear();
124 127 }
125 128 }
126 129 }
General Comments 3
Under Review
author

Auto status change to "Under Review"

Approved
author

ok, latest stable version should be in default

You need to be logged in to leave comments. Login now