using System.Collections.Generic; namespace Lean.Pool { /// This class allows you to pool normal C# classes, for example: /// var foo = Lean.Pool.LeanClassPool.Spawn() ?? new Foo(); /// Lean.Pool.LeanClassPool.Despawn(foo); public static class LeanClassPool where T : class { // Store cache of all despanwed classes here, in a list so we can search it private static List cache = new List(); // This will either return a pooled class instance, or null public static T Spawn() { var count = cache.Count; if (count > 0) { var index = count - 1; var instance = cache[index]; cache.RemoveAt(index); return instance; } return null; } /// This will either return a pooled class instance, or null. If an instance it found, onSpawn will be called with it. NOTE: onSpawn is expected to not be null. public static T Spawn(System.Action onSpawn) { var instance = default(T); TrySpawn(onSpawn, ref instance); return instance; } public static bool TrySpawn(System.Action onSpawn, ref T instance) { var count = cache.Count; if (count > 0) { var index = count - 1; instance = cache[index]; cache.RemoveAt(index); onSpawn(instance); return true; } return false; } /// This will either return a pooled class instance, or null. /// All pooled classes will be checked with match to see if they qualify. /// NOTE: match is expected to not be null. public static T Spawn(System.Predicate match) { var instance = default(T); TrySpawn(match, ref instance); return instance; } public static bool TrySpawn(System.Predicate match, ref T instance) { var index = cache.FindIndex(match); if (index >= 0) { instance = cache[index]; cache.RemoveAt(index); return true; } return false; } /// This will either return a pooled class instance, or null. /// All pooled classes will be checked with match to see if they qualify. /// If an instance it found, onSpawn will be called with it. /// NOTE: match is expected to not be null. /// NOTE: onSpawn is expected to not be null. public static T Spawn(System.Predicate match, System.Action onSpawn) { var instance = default(T); TrySpawn(match, onSpawn, ref instance); return instance; } public static bool TrySpawn(System.Predicate match, System.Action onSpawn, ref T instance) { var index = cache.FindIndex(match); if (index >= 0) { instance = cache[index]; cache.RemoveAt(index); onSpawn(instance); return true; } return false; } /// This will pool the passed class instance. public static void Despawn(T instance) { if (instance != null) { cache.Add(instance); } } /// This will pool the passed class instance. /// If you need to perform despawning code then you can do that via onDespawn. public static void Despawn(T instance, System.Action onDespawn) { if (instance != null) { onDespawn(instance); cache.Add(instance); } } } }