##// END OF EJS Templates
missing files
missing files

File last commit:

r216:1e082fb67a46 v2
r246:5aa9cfbe56c3 v3
Show More
ComponentContainer.cs
130 lines | 4.1 KiB | text/x-csharp | CSharpLexer
/ Implab / Components / ComponentContainer.cs
cin
component model refactoring
r152 using System;
using System.Collections.Generic;
using System.Linq;
namespace Implab.Components {
/// <summary>
cin
docs
r153 /// Component container, used to store track components in multi-threaded environmment.
cin
component model refactoring
r152 /// </summary>
/// <remarks>Instanses of this class are thread safe.</remarks>
public class ComponentContainer<T> : Disposable, ICollection<T> {
cin
Fixed component container
r216 List<T> m_components = new List<T>();
readonly object m_lock = new object();
cin
component model refactoring
r152
cin
docs
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>
cin
component model refactoring
r152 public void Clear() {
cin
Fixed component container
r216 List<T> removed;
cin
component model refactoring
r152
cin
Fixed component container
r216 lock (m_lock) {
removed = m_components;
m_components = new List<T>();
cin
component model refactoring
r152 }
foreach (var item in removed.OfType<IDisposable>())
item.Dispose();
}
cin
docs
r153 /// <summary>
/// Checks whether the specified item in the collection.
/// </summary>
/// <param name="item">The item to check.</param>
cin
component model refactoring
r152 public bool Contains(T item) {
cin
Fixed component container
r216 lock (m_lock)
cin
component model refactoring
r152 return m_components.Contains(item);
}
cin
docs
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>
cin
component model refactoring
r152 public void CopyTo(T[] array, int arrayIndex) {
cin
Fixed component container
r216 lock (m_lock)
cin
component model refactoring
r152 m_components.CopyTo(array, arrayIndex);
}
cin
docs
r153 /// <summary>
/// Remove the specified item from the collection.
/// </summary>
/// <param name="item">The item to remove.</param>
cin
component model refactoring
r152 public bool Remove(T item) {
cin
Fixed component container
r216 lock (m_lock)
cin
component model refactoring
r152 return m_components.Remove(item);
}
cin
docs
r153 /// <summary>
/// Gets the count of components in the collection.
/// </summary>
cin
component model refactoring
r152 public int Count {
get {
cin
Fixed component container
r216 lock (m_lock)
cin
component model refactoring
r152 return m_components.Count;
}
}
cin
docs
r153 /// <summary>
/// Gets a value indicating whether this instance is read only.
/// </summary>
/// <remarks>
/// Always false.
/// </remarks>
cin
component model refactoring
r152 public bool IsReadOnly {
get {
return false;
}
}
cin
docs
r153 /// <summary>
/// Gets the enumerator for components in the collection.
/// </summary>
/// <returns>The enumerator.</returns>
cin
component model refactoring
r152 public IEnumerator<T> GetEnumerator() {
cin
Fixed component container
r216 T[] items = new T[m_components.Count];
lock (m_lock) {
cin
component model refactoring
r152 m_components.CopyTo(items);
}
cin
Fixed component container
r216 return (IEnumerator<T>)items.GetEnumerator();
cin
component model refactoring
r152 }
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return GetEnumerator();
}
cin
docs
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>
cin
component model refactoring
r152 public void Add(T item) {
Safe.ArgumentNotNull(item, "item");
cin
Fixed component container
r216 bool dispose = false;
lock (m_lock) {
cin
component model refactoring
r152 if (IsDisposed)
cin
Fixed component container
r216 dispose = true;
cin
component model refactoring
r152 else
m_components.Add(item);
}
cin
Fixed component container
r216 if (dispose)
Safe.Dispose(item);
cin
component model refactoring
r152 }
cin
docs
r153 /// <summary>
/// Disposes the components stored in the collection.
/// </summary>
/// <param name="disposing">If set to <c>true</c> the collection is disposing.</param>
cin
component model refactoring
r152 protected override void Dispose(bool disposing) {
cin
Fixed component container
r216 if (disposing)
Clear();
cin
component model refactoring
r152 base.Dispose(disposing);
}
}
}