2007-05-11 21:15:35 +00:00
|
|
|
#region CPL License
|
|
|
|
/*
|
|
|
|
Nuclex Framework
|
2014-07-19 09:13:36 +00:00
|
|
|
Copyright (C) 2002-2014 Nuclex Development Labs
|
2007-05-11 21:15:35 +00:00
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
License along with this library
|
|
|
|
*/
|
|
|
|
#endregion
|
2007-07-24 20:15:19 +00:00
|
|
|
|
2007-02-28 20:20:50 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
using System.IO;
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Collections {
|
|
|
|
|
|
|
|
/// <summary>Collection that automatically assigns an owner to all its elements</summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This collection automatically assigns a parent object to elements that
|
|
|
|
/// are managed in it. The elements have to derive from the Parentable<>
|
|
|
|
/// base class.
|
|
|
|
/// </remarks>
|
2012-02-29 16:27:43 +00:00
|
|
|
/// <typeparam name="TParent">Type of the parent object to assign to items</typeparam>
|
|
|
|
/// <typeparam name="TItem">Type of the items being managed in the collection</typeparam>
|
|
|
|
public class ParentingCollection<TParent, TItem> : Collection<TItem>
|
|
|
|
where TItem : Parentable<TParent> {
|
2007-02-28 20:20:50 +00:00
|
|
|
|
|
|
|
/// <summary>Reparents all elements in the collection</summary>
|
|
|
|
/// <param name="parent">New parent to take ownership of the items</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
protected void Reparent(TParent parent) {
|
2007-02-28 20:20:50 +00:00
|
|
|
this.parent = parent;
|
|
|
|
|
|
|
|
for(int index = 0; index < Count; ++index)
|
|
|
|
base[index].SetParent(parent);
|
|
|
|
}
|
|
|
|
|
2007-07-19 20:02:05 +00:00
|
|
|
/// <summary>Clears all elements from the collection</summary>
|
|
|
|
protected override void ClearItems() {
|
|
|
|
for(int index = 0; index < Count; ++index)
|
2012-02-29 16:27:43 +00:00
|
|
|
base[index].SetParent(default(TParent));
|
2007-02-28 20:20:50 +00:00
|
|
|
|
2007-07-19 20:02:05 +00:00
|
|
|
base.ClearItems();
|
2007-02-28 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
2007-07-19 20:02:05 +00:00
|
|
|
/// <summary>Inserts a new element into the collection</summary>
|
|
|
|
/// <param name="index">Index at which to insert the element</param>
|
|
|
|
/// <param name="item">Item to be inserted</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
protected override void InsertItem(int index, TItem item) {
|
2007-07-19 20:02:05 +00:00
|
|
|
base.InsertItem(index, item);
|
|
|
|
item.SetParent(this.parent);
|
|
|
|
}
|
2007-07-16 20:09:51 +00:00
|
|
|
|
2007-07-19 20:02:05 +00:00
|
|
|
/// <summary>Removes an element from the collection</summary>
|
|
|
|
/// <param name="index">Index of the element to remove</param>
|
|
|
|
protected override void RemoveItem(int index) {
|
2012-02-29 16:27:43 +00:00
|
|
|
base[index].SetParent(default(TParent));
|
2007-07-19 20:02:05 +00:00
|
|
|
base.RemoveItem(index);
|
|
|
|
}
|
2007-07-16 20:09:51 +00:00
|
|
|
|
2007-07-19 20:02:05 +00:00
|
|
|
/// <summary>Takes over a new element that is directly assigned</summary>
|
|
|
|
/// <param name="index">Index of the element that was assigned</param>
|
|
|
|
/// <param name="item">New item</param>
|
2012-02-29 16:27:43 +00:00
|
|
|
protected override void SetItem(int index, TItem item) {
|
|
|
|
base[index].SetParent(default(TParent));
|
2007-07-19 20:02:05 +00:00
|
|
|
base.SetItem(index, item);
|
|
|
|
item.SetParent(this.parent);
|
2007-07-16 20:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Disposes all items contained in the collection</summary>
|
2007-03-05 18:22:31 +00:00
|
|
|
/// <remarks>
|
2007-05-11 21:15:35 +00:00
|
|
|
/// <para>
|
2007-07-16 20:09:51 +00:00
|
|
|
/// This method is intended to support collections that need to dispose their
|
2007-11-02 21:36:15 +00:00
|
|
|
/// items. It will unparent all of the collection's items and call Dispose()
|
2007-07-16 20:09:51 +00:00
|
|
|
/// on any item that implements IDisposable.
|
2007-05-11 21:15:35 +00:00
|
|
|
/// </para>
|
|
|
|
/// <para>
|
2007-11-02 21:36:15 +00:00
|
|
|
/// Do not call this method from your destructor as it will access the
|
|
|
|
/// contained items in order to unparent and to Dispose() them, which leads
|
|
|
|
/// to undefined behavior since the object might have already been collected
|
|
|
|
/// by the GC. Call it only if your object is being manually disposed.
|
2007-05-11 21:15:35 +00:00
|
|
|
/// </para>
|
2007-03-05 18:22:31 +00:00
|
|
|
/// </remarks>
|
2007-07-10 19:25:18 +00:00
|
|
|
protected void DisposeItems() {
|
2007-03-05 18:22:31 +00:00
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
// Dispose all the items in the collection that implement IDisposable,
|
|
|
|
// starting from the last item in the assumption that this is the fastest
|
|
|
|
// way to empty a list without causing excessive shiftings in the array.
|
|
|
|
for(int index = base.Count - 1; index >= 0; --index) {
|
|
|
|
IDisposable disposable = base[index] as IDisposable;
|
2007-05-10 19:11:28 +00:00
|
|
|
|
2007-07-10 19:25:18 +00:00
|
|
|
// If the item is disposable, destroy it now
|
2012-03-08 11:05:20 +00:00
|
|
|
if(disposable != null) {
|
2007-07-10 19:25:18 +00:00
|
|
|
disposable.Dispose();
|
2012-03-08 11:05:20 +00:00
|
|
|
}
|
2007-03-05 18:22:31 +00:00
|
|
|
}
|
|
|
|
|
2007-07-19 20:02:05 +00:00
|
|
|
base.ClearItems();
|
|
|
|
|
2007-02-28 20:20:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>Parent this collection currently belongs to</summary>
|
2012-02-29 16:27:43 +00:00
|
|
|
private TParent parent;
|
2007-02-28 20:20:50 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Collections
|