using System.Collections.Generic;
namespace Unity.XR.CoreUtils
{
///
/// Provides a generic object pool implementation.
///
/// The of objects in this pool.
public class ObjectPool where T : class, new()
{
///
/// All objects currently in this pool.
///
protected readonly Queue PooledQueue = new Queue();
///
/// Gets an object instance from the pool. Creates a new instance if the pool is empty.
///
/// The object instance
public virtual T Get()
{
return PooledQueue.Count == 0 ? new T() : PooledQueue.Dequeue();
}
///
/// Returns an object instance to the pool.
///
///
/// Object values can be cleared automatically if is implemented.
/// The base `ObjectPool` class does not clear objects to the pool.
///
/// The instance to return.
public void Recycle(T instance)
{
ClearInstance(instance);
PooledQueue.Enqueue(instance);
}
///
/// Implement this function in a derived class to
/// automatically clear an instance when is called.
///
/// The object to clear.
protected virtual void ClearInstance(T instance) { }
}
}