##// END OF EJS Templates
Fixed component container
cin -
r216:1e082fb67a46 v2
parent child
Show More
@@ -1,127 +1,130
1 1 using System;
2 2 using System.Collections.Generic;
3 3 using System.Linq;
4 4
5 5 namespace Implab.Components {
6 6 /// <summary>
7 7 /// Component container, used to store track components in multi-threaded environmment.
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.
15 16 /// </summary>
16 17 /// <remarks>
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>())
29 29 item.Dispose();
30 30 }
31 31
32 32 /// <summary>
33 33 /// Checks whether the specified item in the collection.
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
41 41 /// <summary>
42 42 /// Copies currently stored components to the specified array.
43 43 /// </summary>
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
51 51 /// <summary>
52 52 /// Remove the specified item from the collection.
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
60 60 /// <summary>
61 61 /// Gets the count of components in the collection.
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 }
69 69
70 70 /// <summary>
71 71 /// Gets a value indicating whether this instance is read only.
72 72 /// </summary>
73 73 /// <remarks>
74 74 /// Always false.
75 75 /// </remarks>
76 76 public bool IsReadOnly {
77 77 get {
78 78 return false;
79 79 }
80 80 }
81 81
82 82 /// <summary>
83 83 /// Gets the enumerator for components in the collection.
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);
91 return (IEnumerator<T>)items.GetEnumerator();
92 90 }
91 return (IEnumerator<T>)items.GetEnumerator();
93 92 }
94 93
95 94 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
96 95 return GetEnumerator();
97 96 }
98 97
99 98 /// <summary>
100 99 /// Add the specified item to the collection.
101 100 /// </summary>
102 101 /// <param name="item">The item to add.</param>
103 102 /// <remarks>
104 103 /// If the collection is alredy disposed, the item isn't added to the collection and disposed if possible.
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>
118 119 /// Disposes the components stored in the collection.
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 }
127 130
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