annotate Implab/Components/ComponentContainer.cs @ 152:240aa6994018 v2

component model refactoring
author cin
date Thu, 11 Feb 2016 01:56:27 +0300
parents
children b933ec88446e
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>
240aa6994018 component model refactoring
cin
parents:
diff changeset
7 /// Component container.
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
240aa6994018 component model refactoring
cin
parents:
diff changeset
13 public void Clear() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
14 T[] removed;
240aa6994018 component model refactoring
cin
parents:
diff changeset
15
240aa6994018 component model refactoring
cin
parents:
diff changeset
16 lock (m_components) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
17 removed = new T[m_components.Count];
240aa6994018 component model refactoring
cin
parents:
diff changeset
18 m_components.CopyTo(removed);
240aa6994018 component model refactoring
cin
parents:
diff changeset
19 m_components.Clear();
240aa6994018 component model refactoring
cin
parents:
diff changeset
20 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
21
240aa6994018 component model refactoring
cin
parents:
diff changeset
22 foreach (var item in removed.OfType<IDisposable>())
240aa6994018 component model refactoring
cin
parents:
diff changeset
23 item.Dispose();
240aa6994018 component model refactoring
cin
parents:
diff changeset
24 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
25
240aa6994018 component model refactoring
cin
parents:
diff changeset
26 public bool Contains(T item) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
27 lock (m_components)
240aa6994018 component model refactoring
cin
parents:
diff changeset
28 return m_components.Contains(item);
240aa6994018 component model refactoring
cin
parents:
diff changeset
29 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
30
240aa6994018 component model refactoring
cin
parents:
diff changeset
31 public void CopyTo(T[] array, int arrayIndex) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
32 lock (m_components)
240aa6994018 component model refactoring
cin
parents:
diff changeset
33 m_components.CopyTo(array, arrayIndex);
240aa6994018 component model refactoring
cin
parents:
diff changeset
34 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
35
240aa6994018 component model refactoring
cin
parents:
diff changeset
36 public bool Remove(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.Remove(item);
240aa6994018 component model refactoring
cin
parents:
diff changeset
39 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
40
240aa6994018 component model refactoring
cin
parents:
diff changeset
41 public int Count {
240aa6994018 component model refactoring
cin
parents:
diff changeset
42 get {
240aa6994018 component model refactoring
cin
parents:
diff changeset
43 lock (m_components)
240aa6994018 component model refactoring
cin
parents:
diff changeset
44 return m_components.Count;
240aa6994018 component model refactoring
cin
parents:
diff changeset
45 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
46 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
47
240aa6994018 component model refactoring
cin
parents:
diff changeset
48 public bool IsReadOnly {
240aa6994018 component model refactoring
cin
parents:
diff changeset
49 get {
240aa6994018 component model refactoring
cin
parents:
diff changeset
50 return false;
240aa6994018 component model refactoring
cin
parents:
diff changeset
51 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
52 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
53
240aa6994018 component model refactoring
cin
parents:
diff changeset
54 public IEnumerator<T> GetEnumerator() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
55 T[] items;
240aa6994018 component model refactoring
cin
parents:
diff changeset
56 lock (m_components) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
57 items = new T[m_components.Count];
240aa6994018 component model refactoring
cin
parents:
diff changeset
58 m_components.CopyTo(items);
240aa6994018 component model refactoring
cin
parents:
diff changeset
59 return (IEnumerator<T>)items.GetEnumerator();
240aa6994018 component model refactoring
cin
parents:
diff changeset
60 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
61 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
62
240aa6994018 component model refactoring
cin
parents:
diff changeset
63 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
240aa6994018 component model refactoring
cin
parents:
diff changeset
64 return GetEnumerator();
240aa6994018 component model refactoring
cin
parents:
diff changeset
65 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
66
240aa6994018 component model refactoring
cin
parents:
diff changeset
67 public void Add(T item) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
68 Safe.ArgumentNotNull(item, "item");
240aa6994018 component model refactoring
cin
parents:
diff changeset
69
240aa6994018 component model refactoring
cin
parents:
diff changeset
70 lock (m_components) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
71 if (IsDisposed)
240aa6994018 component model refactoring
cin
parents:
diff changeset
72 Safe.Dispose(item);
240aa6994018 component model refactoring
cin
parents:
diff changeset
73 else
240aa6994018 component model refactoring
cin
parents:
diff changeset
74 m_components.Add(item);
240aa6994018 component model refactoring
cin
parents:
diff changeset
75 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
76 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
77
240aa6994018 component model refactoring
cin
parents:
diff changeset
78 protected override void Dispose(bool disposing) {
240aa6994018 component model refactoring
cin
parents:
diff changeset
79 base.Dispose(disposing);
240aa6994018 component model refactoring
cin
parents:
diff changeset
80 Clear();
240aa6994018 component model refactoring
cin
parents:
diff changeset
81 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
82 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
83 }
240aa6994018 component model refactoring
cin
parents:
diff changeset
84