Switched the parenting collection back to derive directly from the .NET Framework collection class to simplify the implementation of the DisposeItems() method; fixed some documentation errors in one of the rectangle packers

git-svn-id: file:///srv/devel/repo-conversion/nusu@41 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-07-19 20:02:05 +00:00
parent fba9d87e65
commit cf92f22b31
3 changed files with 30 additions and 19 deletions

View File

@ -33,7 +33,7 @@ namespace Nuclex.Support.Collections {
/// </remarks> /// </remarks>
/// <typeparam name="ParentType">Type of the parent object to assign to items</typeparam> /// <typeparam name="ParentType">Type of the parent object to assign to items</typeparam>
/// <typeparam name="ItemType">Type of the items being managed in the collection</typeparam> /// <typeparam name="ItemType">Type of the items being managed in the collection</typeparam>
public class ParentingCollection<ParentType, ItemType> : AcquiringCollection<ItemType> public class ParentingCollection<ParentType, ItemType> : Collection<ItemType>
where ItemType : Parentable<ParentType> { where ItemType : Parentable<ParentType> {
/// <summary>Reparents all elements in the collection</summary> /// <summary>Reparents all elements in the collection</summary>
@ -45,25 +45,35 @@ namespace Nuclex.Support.Collections {
base[index].SetParent(parent); base[index].SetParent(parent);
} }
/// <summary>Called when an item has been added to the collection</summary> /// <summary>Clears all elements from the collection</summary>
/// <param name="item">Item that has been added to the collection</param> protected override void ClearItems() {
protected override void OnAdded(ItemType item) { for(int index = 0; index < Count; ++index)
base[index].SetParent(default(ParentType));
base.ClearItems();
}
/// <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>
protected override void InsertItem(int index, ItemType item) {
base.InsertItem(index, item);
item.SetParent(this.parent); item.SetParent(this.parent);
} }
/// <summary>Called when an item has been removed from the collection</summary> /// <summary>Removes an element from the collection</summary>
/// <param name="item">Item that has been removed from the collection</param> /// <param name="index">Index of the element to remove</param>
protected override void OnRemoved(ItemType item) { protected override void RemoveItem(int index) {
item.SetParent(default(ParentType)); base[index].SetParent(default(ParentType));
base.RemoveItem(index);
} }
/// <summary>Called when the collection is being cleared</summary> /// <summary>Takes over a new element that is directly assigned</summary>
protected override void OnClearing() { /// <param name="index">Index of the element that was assigned</param>
/// <param name="item">New item</param>
// Unset the parent of all objects before allowing the list to be emptied protected override void SetItem(int index, ItemType item) {
for(int index = 0; index < base.Count; ++index) base.SetItem(index, item);
base[index].SetParent(default(ParentType)); item.SetParent(this.parent);
} }
/// <summary>Disposes all items contained in the collection</summary> /// <summary>Disposes all items contained in the collection</summary>
@ -89,14 +99,14 @@ namespace Nuclex.Support.Collections {
IDisposable disposable = base[index] as IDisposable; IDisposable disposable = base[index] as IDisposable;
base.RemoveAt(index);
// If the item is disposable, destroy it now // If the item is disposable, destroy it now
if(disposable != null) if(disposable != null)
disposable.Dispose(); disposable.Dispose();
} }
base.ClearItems();
} }
/// <summary>Parent this collection currently belongs to</summary> /// <summary>Parent this collection currently belongs to</summary>

View File

@ -110,8 +110,8 @@ namespace Nuclex.Support.Packing {
/// <summary>Finds the best position for a rectangle of the given dimensions</summary> /// <summary>Finds the best position for a rectangle of the given dimensions</summary>
/// <param name="rectangleWidth">Width of the rectangle to find a position for</param> /// <param name="rectangleWidth">Width of the rectangle to find a position for</param>
/// <param name="rectangleHeight">Height of the rectangle to find a position for</param> /// <param name="rectangleHeight">Height of the rectangle to find a position for</param>
/// <param name="placement">Received the best placement found for the rectangle</param> /// <param name="placement">Receives the best placement found for the rectangle</param>
/// <returns>The best position for a rectangle of the specified dimensions</returns> /// <returns>True if a valid placement for the rectangle could be found</returns>
private bool tryFindBestPlacement( private bool tryFindBestPlacement(
int rectangleWidth, int rectangleHeight, out Point placement int rectangleWidth, int rectangleHeight, out Point placement
) { ) {

View File

@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public
License along with this library License along with this library
*/ */
#endregion #endregion
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading; using System.Threading;