Mercurial > pub > ImplabNet
changeset 153:b933ec88446e v2
docs
author | cin |
---|---|
date | Fri, 12 Feb 2016 00:59:29 +0300 |
parents | 240aa6994018 |
children | 2dcdee4c0810 |
files | Implab/Components/App.cs Implab/Components/ComponentContainer.cs Implab/Components/DisposablePool.cs |
diffstat | 3 files changed, 68 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/Implab/Components/App.cs Thu Feb 11 01:56:27 2016 +0300 +++ b/Implab/Components/App.cs Fri Feb 12 00:59:29 2016 +0300 @@ -8,12 +8,17 @@ public static class App { readonly static ComponentContainer<object> _root = new ComponentContainer<object>(); + /// <summary> + /// The container for application level components. + /// </summary> + /// <remarks>Pools of disposable objects can be placed here and they will be automatically + /// disposed when application domain is unloaded.</remarks> public static ICollection<object> RootContainer { get { return _root; } } static App() { - AppDomain.CurrentDomain.ProcessExit += (sender, e) => _root.Dispose(); + AppDomain.CurrentDomain.DomainUnload += (sender, e) => _root.Dispose(); } } }
--- a/Implab/Components/ComponentContainer.cs Thu Feb 11 01:56:27 2016 +0300 +++ b/Implab/Components/ComponentContainer.cs Fri Feb 12 00:59:29 2016 +0300 @@ -4,12 +4,18 @@ namespace Implab.Components { /// <summary> - /// Component container. + /// Component container, used to store track components in multi-threaded environmment. /// </summary> /// <remarks>Instanses of this class are thread safe.</remarks> public class ComponentContainer<T> : Disposable, ICollection<T> { readonly HashSet<T> m_components = new HashSet<T>(); + /// <summary> + /// Removes currently stored compoenents from the container and disposes them if possible. + /// </summary> + /// <remarks> + /// A new components may be added before this method completes. + /// </remarks> public void Clear() { T[] removed; @@ -23,21 +29,37 @@ item.Dispose(); } + /// <summary> + /// Checks whether the specified item in the collection. + /// </summary> + /// <param name="item">The item to check.</param> public bool Contains(T item) { lock (m_components) return m_components.Contains(item); } + /// <summary> + /// Copies currently stored components to the specified array. + /// </summary> + /// <param name="array">A destination array for components.</param> + /// <param name="arrayIndex">A starting index in the destination array.</param> public void CopyTo(T[] array, int arrayIndex) { lock (m_components) m_components.CopyTo(array, arrayIndex); } + /// <summary> + /// Remove the specified item from the collection. + /// </summary> + /// <param name="item">The item to remove.</param> public bool Remove(T item) { lock (m_components) return m_components.Remove(item); } + /// <summary> + /// Gets the count of components in the collection. + /// </summary> public int Count { get { lock (m_components) @@ -45,12 +67,22 @@ } } + /// <summary> + /// Gets a value indicating whether this instance is read only. + /// </summary> + /// <remarks> + /// Always false. + /// </remarks> public bool IsReadOnly { get { return false; } } + /// <summary> + /// Gets the enumerator for components in the collection. + /// </summary> + /// <returns>The enumerator.</returns> public IEnumerator<T> GetEnumerator() { T[] items; lock (m_components) { @@ -64,6 +96,13 @@ return GetEnumerator(); } + /// <summary> + /// Add the specified item to the collection. + /// </summary> + /// <param name="item">The item to add.</param> + /// <remarks> + /// If the collection is alredy disposed, the item isn't added to the collection and disposed if possible. + /// </remarks> public void Add(T item) { Safe.ArgumentNotNull(item, "item"); @@ -75,6 +114,10 @@ } } + /// <summary> + /// Disposes the components stored in the collection. + /// </summary> + /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param> protected override void Dispose(bool disposing) { base.Dispose(disposing); Clear();
--- a/Implab/Components/DisposablePool.cs Thu Feb 11 01:56:27 2016 +0300 +++ b/Implab/Components/DisposablePool.cs Fri Feb 12 00:59:29 2016 +0300 @@ -4,7 +4,16 @@ using System.Diagnostics; using System.Diagnostics.CodeAnalysis; -namespace Implab { +namespace Implab.Components { + /// <summary> + /// The base class for implementing pools of disposable objects. + /// </summary> + /// <remarks> + /// <para>This class maintains a set of pre-created objects and which are frequently allocated and released + /// by clients. The pool maintains maximum number of unsued object, any object above this limit is disposed, + /// if the pool is empty it will create new objects on demand.</para> + /// <para>Instances of this class are thread-safe.</para> + /// </remarks> public abstract class DisposablePool<T> : IDisposable { readonly int m_size; readonly AsyncQueue<T> m_queue = new AsyncQueue<T>(); @@ -62,8 +71,9 @@ ((IDisposable)instance).Dispose() ; } else { - if (instance is IDisposable) - ((IDisposable)instance).Dispose(); + var disposable = instance as IDisposable; + if (disposable != null) + disposable.Dispose(); } } @@ -71,9 +81,11 @@ if (disposing) { m_disposed = true; T instance; - while (m_queue.TryDequeue(out instance)) - if (instance is IDisposable) - ((IDisposable)instance).Dispose(); + while (m_queue.TryDequeue(out instance)) { + var disposable = instance as IDisposable; + if (disposable != null) + disposable.Dispose(); + } } }