diff --git a/Nuclex.Support (PC).csproj b/Nuclex.Support (PC).csproj index 46c5d4b..98c1ad2 100644 --- a/Nuclex.Support (PC).csproj +++ b/Nuclex.Support (PC).csproj @@ -86,6 +86,11 @@ false TransformingReadOnlyCollection + + false + TransformingReadOnlyCollection.Interfaces + TransformingReadOnlyCollection.cs + false UnintrusivePriorityQueue @@ -152,6 +157,10 @@ CygonRectanglePacker.Test CygonRectanglePacker.cs + + false + OutOfSpaceException + false RectanglePacker diff --git a/Source/Collections/AcquiringCollection.cs b/Source/Collections/AcquiringCollection.cs index 33848ca..afe73e3 100644 --- a/Source/Collections/AcquiringCollection.cs +++ b/Source/Collections/AcquiringCollection.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -98,7 +99,7 @@ namespace Nuclex.Support.Collections { /// Called when the collection is being cleared /// - /// Instead of called the OnRemoved() method for each item in the collection when + /// Instead of calling the OnRemoved() method for each item in the collection when /// it is being cleared, this variant only triggers the OnClearing() method /// to allow the implementer some room for optimizations. /// diff --git a/Source/Collections/Parentable.cs b/Source/Collections/Parentable.cs index 7d75b86..c784a96 100644 --- a/Source/Collections/Parentable.cs +++ b/Source/Collections/Parentable.cs @@ -26,14 +26,6 @@ namespace Nuclex.Support.Collections { /// Type of the parent object public class Parentable { - /// Assigns a new parent to this instance - internal void SetParent(ParentType parent) { - ParentType oldParent = this.parent; - this.parent = parent; - - OnParentChanged(oldParent); - } - /// The parent object that owns this instance protected ParentType Parent { get { return this.parent; } @@ -48,6 +40,14 @@ namespace Nuclex.Support.Collections { /// Previous owner of the instance protected virtual void OnParentChanged(ParentType oldParent) { } + /// Assigns a new parent to this instance + protected internal void SetParent(ParentType parent) { + ParentType oldParent = this.parent; + this.parent = parent; + + OnParentChanged(oldParent); + } + /// Current parent of this object private ParentType parent; diff --git a/Source/Collections/ParentingCollection.cs b/Source/Collections/ParentingCollection.cs index a3f8937..4420a1d 100644 --- a/Source/Collections/ParentingCollection.cs +++ b/Source/Collections/ParentingCollection.cs @@ -47,32 +47,37 @@ namespace Nuclex.Support.Collections { /// Called when an item has been added to the collection /// Item that has been added to the collection - protected virtual void OnAdded(ItemType item) { + protected override void OnAdded(ItemType item) { item.SetParent(this.parent); } /// Called when an item has been removed from the collection /// Item that has been removed from the collection - protected virtual void OnRemoved(ItemType item) { + protected override void OnRemoved(ItemType item) { item.SetParent(default(ParentType)); } - /// Disposes the collection and optionally all items contained therein + /// Called when the collection is being cleared + protected override void OnClearing() { + + // Unset the parent of all objects before allowing the list to be emptied + for(int index = 0; index < base.Count; ++index) + base[index].SetParent(default(ParentType)); + + } + + /// Disposes all items contained in the collection /// /// - /// 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. + /// This method is intended to support collections that need to dispose their + /// items. It will unparent all of the collections items and call Dispose() + /// on any item that implements IDisposable. /// /// - /// The items contained in the collection are not disconnected from their parent - /// (eg. Reparent()ed to null) because this is out of the scope for the - /// ParentingCollection<> class to decide and provokes the potentially - /// risky situation that an item, when it is Dispose()d, might try to disconnect - /// some events or perform other maintenance work on its parent object that - /// would then be no longer available. If you wish to disconnect the items from - /// their parent prior to disposing them, add a Reparent(null); call before the - /// line with InternalDispose(true); in your custom Dispose() method. + /// Do not call this method during a GC run (eg. 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. /// /// protected void DisposeItems() { diff --git a/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs b/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs new file mode 100644 index 0000000..acc3a47 --- /dev/null +++ b/Source/Collections/TransformingReadOnlyCollection.Interfaces.cs @@ -0,0 +1,434 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2007 Nuclex Development Labs + +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 + +using System; +using System.Threading; +using System.Collections; +using System.Collections.Generic; + +namespace Nuclex.Support.Collections { + + // More than 400 lines of code just to implement the .NET collection interfaces. + // Shows the niceties of having to support languages without generics and using + // an inferior design to make collections "more convenient" for the user :/ + + partial class TransformingReadOnlyCollection< + ContainedItemType, ExposedItemType + > { + + #region IList Members + + /// + /// Determines the index of a specific item in the TransformingReadOnlyCollection. + /// + /// + /// The object to locate in the TransformingReadOnlyCollection. + /// + /// + /// The index of item if found in the list; otherwise, -1. + /// + int IList.IndexOf(ExposedItemType item) { + return IndexOf(item); + } + + /// + /// Inserts an item to the TransformingReadOnlyCollection at the specified index. + /// + /// + /// The zero-based index at which item should be inserted. + /// + /// + /// The object to insert into the TransformingReadOnlyCollection + /// + /// + /// The TransformingReadOnlyCollection is read-only. + /// + /// + /// index is not a valid index in the TransformingReadOnlyCollection. + /// + void IList.Insert(int index, ExposedItemType item) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// + /// Removes the TransformingReadOnlyCollection item at the specified index. + /// + /// The zero-based index of the item to remove. + /// + /// The TransformingReadOnlyCollection is read-only. + /// + /// + /// Index is not a valid index in the TransformingReadOnlyCollection. + /// + void IList.RemoveAt(int index) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// Gets or sets the element at the specified index. + /// The zero-based index of the element to get or set. + /// The element at the specified index. + /// + /// Index is not a valid index in the TransformingReadOnlyCollection. + /// + /// + /// The property is set and the TransformingReadOnlyCollection is read-only + /// + ExposedItemType IList.this[int index] { + get { + return this[index]; + } + set { + throw new NotSupportedException("The collection is ready-only"); + } + } + + #endregion + + #region ICollection Members + + /// Adds an item to the TransformingReadOnlyCollection. + /// The object to add to the TransformingReadOnlyCollection + /// + /// The TransformingReadOnlyCollection is read-only. + /// + void ICollection.Add(ExposedItemType item) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// Removes all items from the TransformingReadOnlyCollection + /// + /// The TransformingReadOnlyCollection is read-only. + /// + void ICollection.Clear() { + throw new NotSupportedException("The collection is ready-only"); + } + + /// + /// Determines whether the TransformingReadOnlyCollection contains a specific value. + /// + /// + /// The object to locate in the TransformingReadOnlyCollection. + /// + /// + /// True if item is found in the TransformingReadOnlyCollection; otherwise, false. + /// + bool ICollection.Contains(ExposedItemType item) { + return Contains(item); + } + + /// + /// Copies the elements of the TransformingReadOnlyCollection to an System.Array, + /// starting at a particular System.Array index. + /// + /// + /// The one-dimensional System.Array that is the destination of the elements + /// copied from TransformingReadOnlyCollection. The System.Array must have + /// zero-based indexing. + /// + /// + /// The zero-based index in array at which copying begins + /// + /// + /// ArrayIndex is less than 0. + /// + /// + /// Array is null. + /// + /// + /// Array is multidimensional or arrayIndex is equal to or greater than the + /// length of array or the number of elements in the source + /// TransformingReadOnlyCollection is greater than the available + /// space from arrayIndex to the end of the destination array or type T cannot + /// be cast automatically to the type of the destination array. + /// + void ICollection.CopyTo(ExposedItemType[] array, int arrayIndex) { + CopyTo(array, arrayIndex); + } + + /// + /// Removes the first occurrence of a specific object from the + /// TransformingReadOnlyCollection. + /// + /// + /// The object to remove from the TransformingReadOnlyCollection + /// + /// + /// True if item was successfully removed from the TransformingReadOnlyCollection; + /// otherwise, false. This method also returns false if item is not found in the + /// original TransformingReadOnlyCollection. + /// + /// + /// The TransformingReadOnlyCollection is read-only. + /// + bool ICollection.Remove(ExposedItemType item) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// + /// The number of elements contained in the TransformingReadOnlyCollection. + /// + int ICollection.Count { + get { return Count; } + } + + /// + /// A value indicating whether the TransformingReadOnlyCollection is read-only. + /// + bool ICollection.IsReadOnly { + get { return true; } + } + + #endregion + + #region IEnumerable Members + + /// Returns an enumerator that iterates through the collection. + /// + /// A System.Collections.Generic.IEnumerator<ExposedItemType> that can be used + /// to iterate through the collection. + /// + IEnumerator IEnumerable.GetEnumerator() { + return GetEnumerator(); + } + + #endregion + + #region IEnumerable Members + + /// Returns an enumerator that iterates through a collection. + /// + /// A System.Collections.IEnumerator object that can be used to iterate through + /// the collection. + /// + IEnumerator IEnumerable.GetEnumerator() { + return GetEnumerator(); + } + + #endregion + + #region IList Members + + /// Adds an item to the TransformingReadOnlyCollection. + /// + /// The System.Object to add to the TransformingReadOnlyCollection. + /// + /// The position into which the new element was inserted. + /// + /// The System.Collections.IList is read-only or the TransformingReadOnlyCollection + /// has a fixed size. + /// + int IList.Add(object value) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// Removes all items from the TransformingReadOnlyCollection. + /// + /// The TransformingReadOnlyCollection is read-only. + /// + void IList.Clear() { + throw new NotSupportedException("The collection is ready-only"); + } + + /// + /// Determines whether the TransformingReadOnlyCollection contains a specific value. + /// + /// + /// The System.Object to locate in the TransformingReadOnlyCollection. + /// + /// + /// True if the System.Object is found in the TransformingReadOnlyCollection; + /// otherwise, false. + /// + bool IList.Contains(object value) { + return Contains((ExposedItemType)value); + } + + /// + /// Determines the index of a specific item in the TransformingReadOnlyCollection. + /// + /// + /// The System.Object to locate in the TransformingReadOnlyCollection. + /// + /// + /// The index of value if found in the list; otherwise, -1. + /// + int IList.IndexOf(object value) { + return IndexOf((ExposedItemType)value); + } + + /// + /// Inserts an item to the TransformingReadOnlyCollection at the specified index. + /// + /// + /// The zero-based index at which value should be inserted. + /// + /// + /// The System.Object to insert into the TransformingReadOnlyCollection. + /// + /// + /// Index is not a valid index in the TransformingReadOnlyCollection. + /// + /// + /// The System.Collections.IList is read-only or the TransformingReadOnlyCollection + /// has a fixed size. + /// + /// + /// Value is null reference in the TransformingReadOnlyCollection. + /// + void IList.Insert(int index, object value) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// + /// A value indicating whether the TransformingReadOnlyCollection has a fixed + /// size. + /// + bool IList.IsFixedSize { + get { return true; } + } + + /// + /// A value indicating whether the index is not a valid index in the is read-only. + /// + bool IList.IsReadOnly { + get { return true; } + } + + /// + /// Removes the first occurrence of a specific object from the + /// TransformingReadOnlyCollection. + /// + /// + /// The System.Object to remove from the TransformingReadOnlyCollection. + /// + /// + /// The TransformingReadOnlyCollection is read-only or the + /// TransformingReadOnlyCollection has a fixed size. + /// + void IList.Remove(object value) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// + /// Removes the TransformingReadOnlyCollection item at the specified index. + /// + /// The zero-based index of the item to remove. + /// + /// Index is not a valid index in the TransformingReadOnlyCollection. + /// + /// + /// The TransformingReadOnlyCollection is read-only or the + /// TransformingReadOnlyCollection has a fixed size. + /// + void IList.RemoveAt(int index) { + throw new NotSupportedException("The collection is ready-only"); + } + + /// Gets or sets the element at the specified index. + /// The zero-based index of the element to get or set. + /// The element at the specified index + /// + /// Index is not a valid index in the TransformingReadOnlyCollection + /// + /// + /// The property is set and the TransformingReadOnlyCollection is read-only. + /// + object IList.this[int index] { + get { + return this[index]; + } + set { + throw new NotSupportedException("The collection is ready-only"); + } + } + + #endregion + + #region ICollection Members + + /// + /// Copies the elements of the TransformingReadOnlyCollection to an System.Array, + /// starting at a particular System.Array index. + /// + /// + /// The one-dimensional System.Array that is the destination of the elements + /// copied from TransformingReadOnlyCollection. The System.Array must have zero-based + /// indexing. + /// + /// The zero-based index in array at which copying begins. + /// + /// Array is null. + /// + /// + /// Index is less than zero. + /// + /// + /// Array is multidimensional or index is equal to or greater than the length + /// of array or the number of elements in the source TransformingReadOnlyCollection + /// is greater than the available space from index to the end of the destination + /// array. + /// + /// + /// The type of the source TransformingReadOnlyCollection cannot be cast + /// automatically to the type of the destination array. + /// + void ICollection.CopyTo(Array array, int index) { + CopyTo((ExposedItemType[])array, index); + } + + /// + /// The number of elements contained in the TransformingReadOnlyCollection. + /// + int ICollection.Count { + get { return Count; } + } + + /// + /// A value indicating whether access to the TransformingReadOnlyCollection + /// is synchronized (thread safe). + /// + bool ICollection.IsSynchronized { + get { return false; } + } + + /// + /// An object that can be used to synchronize access to the + /// TransformingReadOnlyCollection. + /// + object ICollection.SyncRoot { + get { + if(this.syncRoot == null) { + ICollection is2 = this.items as ICollection; + if(is2 != null) { + this.syncRoot = is2.SyncRoot; + } else { + Interlocked.CompareExchange(ref this.syncRoot, new object(), null); + } + } + + return this.syncRoot; + } + } + + #endregion + + } + +} // namespace Nuclex.Support.Collections diff --git a/Source/Collections/TransformingReadOnlyCollection.cs b/Source/Collections/TransformingReadOnlyCollection.cs index 045bdd5..dfabc36 100644 --- a/Source/Collections/TransformingReadOnlyCollection.cs +++ b/Source/Collections/TransformingReadOnlyCollection.cs @@ -17,11 +17,11 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; -using System.Threading; namespace Nuclex.Support.Collections { @@ -48,8 +48,9 @@ namespace Nuclex.Support.Collections { /// downcast each time you need to access elements of the non-public type. /// /// - public abstract class TransformingReadOnlyCollection : - IList, IList { + public abstract partial class TransformingReadOnlyCollection< + ContainedItemType, ExposedItemType + > : IList, IList { #region class TransformingEnumerator @@ -276,402 +277,6 @@ namespace Nuclex.Support.Collections { /// protected abstract ExposedItemType Transform(ContainedItemType item); - #region IList Members - - /// - /// Determines the index of a specific item in the TransformingReadOnlyCollection. - /// - /// - /// The object to locate in the TransformingReadOnlyCollection. - /// - /// - /// The index of item if found in the list; otherwise, -1. - /// - int IList.IndexOf(ExposedItemType item) { - return IndexOf(item); - } - - /// - /// Inserts an item to the TransformingReadOnlyCollection at the specified index. - /// - /// - /// The zero-based index at which item should be inserted. - /// - /// - /// The object to insert into the TransformingReadOnlyCollection - /// - /// - /// The TransformingReadOnlyCollection is read-only. - /// - /// - /// index is not a valid index in the TransformingReadOnlyCollection. - /// - void IList.Insert(int index, ExposedItemType item) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// - /// Removes the TransformingReadOnlyCollection item at the specified index. - /// - /// The zero-based index of the item to remove. - /// - /// The TransformingReadOnlyCollection is read-only. - /// - /// - /// Index is not a valid index in the TransformingReadOnlyCollection. - /// - void IList.RemoveAt(int index) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// Gets or sets the element at the specified index. - /// The zero-based index of the element to get or set. - /// The element at the specified index. - /// - /// Index is not a valid index in the TransformingReadOnlyCollection. - /// - /// - /// The property is set and the TransformingReadOnlyCollection is read-only - /// - ExposedItemType IList.this[int index] { - get { - return this[index]; - } - set { - throw new NotSupportedException("The collection is ready-only"); - } - } - - #endregion - - #region ICollection Members - - /// Adds an item to the TransformingReadOnlyCollection. - /// The object to add to the TransformingReadOnlyCollection - /// - /// The TransformingReadOnlyCollection is read-only. - /// - void ICollection.Add(ExposedItemType item) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// Removes all items from the TransformingReadOnlyCollection - /// - /// The TransformingReadOnlyCollection is read-only. - /// - void ICollection.Clear() { - throw new NotSupportedException("The collection is ready-only"); - } - - /// - /// Determines whether the TransformingReadOnlyCollection contains a specific value. - /// - /// - /// The object to locate in the TransformingReadOnlyCollection. - /// - /// - /// True if item is found in the TransformingReadOnlyCollection; otherwise, false. - /// - bool ICollection.Contains(ExposedItemType item) { - return Contains(item); - } - - /// - /// Copies the elements of the TransformingReadOnlyCollection to an System.Array, - /// starting at a particular System.Array index. - /// - /// - /// The one-dimensional System.Array that is the destination of the elements - /// copied from TransformingReadOnlyCollection. The System.Array must have - /// zero-based indexing. - /// - /// - /// The zero-based index in array at which copying begins - /// - /// - /// ArrayIndex is less than 0. - /// - /// - /// Array is null. - /// - /// - /// Array is multidimensional or arrayIndex is equal to or greater than the - /// length of array or the number of elements in the source - /// TransformingReadOnlyCollection is greater than the available - /// space from arrayIndex to the end of the destination array or type T cannot - /// be cast automatically to the type of the destination array. - /// - void ICollection.CopyTo(ExposedItemType[] array, int arrayIndex) { - CopyTo(array, arrayIndex); - } - - /// - /// Removes the first occurrence of a specific object from the - /// TransformingReadOnlyCollection. - /// - /// - /// The object to remove from the TransformingReadOnlyCollection - /// - /// - /// True if item was successfully removed from the TransformingReadOnlyCollection; - /// otherwise, false. This method also returns false if item is not found in the - /// original TransformingReadOnlyCollection. - /// - /// - /// The TransformingReadOnlyCollection is read-only. - /// - bool ICollection.Remove(ExposedItemType item) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// - /// The number of elements contained in the TransformingReadOnlyCollection. - /// - int ICollection.Count { - get { return Count; } - } - - /// - /// A value indicating whether the TransformingReadOnlyCollection is read-only. - /// - bool ICollection.IsReadOnly { - get { return true; } - } - - #endregion - - #region IEnumerable Members - - /// Returns an enumerator that iterates through the collection. - /// - /// A System.Collections.Generic.IEnumerator<ExposedItemType> that can be used - /// to iterate through the collection. - /// - IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator(); - } - - #endregion - - #region IEnumerable Members - - /// Returns an enumerator that iterates through a collection. - /// - /// A System.Collections.IEnumerator object that can be used to iterate through - /// the collection. - /// - IEnumerator IEnumerable.GetEnumerator() { - return GetEnumerator(); - } - - #endregion - - #region IList Members - - /// Adds an item to the TransformingReadOnlyCollection. - /// - /// The System.Object to add to the TransformingReadOnlyCollection. - /// - /// The position into which the new element was inserted. - /// - /// The System.Collections.IList is read-only or the TransformingReadOnlyCollection - /// has a fixed size. - /// - int IList.Add(object value) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// Removes all items from the TransformingReadOnlyCollection. - /// - /// The TransformingReadOnlyCollection is read-only. - /// - void IList.Clear() { - throw new NotSupportedException("The collection is ready-only"); - } - - /// - /// Determines whether the TransformingReadOnlyCollection contains a specific value. - /// - /// - /// The System.Object to locate in the TransformingReadOnlyCollection. - /// - /// - /// True if the System.Object is found in the TransformingReadOnlyCollection; - /// otherwise, false. - /// - bool IList.Contains(object value) { - return Contains((ExposedItemType)value); - } - - /// - /// Determines the index of a specific item in the TransformingReadOnlyCollection. - /// - /// - /// The System.Object to locate in the TransformingReadOnlyCollection. - /// - /// - /// The index of value if found in the list; otherwise, -1. - /// - int IList.IndexOf(object value) { - return IndexOf((ExposedItemType)value); - } - - /// - /// Inserts an item to the TransformingReadOnlyCollection at the specified index. - /// - /// - /// The zero-based index at which value should be inserted. - /// - /// - /// The System.Object to insert into the TransformingReadOnlyCollection. - /// - /// - /// Index is not a valid index in the TransformingReadOnlyCollection. - /// - /// - /// The System.Collections.IList is read-only or the TransformingReadOnlyCollection - /// has a fixed size. - /// - /// - /// Value is null reference in the TransformingReadOnlyCollection. - /// - void IList.Insert(int index, object value) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// - /// A value indicating whether the TransformingReadOnlyCollection has a fixed - /// size. - /// - bool IList.IsFixedSize { - get { return true; } - } - - /// - /// A value indicating whether the index is not a valid index in the is read-only. - /// - bool IList.IsReadOnly { - get { return true; } - } - - /// - /// Removes the first occurrence of a specific object from the - /// TransformingReadOnlyCollection. - /// - /// - /// The System.Object to remove from the TransformingReadOnlyCollection. - /// - /// - /// The TransformingReadOnlyCollection is read-only or the - /// TransformingReadOnlyCollection has a fixed size. - /// - void IList.Remove(object value) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// - /// Removes the TransformingReadOnlyCollection item at the specified index. - /// - /// The zero-based index of the item to remove. - /// - /// Index is not a valid index in the TransformingReadOnlyCollection. - /// - /// - /// The TransformingReadOnlyCollection is read-only or the - /// TransformingReadOnlyCollection has a fixed size. - /// - void IList.RemoveAt(int index) { - throw new NotSupportedException("The collection is ready-only"); - } - - /// Gets or sets the element at the specified index. - /// The zero-based index of the element to get or set. - /// The element at the specified index - /// - /// Index is not a valid index in the TransformingReadOnlyCollection - /// - /// - /// The property is set and the TransformingReadOnlyCollection is read-only. - /// - object IList.this[int index] { - get { - return this[index]; - } - set { - throw new NotSupportedException("The collection is ready-only"); - } - } - - #endregion - - #region ICollection Members - - /// - /// Copies the elements of the TransformingReadOnlyCollection to an System.Array, - /// starting at a particular System.Array index. - /// - /// - /// The one-dimensional System.Array that is the destination of the elements - /// copied from TransformingReadOnlyCollection. The System.Array must have zero-based - /// indexing. - /// - /// The zero-based index in array at which copying begins. - /// - /// Array is null. - /// - /// - /// Index is less than zero. - /// - /// - /// Array is multidimensional or index is equal to or greater than the length - /// of array or the number of elements in the source TransformingReadOnlyCollection - /// is greater than the available space from index to the end of the destination - /// array. - /// - /// - /// The type of the source TransformingReadOnlyCollection cannot be cast - /// automatically to the type of the destination array. - /// - void ICollection.CopyTo(Array array, int index) { - CopyTo((ExposedItemType[])array, index); - } - - /// - /// The number of elements contained in the TransformingReadOnlyCollection. - /// - int ICollection.Count { - get { return Count; } - } - - /// - /// A value indicating whether access to the TransformingReadOnlyCollection - /// is synchronized (thread safe). - /// - bool ICollection.IsSynchronized { - get { return false; } - } - - /// - /// An object that can be used to synchronize access to the - /// TransformingReadOnlyCollection. - /// - object ICollection.SyncRoot { - get { - if(this.syncRoot == null) { - ICollection is2 = this.items as ICollection; - if(is2 != null) { - this.syncRoot = is2.SyncRoot; - } else { - Interlocked.CompareExchange(ref this.syncRoot, new object(), null); - } - } - - return this.syncRoot; - } - } - - #endregion - /// Items being transformed upon exposure by this collection private IList items; /// Synchronization root for threaded accesses to this collection diff --git a/Source/Collections/UnintrusivePriorityQueue.cs b/Source/Collections/UnintrusivePriorityQueue.cs index 8282bc8..ded3c61 100644 --- a/Source/Collections/UnintrusivePriorityQueue.cs +++ b/Source/Collections/UnintrusivePriorityQueue.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; using System.Collections; diff --git a/Source/Packing/OutOfSpaceException.cs b/Source/Packing/OutOfSpaceException.cs new file mode 100644 index 0000000..afbe9f5 --- /dev/null +++ b/Source/Packing/OutOfSpaceException.cs @@ -0,0 +1,58 @@ +#region CPL License +/* +Nuclex Framework +Copyright (C) 2002-2007 Nuclex Development Labs + +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 + +using System; +using System.Collections.Generic; + +namespace Nuclex.Support.Packing { + + /// Insufficient space left in packing area to contain a given object + /// + /// An exception being sent to you from deep space. Erm, no, wait, it's an exception + /// that occurs when a packing algorithm runs out of space and is unable to fit + /// the object you tried to pack into the remaining packing area. + /// + [Serializable] + public class OutOfSpaceException : ApplicationException { + + /// Initializes the exception + public OutOfSpaceException() { } + + /// Initializes the exception with an error message + /// Error message describing the cause of the exception + public OutOfSpaceException(string message) : base(message) { } + + /// Initializes the exception as a followup exception + /// Error message describing the cause of the exception + /// Preceding exception that has caused this exception + public OutOfSpaceException(string message, Exception inner) : base(message, inner) { } + + /// Initializes the exception from its serialized state + /// Contains the serialized fields of the exception + /// Additional environmental informations + protected OutOfSpaceException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context + ) + : base(info, context) { } + + } + +} // namespace Nuclex.Support.Packing diff --git a/Source/Packing/RectanglePacker.cs b/Source/Packing/RectanglePacker.cs index 738d79e..5617376 100644 --- a/Source/Packing/RectanglePacker.cs +++ b/Source/Packing/RectanglePacker.cs @@ -17,6 +17,7 @@ You should have received a copy of the IBM Common Public License along with this library */ #endregion + using System; using System.Collections.Generic; @@ -54,7 +55,7 @@ namespace Nuclex.Support.Packing { Point point; if(!TryPack(rectangleWidth, rectangleHeight, out point)) - throw new Exception("Rectangle does not fit in packing area"); + throw new OutOfSpaceException("Rectangle does not fit in packing area"); return point; } diff --git a/Source/Serialization/BinarySerializer.cs b/Source/Serialization/BinarySerializer.cs index 18aa7cc..4683633 100644 --- a/Source/Serialization/BinarySerializer.cs +++ b/Source/Serialization/BinarySerializer.cs @@ -41,7 +41,7 @@ namespace Nuclex.Support.Serialization { // Read and verify the version of the file format this was saved in int version = reader.ReadInt32(); if(version > 1) - throw new InvalidOperationException("File format is too new"); + throw new InvalidOperationException("File format mismatch"); // Read all the serialized blueprints int count = reader.ReadInt32();