From 58a1652749231f0a21bf3df1300ca6e31dcedc24 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Thu, 8 Mar 2012 11:31:24 +0000 Subject: [PATCH] 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 --- Source/Collections/Pool.Test.cs | 33 ++++++++++++++++++++++----------- Source/Collections/Pool.cs | 31 +++++++++++++++++++++++++++---- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/Source/Collections/Pool.Test.cs b/Source/Collections/Pool.Test.cs index 27d1dc5..6d558d0 100644 --- a/Source/Collections/Pool.Test.cs +++ b/Source/Collections/Pool.Test.cs @@ -48,32 +48,43 @@ namespace Nuclex.Support.Collections { #endregion // class TestClass + #region class NoDefaultConstructor + + /// Used to test the pool + private class NoDefaultConstructor { + + /// Private constructor so no instances can be created + private NoDefaultConstructor() { } + + } + + #endregion // class NoDefaultConstructor + /// /// Verifies that the pool can return newly constructed objects /// [Test] - public void TestGet() { + public void NewInstancesCanBeObtained() { Pool pool = new Pool(); Assert.IsNotNull(pool.Get()); } /// - /// 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 /// [Test] - public void TestGetRecycled() { - Pool pool = new Pool(); - pool.Redeem(new TestClass()); - - TestClass test = pool.Get(); - Assert.IsTrue(test.Recycled); + public void UsingDefaultInstanceCreatorRequiresDefaultConstructor() { + Assert.Throws( + delegate() { new Pool(); } + ); } /// /// Tests whether the pool can redeem objects that are no longer used /// [Test] - public void TestRedeem() { + public void InstancesCanBeRedeemed() { Pool pool = new Pool(); pool.Redeem(new TestClass()); } @@ -82,7 +93,7 @@ namespace Nuclex.Support.Collections { /// Tests whether the Recycle() method is called at the appropriate time /// [Test] - public void TestRecycle() { + public void RedeemedItemsWillBeRecycled() { Pool pool = new Pool(); TestClass x = new TestClass(); @@ -93,7 +104,7 @@ namespace Nuclex.Support.Collections { /// Verifies that the pool's Capacity is applied correctly [Test] - public void TestPoolSize() { + public void PoolCapacityCanBeAdjusted() { Pool pool = new Pool(123); Assert.AreEqual(123, pool.Capacity); pool.Capacity = 321; diff --git a/Source/Collections/Pool.cs b/Source/Collections/Pool.cs index 75a04fc..1fd151a 100644 --- a/Source/Collections/Pool.cs +++ b/Source/Collections/Pool.cs @@ -50,22 +50,45 @@ namespace Nuclex.Support.Collections { /// Default number of recyclable objects the pool will store public const int DefaultPoolSize = 64; + /// Initializes a new pool using the default capacity + public Pool() : this(DefaultPoolSize, null, null) { } + + /// Initializes a new pool using the default capacity + /// Delegate that will be used to create new items + public Pool(Func createNewDelegate) : + this(DefaultPoolSize, createNewDelegate, null) { } + /// Initializes a new pool using the default capacity /// Delegate that will be used to create new items /// Delegate that will be used to recycle items - public Pool(Func createNewDelegate = null, Action recycleDelegate = null) : + public Pool(Func createNewDelegate, Action recycleDelegate) : this(DefaultPoolSize, createNewDelegate, recycleDelegate) { } + /// Initializes a new pool using a user-specified capacity + /// Capacity of the pool + public Pool(int capacity) : + this(capacity, null, null) { } + + /// Initializes a new pool using a user-specified capacity + /// Capacity of the pool + /// Delegate that will be used to create new items + public Pool(int capacity, Func createNewDelegate) : + this(capacity, createNewDelegate, null) { } + /// Initializes a new pool using a user-specified capacity /// Capacity of the pool /// Delegate that will be used to create new items /// Delegate that will be used to recycle items - public Pool( - int capacity, Func createNewDelegate = null, Action recycleDelegate = null - ) { + public Pool(int capacity, Func createNewDelegate, Action 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(Activator.CreateInstance); } if(recycleDelegate == null) {