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, IDisposable
where ItemType : Parentable
where ParentType : class {
/// Called when the object is garbage-collected
~ParentingCollection() {
Dispose(false); // called from GC
}
/// Reparents all elements in the collection
/// New parent to take ownership of the items
internal void Reparent(ParentType parent) {
this.parent = parent;
for(int index = 0; index < Count; ++index)
base[index].SetParent(parent);
}
/// Called when the asset needs to release its resources
///
/// Whether the mehod has been called from user code. If this argument
/// is false, the object is being disposed by the garbage collection and
/// it mustn't access other objects (including the attempt to Dispose() them)
/// as these might have already been destroyed by the GC.
///
protected virtual void Dispose(bool calledByUser) {
// Only destroy the other resources when we're not being called from
// the garbage collector, otherwise we'd risk accessing objects that
// have already been disposed
if(calledByUser) {
// Have the items do their cleanup work
Reparent(null);
// Dispose of all the items in the collection
foreach(ItemType item in this) {
IDisposable disposable = item as IDisposable;
if(disposable != null)
disposable.Dispose();
}
// Remove all items from the collection
base.ClearItems();
}
}
/// 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);
}
/// Release all resources owned by the instance explicitely
void IDisposable.Dispose() {
Dispose(true); // Called by user
}
/// Parent this collection currently belongs to
private ParentType parent;
}
} // namespace Nuclex.Support.Collections