diff --git a/CHANGELOG.md b/CHANGELOG.md index ebd5afda..98759947 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ _See also: [the official libGDX changelog](https://github.com/libgdx/libgdx/blob #### 1.12.1-SNAPSHOT +- **[CHANGE]** (`ktx-assets`) The `pool` factory method now attempts to reset `Poolable` objects with the default `discard` lambda +to match the default libGDX pool behavior. Pass a custom function to override it. + #### 1.12.1-rc1 - **[UPDATE]** Updated to libGDX 1.12.1. diff --git a/assets/src/main/kotlin/ktx/assets/pools.kt b/assets/src/main/kotlin/ktx/assets/pools.kt index 73b6af6e..c9a271f5 100644 --- a/assets/src/main/kotlin/ktx/assets/pools.kt +++ b/assets/src/main/kotlin/ktx/assets/pools.kt @@ -2,6 +2,7 @@ package ktx.assets import com.badlogic.gdx.utils.Disposable import com.badlogic.gdx.utils.Pool +import com.badlogic.gdx.utils.Pool.Poolable /** * Allows to use a [Pool] instance as a functional object. When invoked with no parameters, [Pool] will provide an @@ -14,7 +15,7 @@ operator fun Pool.invoke(): Type = this.obtain() /** * Allows to use a [Pool] instance as a functional object. When invoked with a parameter, [Pool] will treat the passed * parameter as an object freed to the pool. - * @param free will be returned to the pool. Might be reset if it implements the Poolable interface. + * @param free will be returned to the pool. Might be reset if it implements the [Poolable] interface. * @see Pool.free */ operator fun Pool.invoke(free: Type) = this.free(free) @@ -24,14 +25,19 @@ operator fun Pool.invoke(free: Type) = this.free(free) * @param max max amount stored in the pool. When exceeded, freed objects are no longer accepted. * @param discard invoked each time an object is rejected or removed from the pool. This might happen if an object is * freed with [Pool.free] or [Pool.freeAll] if the pool is full, or when [Pool.clear] is called. Optional, defaults - * to no operation. If the objects are [Disposable], this lambda might be used to dispose of them. + * to resetting the objects implementing the [Poolable] interface to replicate the default behavior. If the objects are + * [Disposable], this lambda should be used to dispose of them. * @param provider creates instances of the requested objects. * @return a new [Pool] instance, creating the object with the passed provider. */ inline fun pool( initialCapacity: Int = 16, max: Int = Int.MAX_VALUE, - crossinline discard: (Type) -> Unit = {}, + crossinline discard: (Type) -> Unit = { + if (it is Poolable) { + it.reset() + } + }, crossinline provider: () -> Type, ): Pool = object : Pool(initialCapacity, max) { diff --git a/assets/src/test/kotlin/ktx/assets/PoolsTest.kt b/assets/src/test/kotlin/ktx/assets/PoolsTest.kt index 2dd43ad6..df50ec18 100644 --- a/assets/src/test/kotlin/ktx/assets/PoolsTest.kt +++ b/assets/src/test/kotlin/ktx/assets/PoolsTest.kt @@ -1,9 +1,11 @@ package ktx.assets import com.badlogic.gdx.utils.Pool +import com.badlogic.gdx.utils.Pool.Poolable import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull import org.junit.Assert.assertSame +import org.junit.Assert.assertTrue import org.junit.Test /** @@ -76,6 +78,30 @@ class PoolsTest { assertEquals(listOf("Value6", "Value7", "Value8", "Value9", "Value10"), discarded) } + @Test + fun `should create new pools that reset discarded objects by default`() { + // Given: + val pool = pool(max = 1) { SamplePoolable() } + val freed = pool() + val discarded = pool() + pool.free(freed) + + // When: + pool.free(discarded) + + // Then: + assertTrue(discarded.isReset) + } + + /** + * A simple data object implementing the [Poolable] interface. + */ + private class SamplePoolable(var isReset: Boolean = false) : Poolable { + override fun reset() { + this.isReset = true + } + } + /** * Provides new [Any] instances. */