1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | namespace KerbalEngineer.VesselSimulator { using System.Collections.Generic; public class Pool<T> where T : new() { private static List<T> available = new List<T>(); private static List<T> inUse = new List<T>(); public static int PoolCount { get { return available.Count + inUse.Count; } } public static T GetPoolObject() { T obj; if (available.Count > 0) { obj = available[0]; available.RemoveAt(0); } else { obj = new T(); } inUse.Add(obj); return obj; } public static void Release(T obj) { if (inUse.Contains(obj)) { inUse.Remove(obj); available.Add(obj); } } public static void Release(List<T> objList) { for (int i = 0; i < objList.Count; ++i) { Release(objList[i]); } } public static void ReleaseAll() { for (int i = 0; i < inUse.Count; ++i) { available.Add(inUse[i]); } inUse.Clear(); } } } |