annotate Implab/Components/ComponentContainer.cs @ 184:d6a8cba73acc ref20160224

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