Pool can now be used with types not derived from IRecyclable and/or without a public default constructor; consolidated type-related helper methods into a common helper class (TypeHelper.cs); optimized GetFieldInfosIncludingBaseClasses() method

git-svn-id: file:///srv/devel/repo-conversion/nusu@268 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-08 11:05:20 +00:00
parent 24439da822
commit c91a082e84
18 changed files with 277 additions and 292 deletions

View file

@ -30,9 +30,9 @@ namespace Nuclex.Support.Collections {
/// priority data type implements the IComparable interface, the user does not
/// even
/// </remarks>
public class PairPriorityQueue<TPriority, TItem>
: ICollection, IEnumerable<PriorityItemPair<TPriority, TItem>> {
public class PairPriorityQueue<TPriority, TItem> :
ICollection, IEnumerable<PriorityItemPair<TPriority, TItem>> {
#region class PairComparer
/// <summary>Compares two priority queue entries based on their priority</summary>

View file

@ -98,13 +98,12 @@ namespace Nuclex.Support.Collections {
// 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;
// If the item is disposable, destroy it now
if(disposable != null)
if(disposable != null) {
disposable.Dispose();
}
}
base.ClearItems();

View file

@ -45,18 +45,35 @@ namespace Nuclex.Support.Collections {
/// automatically call its IRecyclable.Recycle() method.
/// </para>
/// </remarks>
public class Pool<TItem> where TItem : class, new() {
public class Pool<TItem> {
/// <summary>Default number of recyclable objects the pool will store</summary>
public const int DefaultPoolSize = 64;
/// <summary>Initializes a new pool using the default capacity</summary>
public Pool() : this(DefaultPoolSize) { }
/// <param name="createNewDelegate">Delegate that will be used to create new items</param>
/// <param name="recycleDelegate">Delegate that will be used to recycle items</param>
public Pool(Func<TItem> createNewDelegate = null, Action<TItem> recycleDelegate = null) :
this(DefaultPoolSize, createNewDelegate, recycleDelegate) { }
/// <summary>Initializes a new pool using a user-specified capacity</summary>
/// <param name="capacity">Capacity of the pool</param>
public Pool(int capacity) {
/// <param name="createNewDelegate">Delegate that will be used to create new items</param>
/// <param name="recycleDelegate">Delegate that will be used to recycle items</param>
public Pool(
int capacity, Func<TItem> createNewDelegate = null, Action<TItem> recycleDelegate = null
) {
Capacity = capacity;
if(createNewDelegate == null) {
createNewDelegate = new Func<TItem>(Activator.CreateInstance<TItem>);
}
if(recycleDelegate == null) {
recycleDelegate = new Action<TItem>(callRecycleIfSupported);
}
this.createNewDelegate = createNewDelegate;
this.recycleDelegate = recycleDelegate;
}
/// <summary>
@ -68,7 +85,7 @@ namespace Nuclex.Support.Collections {
if(this.items.Count > 0) {
return this.items.Dequeue();
} else {
return new TItem();
return this.createNewDelegate();
}
}
}
@ -82,13 +99,14 @@ namespace Nuclex.Support.Collections {
// Call Recycle() when the object is redeemed (instead of when it leaves
// the pool again) in order to eliminate any references the object may hold
// to other objects.
callRecycleIfSupported(item);
this.recycleDelegate(item);
lock(this) {
if(this.items.Count < this.capacity) {
this.items.Enqueue(item);
}
}
}
/// <summary>Number of objects the pool can retain</summary>
@ -125,6 +143,8 @@ namespace Nuclex.Support.Collections {
/// Required because the Queue class doesn't allow this value to be retrieved
/// </remarks>
private int capacity;
private Func<TItem> createNewDelegate;
private Action<TItem> recycleDelegate;
}