2using System.Collections.Generic;
6 internal class CesiumObjectPool<T> : IDisposable where T : class
9 private int _maximumSize;
10 private Func<T> _createCallback;
11 private Action<T> _releaseCallback;
12 private Action<T> _destroyCallback;
14 public CesiumObjectPool(Func<T> createCallback, Action<T> releaseCallback, Action<T> destroyCallback,
int maximumSize = 1000)
16 this._pool =
new List<T>(maximumSize);
17 this._maximumSize = maximumSize;
18 this._createCallback = createCallback;
19 this._releaseCallback = releaseCallback;
20 this._destroyCallback = destroyCallback;
32 public int CountInactive => this._pool.Count;
36 if (this._pool ==
null)
39 foreach (T o
in this._pool)
41 this._destroyCallback(o);
47 if (this._pool !=
null && this._pool.Count > 0)
49 int pos = this._pool.Count - 1;
50 T result = this._pool[pos];
51 this._pool.RemoveAt(pos);
56 return this._createCallback();
60 public void Release(T element)
62 this._releaseCallback(element);
64 if (this._pool !=
null && this._pool.Count <
this._maximumSize)
66 this._pool.Add(element);
70 this._destroyCallback(element);