##// END OF EJS Templates
docs
cin -
r153:b933ec88446e v2
parent child
Show More
@@ -8,12 +8,17 namespace Implab.Components {
8 public static class App {
8 public static class App {
9 readonly static ComponentContainer<object> _root = new ComponentContainer<object>();
9 readonly static ComponentContainer<object> _root = new ComponentContainer<object>();
10
10
11 /// <summary>
12 /// The container for application level components.
13 /// </summary>
14 /// <remarks>Pools of disposable objects can be placed here and they will be automatically
15 /// disposed when application domain is unloaded.</remarks>
11 public static ICollection<object> RootContainer {
16 public static ICollection<object> RootContainer {
12 get { return _root; }
17 get { return _root; }
13 }
18 }
14
19
15 static App() {
20 static App() {
16 AppDomain.CurrentDomain.ProcessExit += (sender, e) => _root.Dispose();
21 AppDomain.CurrentDomain.DomainUnload += (sender, e) => _root.Dispose();
17 }
22 }
18 }
23 }
19 }
24 }
@@ -4,12 +4,18 using System.Linq;
4
4
5 namespace Implab.Components {
5 namespace Implab.Components {
6 /// <summary>
6 /// <summary>
7 /// Component container.
7 /// Component container, used to store track components in multi-threaded environmment.
8 /// </summary>
8 /// </summary>
9 /// <remarks>Instanses of this class are thread safe.</remarks>
9 /// <remarks>Instanses of this class are thread safe.</remarks>
10 public class ComponentContainer<T> : Disposable, ICollection<T> {
10 public class ComponentContainer<T> : Disposable, ICollection<T> {
11 readonly HashSet<T> m_components = new HashSet<T>();
11 readonly HashSet<T> m_components = new HashSet<T>();
12
12
13 /// <summary>
14 /// Removes currently stored compoenents from the container and disposes them if possible.
15 /// </summary>
16 /// <remarks>
17 /// A new components may be added before this method completes.
18 /// </remarks>
13 public void Clear() {
19 public void Clear() {
14 T[] removed;
20 T[] removed;
15
21
@@ -23,21 +29,37 namespace Implab.Components {
23 item.Dispose();
29 item.Dispose();
24 }
30 }
25
31
32 /// <summary>
33 /// Checks whether the specified item in the collection.
34 /// </summary>
35 /// <param name="item">The item to check.</param>
26 public bool Contains(T item) {
36 public bool Contains(T item) {
27 lock (m_components)
37 lock (m_components)
28 return m_components.Contains(item);
38 return m_components.Contains(item);
29 }
39 }
30
40
41 /// <summary>
42 /// Copies currently stored components to the specified array.
43 /// </summary>
44 /// <param name="array">A destination array for components.</param>
45 /// <param name="arrayIndex">A starting index in the destination array.</param>
31 public void CopyTo(T[] array, int arrayIndex) {
46 public void CopyTo(T[] array, int arrayIndex) {
32 lock (m_components)
47 lock (m_components)
33 m_components.CopyTo(array, arrayIndex);
48 m_components.CopyTo(array, arrayIndex);
34 }
49 }
35
50
51 /// <summary>
52 /// Remove the specified item from the collection.
53 /// </summary>
54 /// <param name="item">The item to remove.</param>
36 public bool Remove(T item) {
55 public bool Remove(T item) {
37 lock (m_components)
56 lock (m_components)
38 return m_components.Remove(item);
57 return m_components.Remove(item);
39 }
58 }
40
59
60 /// <summary>
61 /// Gets the count of components in the collection.
62 /// </summary>
41 public int Count {
63 public int Count {
42 get {
64 get {
43 lock (m_components)
65 lock (m_components)
@@ -45,12 +67,22 namespace Implab.Components {
45 }
67 }
46 }
68 }
47
69
70 /// <summary>
71 /// Gets a value indicating whether this instance is read only.
72 /// </summary>
73 /// <remarks>
74 /// Always false.
75 /// </remarks>
48 public bool IsReadOnly {
76 public bool IsReadOnly {
49 get {
77 get {
50 return false;
78 return false;
51 }
79 }
52 }
80 }
53
81
82 /// <summary>
83 /// Gets the enumerator for components in the collection.
84 /// </summary>
85 /// <returns>The enumerator.</returns>
54 public IEnumerator<T> GetEnumerator() {
86 public IEnumerator<T> GetEnumerator() {
55 T[] items;
87 T[] items;
56 lock (m_components) {
88 lock (m_components) {
@@ -64,6 +96,13 namespace Implab.Components {
64 return GetEnumerator();
96 return GetEnumerator();
65 }
97 }
66
98
99 /// <summary>
100 /// Add the specified item to the collection.
101 /// </summary>
102 /// <param name="item">The item to add.</param>
103 /// <remarks>
104 /// If the collection is alredy disposed, the item isn't added to the collection and disposed if possible.
105 /// </remarks>
67 public void Add(T item) {
106 public void Add(T item) {
68 Safe.ArgumentNotNull(item, "item");
107 Safe.ArgumentNotNull(item, "item");
69
108
@@ -75,6 +114,10 namespace Implab.Components {
75 }
114 }
76 }
115 }
77
116
117 /// <summary>
118 /// Disposes the components stored in the collection.
119 /// </summary>
120 /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param>
78 protected override void Dispose(bool disposing) {
121 protected override void Dispose(bool disposing) {
79 base.Dispose(disposing);
122 base.Dispose(disposing);
80 Clear();
123 Clear();
@@ -4,7 +4,16 using System.Threading;
4 using System.Diagnostics;
4 using System.Diagnostics;
5 using System.Diagnostics.CodeAnalysis;
5 using System.Diagnostics.CodeAnalysis;
6
6
7 namespace Implab {
7 namespace Implab.Components {
8 /// <summary>
9 /// The base class for implementing pools of disposable objects.
10 /// </summary>
11 /// <remarks>
12 /// <para>This class maintains a set of pre-created objects and which are frequently allocated and released
13 /// by clients. The pool maintains maximum number of unsued object, any object above this limit is disposed,
14 /// if the pool is empty it will create new objects on demand.</para>
15 /// <para>Instances of this class are thread-safe.</para>
16 /// </remarks>
8 public abstract class DisposablePool<T> : IDisposable {
17 public abstract class DisposablePool<T> : IDisposable {
9 readonly int m_size;
18 readonly int m_size;
10 readonly AsyncQueue<T> m_queue = new AsyncQueue<T>();
19 readonly AsyncQueue<T> m_queue = new AsyncQueue<T>();
@@ -62,8 +71,9 namespace Implab {
62 ((IDisposable)instance).Dispose() ;
71 ((IDisposable)instance).Dispose() ;
63
72
64 } else {
73 } else {
65 if (instance is IDisposable)
74 var disposable = instance as IDisposable;
66 ((IDisposable)instance).Dispose();
75 if (disposable != null)
76 disposable.Dispose();
67 }
77 }
68 }
78 }
69
79
@@ -71,9 +81,11 namespace Implab {
71 if (disposing) {
81 if (disposing) {
72 m_disposed = true;
82 m_disposed = true;
73 T instance;
83 T instance;
74 while (m_queue.TryDequeue(out instance))
84 while (m_queue.TryDequeue(out instance)) {
75 if (instance is IDisposable)
85 var disposable = instance as IDisposable;
76 ((IDisposable)instance).Dispose();
86 if (disposable != null)
87 disposable.Dispose();
88 }
77 }
89 }
78 }
90 }
79
91
General Comments 0
You need to be logged in to leave comments. Login now