< Summary

Class:GDX.Collections.Pooling.ManagedPools
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ManagedPools.cs
Covered lines:28
Uncovered lines:50
Coverable lines:78
Total lines:222
Line coverage:35.8% (28 of 78)
Covered branches:0
Total branches:0
Covered methods:6
Total methods:12
Method coverage:50% (6 of 12)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
ManagedPools()0%110100%
GetNextPoolKey()0%110100%
GetPool(...)0%2100%
GetPool[T](...)0%2100%
GetPoolWithContainsCheck(...)0%12300%
GetPoolWithContainsCheck[T](...)0%6200%
HasPool(...)0%110100%
ReturnAll(...)0%6200%
Register(...)0%2.112070%
TearDown(...)0%30500%
TryGetFirstPool(...)0%4.123050%
Unregister(...)0%4.134080%

File(s)

./Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ManagedPools.cs

#LineLine coverage
 1// Copyright (c) 2020-2024 dotBunny Inc.
 2// dotBunny licenses this file to you under the BSL-1.0 license.
 3// See the LICENSE file in the project root for more information.
 4
 5using System.Collections.Generic;
 6using GDX.Collections.Generic;
 7using UnityEngine;
 8
 9namespace GDX.Collections.Pooling
 10{
 11    /// <summary>
 12    ///     A managed pooling system implementation primarily meant for the object oriented patterns, based on the C# ba
 13    ///     object.
 14    /// </summary>
 15    [VisualScriptingCompatible(1)]
 16    public static class ManagedPools
 17    {
 18        /// <summary>
 19        ///     The last issued pool key used by internal dictionary's <see cref="KeyValuePair{TKey,TValue}" /> when ref
 20        ///     <see cref="IManagedPool" />.
 21        /// </summary>
 22        /// <remarks>
 23        ///     This value resets on domain reload, and as such the keys should not be relied on through any sort of
 24        ///     serialization (including networking) or session based process.
 25        /// </remarks>
 26        static uint s_LastPoolKey;
 27
 28        /// <summary>
 29        ///     An internal dictionary containing the <see cref="IManagedPool" />s, uniquely indexed by constant ticket-
 30        ///     system.
 31        /// </summary>
 132        static readonly Dictionary<uint, IManagedPool> k_Pools = new Dictionary<uint, IManagedPool>();
 33
 34        /// <summary>
 35        ///     Get the next available pool key.
 36        /// </summary>
 37        /// <remarks>Increments the previously issued stored value, and returns the new value.</remarks>
 38        /// <returns>A unique pool identifying key.</returns>
 39        public static uint GetNextPoolKey()
 1440        {
 1441            s_LastPoolKey++;
 1442            return s_LastPoolKey;
 1443        }
 44
 45        /// <summary>
 46        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />.
 47        /// </summary>
 48        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 49        /// <returns>An <see cref="IManagedPool" /> identified by the provided <paramref name="key" />.</returns>
 50        public static IManagedPool GetPool(uint key)
 051        {
 052            return k_Pools[key];
 053        }
 54
 55        /// <summary>
 56        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />.
 57        /// </summary>
 58        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 59        /// <typeparam name="T">The type of the pool, used to cast the return pool</typeparam>
 60        /// <returns>A type casted pool identified by the provided <paramref name="key" />.</returns>
 61        public static T GetPool<T>(uint key)
 062        {
 063            return (T)k_Pools[key];
 064        }
 65
 66        /// <summary>
 67        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />, first checking if it 
 68        ///     registered.
 69        /// </summary>
 70        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 71        /// <returns>An <see cref="IManagedPool" /> identified by the provided <paramref name="key" />, null if not foun
 72        public static IManagedPool GetPoolWithContainsCheck(uint key)
 073        {
 074            return k_Pools.ContainsKey(key) ? k_Pools[key] : null;
 075        }
 76
 77        /// <summary>
 78        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />, first checking if it 
 79        ///     registered.
 80        /// </summary>
 81        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 82        /// <typeparam name="T">The type of the pool, used to cast the return pool</typeparam>
 83        /// <returns>A type casted pool identified by the provided <paramref name="key" />, null if not found.</returns>
 84        public static T GetPoolWithContainsCheck<T>(uint key) where T : class
 085        {
 086            if (k_Pools.ContainsKey(key))
 087            {
 088                return (T)k_Pools[key];
 89            }
 90
 091            return null;
 092        }
 93
 94        /// <summary>
 95        ///     Is an <see cref="IManagedPool" /> registered with the provided <paramref name="key" />?
 96        /// </summary>
 97        /// <param name="key">A unique pool key</param>
 98        /// <returns>true if a pool is found registered with this system, false otherwise.</returns>
 99        public static bool HasPool(uint key)
 1100        {
 1101            return k_Pools.ContainsKey(key);
 1102        }
 103
 104        /// <summary>
 105        ///     Attempts to return all spawned items to their original <see cref="IManagedPool" />s.
 106        /// </summary>
 107        /// <param name="shouldShrink">Should the pool be shrunk (destroying created items) to its original set minimum 
 108        public static void ReturnAll(bool shouldShrink = true)
 0109        {
 0110            foreach (KeyValuePair<uint, IManagedPool> p in k_Pools)
 0111            {
 0112                p.Value.ReturnAll(shouldShrink);
 0113            }
 0114        }
 115
 116        /// <summary>
 117        ///     Register a <see cref="IManagedPool" /> with the global management system.
 118        /// </summary>
 119        /// <param name="managedPool">Target <see cref="IManagedPool" /></param>
 120        public static void Register(IManagedPool managedPool)
 14121        {
 122            // Add to numerical index
 14123            uint key = managedPool.GetKey();
 14124            if (!k_Pools.ContainsKey(key))
 14125            {
 14126                k_Pools.Add(key, managedPool);
 14127            }
 128            else
 0129            {
 0130                Debug.LogError(
 131                    "A managed pool attempted to register itself with the ManagedPools, but the provided key is already 
 0132            }
 14133        }
 134
 135        /// <summary>
 136        ///     Execute <see cref="IManagedPool.TearDown()" /> (destroying contents) on all registered <see cref="IManag
 137        ///     which have been flagged to accept it, evaluated by <see cref="IManagedPool.IsAllowedManagedTearDown()" /
 138        /// </summary>
 139        /// <remarks>
 140        ///     This will unregister the <see cref="IManagedPool" /> itself as well.
 141        /// </remarks>
 142        /// <param name="forceAll">
 143        ///     Execute <see cref="IManagedPool.TearDown()" /> regardless of the
 144        ///     <see cref="IManagedPool.IsAllowedManagedTearDown()" /> response.
 145        /// </param>
 146        public static void TearDown(bool forceAll = false)
 0147        {
 148            // Make removal buffer
 0149            SimpleList<uint> removeKeyBuffer = new SimpleList<uint>(k_Pools.Count);
 150
 151            // Now we pay the cost, however its during a teardown so, its less bad.
 0152            int poolCount = k_Pools.Count;
 153
 0154            foreach (KeyValuePair<uint, IManagedPool> pool in k_Pools)
 0155            {
 0156                if (!pool.Value.IsAllowedManagedTearDown() && !forceAll)
 0157                {
 0158                    continue;
 159                }
 160
 0161                pool.Value.TearDown();
 0162                removeKeyBuffer.AddUnchecked(pool.Key);
 0163            }
 164
 165            // Can't modify the dictionary while iterating over it, so here we are.
 0166            int removeCount = removeKeyBuffer.Count;
 0167            for (int r = 0; r < removeCount; r++)
 0168            {
 0169                k_Pools.Remove(removeKeyBuffer.Array[r]);
 0170            }
 171
 0172            Debug.Log(
 173                $"[PoolSystem::TearDown] Removed {removeCount.ToString()}/{poolCount.ToString()}");
 0174        }
 175
 176        /// <summary>
 177        ///     Attempt to get an <see cref="IManagedPool" /> based on the <paramref name="baseObject" />.
 178        /// </summary>
 179        /// <param name="baseObject">The <see cref="object" /> which was used to create the pool.</param>
 180        /// <param name="pool">The first found <see cref="IManagedPool" /> created with <paramref name="baseObject" />.<
 181        /// <returns>true/false if an <see cref="IManagedPool" /> was found.</returns>
 182        public static bool TryGetFirstPool(object baseObject, out IManagedPool pool)
 14183        {
 14184            pool = null;
 42185            foreach (KeyValuePair<uint, IManagedPool> kvp in k_Pools)
 0186            {
 0187                if (kvp.Value.GetBaseObject() != baseObject)
 0188                {
 0189                    continue;
 190                }
 191
 0192                pool = kvp.Value;
 0193                return true;
 194            }
 195
 14196            return false;
 14197        }
 198
 199        /// <summary>
 200        ///     Unregister a <see cref="IManagedPool" /> from with the management system.
 201        /// </summary>
 202        /// <param name="managedPool">Target <see cref="IManagedPool" /></param>
 203        public static void Unregister(IManagedPool managedPool)
 14204        {
 205            // Well we cant be doing anything with that
 14206            if (managedPool == null)
 0207            {
 0208                return;
 209            }
 210
 211            // Get the apparent key
 14212            uint key = managedPool.GetKey();
 213
 214            // Checks key and the matching pool
 14215            if (k_Pools.ContainsKey(key) && k_Pools[key] == managedPool)
 14216            {
 217                // Remove it from the registry
 14218                k_Pools.Remove(key);
 14219            }
 14220        }
 221    }
 222}

Coverage by test methods















Methods/Properties

ManagedPools()
GetNextPoolKey()
GetPool(System.UInt32)
GetPool[T](System.UInt32)
GetPoolWithContainsCheck(System.UInt32)
GetPoolWithContainsCheck[T](System.UInt32)
HasPool(System.UInt32)
ReturnAll(System.Boolean)
Register(GDX.Collections.Pooling.IManagedPool)
TearDown(System.Boolean)
TryGetFirstPool(System.Object, GDX.Collections.Pooling.IManagedPool&)
Unregister(GDX.Collections.Pooling.IManagedPool)