Class ObjectPool<T>
Generic object pool implementation.
Inherited Members
Namespace: StansAssets.Foundation.Patterns
Assembly: cs.temp.dll.dll
Syntax
public class ObjectPool<T>
where T : class
Type Parameters
| Name | Description |
|---|---|
| T | Type of the object pool. |
Constructors
ObjectPool(Func<T>, Action<T>, Action<T>, Boolean, Boolean, Int32, UInt32)
Creates a new ObjectPool.
Declaration
public ObjectPool(Func<T> actionCreate, Action<T> actionOnGet = null, Action<T> actionOnRelease = null, bool concurrent = false, bool collectionCheck = true, int defaultCapacity = -1, uint maxSize = 10000U)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Func<T> | actionCreate | Use to create a new instance when the pool is empty. In most cases this will just be
|
| System.Action<T> | actionOnGet | Called when the instance is being taken from the pool. |
| System.Action<T> | actionOnRelease | Called when the instance is being returned to the pool. This could be used to clean up or disable the instance. |
| System.Boolean | concurrent | When set to |
| System.Boolean | collectionCheck | Collection checks are performed when an instance is returned back to the pool. An exception will be thrown if the instance is already in the pool. Collection checks are only performed in the Editor. |
| System.Int32 | defaultCapacity | The default capacity the stack will be created with. |
| System.UInt32 | maxSize | The maximum size of the pool. When the pool reaches the max size then any further instances returned to the pool will be ignored and can be garbage collected. This can be used to prevent the pool growing to a very large size. |
Properties
CountActive
Number of objects that have been created by the pool but are currently in use and have not yet been returned.
Declaration
public int CountActive { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
CountAll
The total number of active and inactive objects.
Declaration
public int CountAll { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
CountInactive
Number of objects that are currently available in the pool.
Declaration
public int CountInactive { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 |
Methods
Clear()
Releases all pooled objects so they can be garbage collected.
Declaration
public void Clear()
Get()
Get an object from the pool.
Declaration
public T Get()
Returns
| Type | Description |
|---|---|
| T | A new object from the pool. |
Get(out T)
Get a new ObjectPool<T>.PooledObject which can be used to return the instance back to the pool when the PooledObject is disposed.
Declaration
public ObjectPool<T>.PooledObject Get(out T v)
Parameters
| Type | Name | Description |
|---|---|---|
| T | v | Output new typed object. |
Returns
| Type | Description |
|---|---|
| ObjectPool.PooledObject<> | New PooledObject |
PreWarm(Int32)
Fills instance with
Declaration
public void PreWarm(int count)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | count |
Release(T)
Release an object to the pool.
Declaration
public void Release(T element)
Parameters
| Type | Name | Description |
|---|---|---|
| T | element | Object to release. |
TryGet(Predicate<T>, out T)
Try to get an object in the pool.
Declaration
public bool TryGet(Predicate<T> pred, out T result)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Predicate<T> | pred | Search predicate. |
| T | result | An existing object from the pool or |
Returns
| Type | Description |
|---|---|
| System.Boolean |
|