using System.Text; using System.Collections.Generic; namespace WXB { internal class PoolData where T : new() { static public List bufs = new List(); static public T Get() { if (bufs.Count == 0) { return new T(); } T t = bufs[bufs.Count - 1]; bufs.RemoveAt(bufs.Count - 1); return t; } static public void Free(T t) { bufs.Add(t); } static public void FreeList(List list, System.Action fun) { for (int i = 0; i < list.Count; ++i) fun(list[i]); bufs.AddRange(list); list.Clear(); } } internal struct PD : System.IDisposable where T : new() { public PD(System.Action free) { value = PoolData.Get(); this.free = free; } public T value; private System.Action free; public void Dispose() { free(value); PoolData.Free(value); } } interface IFactory { object create(); } internal class Factory : IFactory where T : new() { public Factory(System.Action f) { free = f; } public System.Action free; public object create() { return new PD(free); } } internal static class Pool { static Factory sb_factory = null; public static PD GetSB() { if (sb_factory == null) { sb_factory = new Factory((StringBuilder sb) => { sb.Length = 0; }); } return (PD)sb_factory.create(); } } }