annotate Implab/Components/ComponentContainer.cs @ 226:9428ea36838e v2

fixed JSON parser error when parsing escaped double quote symbol
author cin
date Fri, 25 Aug 2017 02:16:35 +0300
parents 1e082fb67a46
children
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> {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
11 List<T> m_components = new List<T>();
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
12 readonly object m_lock = new object();
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
13
153
cin
parents: 152
diff changeset
14 /// <summary>
cin
parents: 152
diff changeset
15 /// Removes currently stored compoenents from the container and disposes them if possible.
cin
parents: 152
diff changeset
16 /// </summary>
cin
parents: 152
diff changeset
17 /// <remarks>
cin
parents: 152
diff changeset
18 /// A new components may be added before this method completes.
cin
parents: 152
diff changeset
19 /// </remarks>
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
20 public void Clear() {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
21 List<T> removed;
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
22
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
23 lock (m_lock) {
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
24 removed = m_components;
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
25 m_components = new List<T>();
152
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) {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
37 lock (m_lock)
152
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) {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
47 lock (m_lock)
152
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) {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
56 lock (m_lock)
152
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 {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
65 lock (m_lock)
152
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() {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
87 T[] items = new T[m_components.Count];
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
88 lock (m_lock) {
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
89 m_components.CopyTo(items);
240aa6994018 component model refactoring
cin
parents:
diff changeset
90 }
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
91 return (IEnumerator<T>)items.GetEnumerator();
152
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 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
95 return GetEnumerator();
240aa6994018 component model refactoring
cin
parents:
diff changeset
96 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
97
153
cin
parents: 152
diff changeset
98 /// <summary>
cin
parents: 152
diff changeset
99 /// Add the specified item to the collection.
cin
parents: 152
diff changeset
100 /// </summary>
cin
parents: 152
diff changeset
101 /// <param name="item">The item to add.</param>
cin
parents: 152
diff changeset
102 /// <remarks>
cin
parents: 152
diff changeset
103 /// If the collection is alredy disposed, the item isn't added to the collection and disposed if possible.
cin
parents: 152
diff changeset
104 /// </remarks>
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
105 public void Add(T item) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
106 Safe.ArgumentNotNull(item, "item");
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
107 bool dispose = false;
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
108 lock (m_lock) {
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
109 if (IsDisposed)
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
110 dispose = true;
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
111 else
240aa6994018 component model refactoring
cin
parents:
diff changeset
112 m_components.Add(item);
240aa6994018 component model refactoring
cin
parents:
diff changeset
113 }
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
114 if (dispose)
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
115 Safe.Dispose(item);
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
116 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
117
153
cin
parents: 152
diff changeset
118 /// <summary>
cin
parents: 152
diff changeset
119 /// Disposes the components stored in the collection.
cin
parents: 152
diff changeset
120 /// </summary>
cin
parents: 152
diff changeset
121 /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param>
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
122 protected override void Dispose(bool disposing) {
216
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
123 if (disposing)
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
124 Clear();
1e082fb67a46 Fixed component container
cin
parents: 153
diff changeset
125
152
240aa6994018 component model refactoring
cin
parents:
diff changeset
126 base.Dispose(disposing);
240aa6994018 component model refactoring
cin
parents:
diff changeset
127 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
128 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
129 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
130