Class Pool<T>
- Namespace
- Gamelogic.Extensions
- Assembly
- Assembly-CSharp.dll
A light-weight pool class. Can be used out of the box, or as base for more sophisticated pools.
public class Pool<T> where T : class
Type Parameters
T
The type of the objects to pool.
- Inheritance
-
Pool<T>
- Inherited Members
- Extension Methods
Constructors
Pool(int, Func<T>, Action<T>, Action<T>, Action<T>)
Initializes a new instance of the Pool<T> class.
public Pool(int initialCount, Func<T> create, Action<T> kill, Action<T> wakeUp, Action<T> setToSleep)
Parameters
initialCount
intThe initial number of objects to create.
create
Func<T>A function that creates a new object of type T.
kill
Action<T>The function that destroys an object of type T.
wakeUp
Action<T>A function called when an object is woken up.
setToSleep
Action<T>A function called when an object is set to sleep.
Properties
Capacity
The number total objects in the pool (awake and asleep).
public int Capacity { get; }
Property Value
- int
The capacity.
IsObjectAvailable
Returns whether there is a sleeping object available.
public bool IsObjectAvailable { get; }
Property Value
- bool
true
if this a sleeping object is available; otherwise,false
.
Methods
DecCapacity(int)
Decreases the capacity of the pool.
public void DecCapacity(int decrement)
Parameters
decrement
intThe number of pool objects to kill.
GetNewObject()
Gets a new object from the pool.
public T GetNewObject()
Returns
- T
A freshly awakened object.
Exceptions
- InvalidOperationException
No items in pool
IncCapacity(int)
Increases thew capacity of the pool.
public void IncCapacity(int increment)
Parameters
increment
intThe number of new pool objects to add.
Release(T)
Releases the specified object back to the pool.
public void Release(T obj)
Parameters
obj
TThe object to release.