< Summary

Class:GDX.Collections.Pooling.ManagedPools
Assembly:GDX
File(s):D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ManagedPools.cs
Covered lines:28
Uncovered lines:50
Coverable lines:78
Total lines:221
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)

D:/BuildAgent/work/GDX-Documentation/Projects/GDX_Development/Packages/com.dotbunny.gdx/GDX/Collections/Pooling/ManagedPools.cs

#LineLine coverage
 1// Copyright (c) 2020-2022 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;
 7
 8namespace GDX.Collections.Pooling
 9{
 10    /// <summary>
 11    ///     A managed pooling system implementation primarily meant for the object oriented patterns, based on the C# ba
 12    ///     object.
 13    /// </summary>
 14    [VisualScriptingCompatible(1)]
 15    public static class ManagedPools
 16    {
 17        /// <summary>
 18        ///     The last issued pool key used by internal dictionary's <see cref="KeyValuePair{TKey,TValue}" /> when ref
 19        ///     <see cref="IManagedPool" />.
 20        /// </summary>
 21        /// <remarks>
 22        ///     This value resets on domain reload, and as such the keys should not be relied on through any sort of
 23        ///     serialization (including networking) or session based process.
 24        /// </remarks>
 25        static uint s_LastPoolKey;
 26
 27        /// <summary>
 28        ///     An internal dictionary containing the <see cref="IManagedPool" />s, uniquely indexed by constant ticket-
 29        ///     system.
 30        /// </summary>
 131        static readonly Dictionary<uint, IManagedPool> k_Pools = new Dictionary<uint, IManagedPool>();
 32
 33        /// <summary>
 34        ///     Get the next available pool key.
 35        /// </summary>
 36        /// <remarks>Increments the previously issued stored value, and returns the new value.</remarks>
 37        /// <returns>A unique pool identifying key.</returns>
 38        public static uint GetNextPoolKey()
 1439        {
 1440            s_LastPoolKey++;
 1441            return s_LastPoolKey;
 1442        }
 43
 44        /// <summary>
 45        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />.
 46        /// </summary>
 47        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 48        /// <returns>An <see cref="IManagedPool" /> identified by the provided <paramref name="key" />.</returns>
 49        public static IManagedPool GetPool(uint key)
 050        {
 051            return k_Pools[key];
 052        }
 53
 54        /// <summary>
 55        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />.
 56        /// </summary>
 57        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 58        /// <typeparam name="T">The type of the pool, used to cast the return pool</typeparam>
 59        /// <returns>A type casted pool identified by the provided <paramref name="key" />.</returns>
 60        public static T GetPool<T>(uint key)
 061        {
 062            return (T)k_Pools[key];
 063        }
 64
 65        /// <summary>
 66        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />, first checking if it 
 67        ///     registered.
 68        /// </summary>
 69        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 70        /// <returns>An <see cref="IManagedPool" /> identified by the provided <paramref name="key" />, null if not foun
 71        public static IManagedPool GetPoolWithContainsCheck(uint key)
 072        {
 073            return k_Pools.ContainsKey(key) ? k_Pools[key] : null;
 074        }
 75
 76        /// <summary>
 77        ///     Get a registered <see cref="IManagedPool" /> based on its <paramref name="key" />, first checking if it 
 78        ///     registered.
 79        /// </summary>
 80        /// <param name="key">The unique key to use when looking for the <see cref="IManagedPool" />.</param>
 81        /// <typeparam name="T">The type of the pool, used to cast the return pool</typeparam>
 82        /// <returns>A type casted pool identified by the provided <paramref name="key" />, null if not found.</returns>
 83        public static T GetPoolWithContainsCheck<T>(uint key) where T : class
 084        {
 085            if (k_Pools.ContainsKey(key))
 086            {
 087                return (T)k_Pools[key];
 88            }
 89
 090            return null;
 091        }
 92
 93        /// <summary>
 94        ///     Is an <see cref="IManagedPool" /> registered with the provided <paramref name="key" />?
 95        /// </summary>
 96        /// <param name="key">A unique pool key</param>
 97        /// <returns>true if a pool is found registered with this system, false otherwise.</returns>
 98        public static bool HasPool(uint key)
 199        {
 1100            return k_Pools.ContainsKey(key);
 1101        }
 102
 103        /// <summary>
 104        ///     Attempts to return all spawned items to their original <see cref="IManagedPool" />s.
 105        /// </summary>
 106        /// <param name="shouldShrink">Should the pool be shrunk (destroying created items) to its original set minimum 
 107        public static void ReturnAll(bool shouldShrink = true)
 0108        {
 0109            foreach (KeyValuePair<uint, IManagedPool> p in k_Pools)
 0110            {
 0111                p.Value.ReturnAll(shouldShrink);
 0112            }
 0113        }
 114
 115        /// <summary>
 116        ///     Register a <see cref="IManagedPool" /> with the global management system.
 117        /// </summary>
 118        /// <param name="managedPool">Target <see cref="IManagedPool" /></param>
 119        public static void Register(IManagedPool managedPool)
 14120        {
 121            // Add to numerical index
 14122            uint key = managedPool.GetKey();
 14123            if (!k_Pools.ContainsKey(key))
 14124            {
 14125                k_Pools.Add(key, managedPool);
 14126            }
 127            else
 0128            {
 0129                Trace.Output(Trace.TraceLevel.Error,
 130                    "A managed pool attempted to register itself with the ManagedPools, but the provided key is already 
 0131            }
 14132        }
 133
 134        /// <summary>
 135        ///     Execute <see cref="IManagedPool.TearDown()" /> (destroying contents) on all registered <see cref="IManag
 136        ///     which have been flagged to accept it, evaluated by <see cref="IManagedPool.IsAllowedManagedTearDown()" /
 137        /// </summary>
 138        /// <remarks>
 139        ///     This will unregister the <see cref="IManagedPool" /> itself as well.
 140        /// </remarks>
 141        /// <param name="forceAll">
 142        ///     Execute <see cref="IManagedPool.TearDown()" /> regardless of the
 143        ///     <see cref="IManagedPool.IsAllowedManagedTearDown()" /> response.
 144        /// </param>
 145        public static void TearDown(bool forceAll = false)
 0146        {
 147            // Make removal buffer
 0148            SimpleList<uint> removeKeyBuffer = new SimpleList<uint>(k_Pools.Count);
 149
 150            // Now we pay the cost, however its during a teardown so, its less bad.
 0151            int poolCount = k_Pools.Count;
 152
 0153            foreach (KeyValuePair<uint, IManagedPool> pool in k_Pools)
 0154            {
 0155                if (!pool.Value.IsAllowedManagedTearDown() && !forceAll)
 0156                {
 0157                    continue;
 158                }
 159
 0160                pool.Value.TearDown();
 0161                removeKeyBuffer.AddUnchecked(pool.Key);
 0162            }
 163
 164            // Can't modify the dictionary while iterating over it, so here we are.
 0165            int removeCount = removeKeyBuffer.Count;
 0166            for (int r = 0; r < removeCount; r++)
 0167            {
 0168                k_Pools.Remove(removeKeyBuffer.Array[r]);
 0169            }
 170
 0171            Trace.Output(Trace.TraceLevel.Info,
 172                $"[PoolSystem::TearDown] Removed {removeCount.ToString()}/{poolCount.ToString()}");
 0173        }
 174
 175        /// <summary>
 176        ///     Attempt to get an <see cref="IManagedPool" /> based on the <paramref name="baseObject" />.
 177        /// </summary>
 178        /// <param name="baseObject">The <see cref="object" /> which was used to create the pool.</param>
 179        /// <param name="pool">The first found <see cref="IManagedPool" /> created with <paramref name="baseObject" />.<
 180        /// <returns>true/false if an <see cref="IManagedPool" /> was found.</returns>
 181        public static bool TryGetFirstPool(object baseObject, out IManagedPool pool)
 14182        {
 14183            pool = null;
 42184            foreach (KeyValuePair<uint, IManagedPool> kvp in k_Pools)
 0185            {
 0186                if (kvp.Value.GetBaseObject() != baseObject)
 0187                {
 0188                    continue;
 189                }
 190
 0191                pool = kvp.Value;
 0192                return true;
 193            }
 194
 14195            return false;
 14196        }
 197
 198        /// <summary>
 199        ///     Unregister a <see cref="IManagedPool" /> from with the management system.
 200        /// </summary>
 201        /// <param name="managedPool">Target <see cref="IManagedPool" /></param>
 202        public static void Unregister(IManagedPool managedPool)
 28203        {
 204            // Well we cant be doing anything with that
 28205            if (managedPool == null)
 0206            {
 0207                return;
 208            }
 209
 210            // Get the apparent key
 28211            uint key = managedPool.GetKey();
 212
 213            // Checks key and the matching pool
 28214            if (k_Pools.ContainsKey(key) && k_Pools[key] == managedPool)
 14215            {
 216                // Remove it from the registry
 14217                k_Pools.Remove(key);
 14218            }
 28219        }
 220    }
 221}