Class Pool<T>
- Namespace
- Gamelogic.Extensions
- Assembly
- Gamelogic.Extensions.dll
A light-weight pool class for objects that cannot be hashed.
public class Pool<T> : IPool<T> where T : class
Type Parameters
T
The type of the objects to pool. Since the methods defined by the interface would copy instances of pool objects if
T
was a struct,T
must be a class.
- Inheritance
-
Pool<T>
- Implements
-
IPool<T>
- Inherited Members
- Extension Methods
Remarks
When the pool is created, it is filled up to capacity with objects that are newly created. A pool object can be active or inactive. When an object is active, it can be used by the client. When it is inactive, it should not be used, but the system has no checks for this. Objects are set to inactive when they are created for the first time.
To get an object that you can use, call Get(). This will activate one of the pool objects up for your use.
When you are done with it, call Release(T). This will deactivate the object, and allow it to be reused.
Acquiring an object or releasing it does not destroy or create any objects, and pool objects always remain in the pool unless the capacity is reduced.
We recommend implementing a bool isActive field in the object you want to pool, and maintain it in the activate and deactivate actions you provide in the constructor. You can then perform asserts in your code on this field to ensure you do not accidentally use objects that are inactive.
Constructors
- Pool(int, Func<T>, Action<T>, Action<T>, Action<T>)
Initializes a new instance of the Pool<T> class.
Properties
- ActiveCount
The number of objects that are currently active.
- Capacity
The total number of objects in the pool (active and inactive), the maximum number of objects that can be returned by Get().
- HasAvailableObject
Returns whether there is an inactive object available to get.
Methods
- DecCapacity(int, bool)
Decreases the capacity of the pool.
- DecreaseCapacity(int, bool)
Decreases the capacity of the pool.
- Get()
Gets a new object from the pool.
- GetNewObject()
Gets a new object from the pool.
- IncCapacity(int)
Increases the capacity of the pool.
- IncreaseCapacity(int)
Increases the capacity of the pool.
- Release(T)
Releases the specified object back to the pool.
- ReleaseAll()
Releases all objects in the pool.