using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; using System.Reflection; namespace Nuclex.Support.Collections { /// Collection that automatically assigns an owner to all its elements /// /// This collection automatically assigns a parent object to elements that /// are managed in it. The elements have to derive from the Parentable<> /// base class. /// /// Type of the parent object to assign to items /// Type of the items being managed in the collection public class ParentingCollection : Collection where ItemType : Parentable where ParentType : class { /// Reparents all elements in the collection /// New parent to take ownership of the items protected void Reparent(ParentType parent) { this.parent = parent; for(int index = 0; index < Count; ++index) base[index].SetParent(parent); } /// Clears all elements from the collection protected override void ClearItems() { for(int index = 0; index < Count; ++index) base[index].SetParent(null); base.ClearItems(); } /// Inserts a new element into the collection /// Index at which to insert the element /// Item to be inserted protected override void InsertItem(int index, ItemType item) { base.InsertItem(index, item); item.SetParent(this.parent); } /// Removes an element from the collection /// Index of the element to remove protected override void RemoveItem(int index) { base[index].SetParent(null); base.RemoveItem(index); } /// Takes over a new element that is directly assigned /// Index of the element that was assigned /// New item protected override void SetItem(int index, ItemType item) { base.SetItem(index, item); item.SetParent(this.parent); } /// Disposes the collection and optionally all items contained therein /// Whether to try calling Dispose() on all items /// /// This method is intended to support collections that need to dispose of their /// items. The ParentingCollection will first detach all items from the parent /// object and then call Dispose() on any item that implements IDisposable. /// protected void InternalDispose(bool disposeItems) { if(disposeItems) { // Have the items do their cleanup work Reparent(null); // Dispose of all the items in the collection that implement IDisposable foreach(ItemType item in this) { IDisposable disposable = item as IDisposable; if(disposable != null) disposable.Dispose(); } } // Remove all items from the collection base.ClearItems(); } /// Parent this collection currently belongs to private ParentType parent; } } // namespace Nuclex.Support.Collections