ComponentContainer.cs
127 lines
| 4.0 KiB
| text/x-csharp
|
CSharpLexer
|
|
r152 | using System; | ||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| namespace Implab.Components { | ||||
| /// <summary> | ||||
|
|
r153 | /// Component container, used to store track components in multi-threaded environmment. | ||
|
|
r152 | /// </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>(); | ||||
|
|
r153 | /// <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> | ||||
|
|
r152 | public void Clear() { | ||
| T[] removed; | ||||
| lock (m_components) { | ||||
| removed = new T[m_components.Count]; | ||||
| m_components.CopyTo(removed); | ||||
| m_components.Clear(); | ||||
| } | ||||
| foreach (var item in removed.OfType<IDisposable>()) | ||||
| item.Dispose(); | ||||
| } | ||||
|
|
r153 | /// <summary> | ||
| /// Checks whether the specified item in the collection. | ||||
| /// </summary> | ||||
| /// <param name="item">The item to check.</param> | ||||
|
|
r152 | public bool Contains(T item) { | ||
| lock (m_components) | ||||
| return m_components.Contains(item); | ||||
| } | ||||
|
|
r153 | /// <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> | ||||
|
|
r152 | public void CopyTo(T[] array, int arrayIndex) { | ||
| lock (m_components) | ||||
| m_components.CopyTo(array, arrayIndex); | ||||
| } | ||||
|
|
r153 | /// <summary> | ||
| /// Remove the specified item from the collection. | ||||
| /// </summary> | ||||
| /// <param name="item">The item to remove.</param> | ||||
|
|
r152 | public bool Remove(T item) { | ||
| lock (m_components) | ||||
| return m_components.Remove(item); | ||||
| } | ||||
|
|
r153 | /// <summary> | ||
| /// Gets the count of components in the collection. | ||||
| /// </summary> | ||||
|
|
r152 | public int Count { | ||
| get { | ||||
| lock (m_components) | ||||
| return m_components.Count; | ||||
| } | ||||
| } | ||||
|
|
r153 | /// <summary> | ||
| /// Gets a value indicating whether this instance is read only. | ||||
| /// </summary> | ||||
| /// <remarks> | ||||
| /// Always false. | ||||
| /// </remarks> | ||||
|
|
r152 | public bool IsReadOnly { | ||
| get { | ||||
| return false; | ||||
| } | ||||
| } | ||||
|
|
r153 | /// <summary> | ||
| /// Gets the enumerator for components in the collection. | ||||
| /// </summary> | ||||
| /// <returns>The enumerator.</returns> | ||||
|
|
r152 | public IEnumerator<T> GetEnumerator() { | ||
| T[] items; | ||||
| lock (m_components) { | ||||
| items = new T[m_components.Count]; | ||||
| m_components.CopyTo(items); | ||||
| return (IEnumerator<T>)items.GetEnumerator(); | ||||
| } | ||||
| } | ||||
| System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { | ||||
| return GetEnumerator(); | ||||
| } | ||||
|
|
r153 | /// <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> | ||||
|
|
r152 | public void Add(T item) { | ||
| Safe.ArgumentNotNull(item, "item"); | ||||
| lock (m_components) { | ||||
| if (IsDisposed) | ||||
| Safe.Dispose(item); | ||||
| else | ||||
| m_components.Add(item); | ||||
| } | ||||
| } | ||||
|
|
r153 | /// <summary> | ||
| /// Disposes the components stored in the collection. | ||||
| /// </summary> | ||||
| /// <param name="disposing">If set to <c>true</c> the collection is disposing.</param> | ||||
|
|
r152 | protected override void Dispose(bool disposing) { | ||
| base.Dispose(disposing); | ||||
| Clear(); | ||||
| } | ||||
| } | ||||
| } | ||||
