Added six separate constructors to avoid default parameters (which aren't supported on Windows Phone 7 or the Xbox 360)

git-svn-id: file:///srv/devel/repo-conversion/nusu@269 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2012-03-08 11:31:24 +00:00
parent c91a082e84
commit 58a1652749
2 changed files with 49 additions and 15 deletions

View File

@ -48,32 +48,43 @@ namespace Nuclex.Support.Collections {
#endregion // class TestClass
#region class NoDefaultConstructor
/// <summary>Used to test the pool</summary>
private class NoDefaultConstructor {
/// <summary>Private constructor so no instances can be created</summary>
private NoDefaultConstructor() { }
}
#endregion // class NoDefaultConstructor
/// <summary>
/// Verifies that the pool can return newly constructed objects
/// </summary>
[Test]
public void TestGet() {
public void NewInstancesCanBeObtained() {
Pool<TestClass> pool = new Pool<TestClass>();
Assert.IsNotNull(pool.Get());
}
/// <summary>
/// Verifies that the pool will return a recycled object if one is available
/// Verifies that an exception is thrown if the pool's default instance creator is used
/// on a type that doesn't have a default constructor
/// </summary>
[Test]
public void TestGetRecycled() {
Pool<TestClass> pool = new Pool<TestClass>();
pool.Redeem(new TestClass());
TestClass test = pool.Get();
Assert.IsTrue(test.Recycled);
public void UsingDefaultInstanceCreatorRequiresDefaultConstructor() {
Assert.Throws<ArgumentException>(
delegate() { new Pool<NoDefaultConstructor>(); }
);
}
/// <summary>
/// Tests whether the pool can redeem objects that are no longer used
/// </summary>
[Test]
public void TestRedeem() {
public void InstancesCanBeRedeemed() {
Pool<TestClass> pool = new Pool<TestClass>();
pool.Redeem(new TestClass());
}
@ -82,7 +93,7 @@ namespace Nuclex.Support.Collections {
/// Tests whether the Recycle() method is called at the appropriate time
/// </summary>
[Test]
public void TestRecycle() {
public void RedeemedItemsWillBeRecycled() {
Pool<TestClass> pool = new Pool<TestClass>();
TestClass x = new TestClass();
@ -93,7 +104,7 @@ namespace Nuclex.Support.Collections {
/// <summary>Verifies that the pool's Capacity is applied correctly</summary>
[Test]
public void TestPoolSize() {
public void PoolCapacityCanBeAdjusted() {
Pool<TestClass> pool = new Pool<TestClass>(123);
Assert.AreEqual(123, pool.Capacity);
pool.Capacity = 321;

View File

@ -50,22 +50,45 @@ namespace Nuclex.Support.Collections {
/// <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, null, null) { }
/// <summary>Initializes a new pool using the default capacity</summary>
/// <param name="createNewDelegate">Delegate that will be used to create new items</param>
public Pool(Func<TItem> createNewDelegate) :
this(DefaultPoolSize, createNewDelegate, null) { }
/// <summary>Initializes a new pool using the default capacity</summary>
/// <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) :
public Pool(Func<TItem> createNewDelegate, Action<TItem> recycleDelegate) :
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) :
this(capacity, null, null) { }
/// <summary>Initializes a new pool using a user-specified capacity</summary>
/// <param name="capacity">Capacity of the pool</param>
/// <param name="createNewDelegate">Delegate that will be used to create new items</param>
public Pool(int capacity, Func<TItem> createNewDelegate) :
this(capacity, createNewDelegate, null) { }
/// <summary>Initializes a new pool using a user-specified capacity</summary>
/// <param name="capacity">Capacity of the pool</param>
/// <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
) {
public Pool(int capacity, Func<TItem> createNewDelegate, Action<TItem> recycleDelegate) {
Capacity = capacity;
if(createNewDelegate == null) {
if(!typeof(TItem).HasDefaultConstructor()) {
throw new ArgumentException(
"Type " + typeof(TItem).Name + " has no default constructor and " +
"requires a custom 'create instance' delegate"
);
}
createNewDelegate = new Func<TItem>(Activator.CreateInstance<TItem>);
}
if(recycleDelegate == null) {