mirror of
https://github.com/Facepunch/sbox-public.git
synced 2026-04-18 21:37:55 -04:00
This commit imports the C# engine code and game files, excluding C++ source code. [Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
32 lines
582 B
C#
32 lines
582 B
C#
namespace Sandbox.Network;
|
|
|
|
internal interface IObjectPoolEvent
|
|
{
|
|
void OnRented() { }
|
|
void OnReturned() { }
|
|
}
|
|
|
|
internal class NetworkObjectPool<T> where T : IObjectPoolEvent, new()
|
|
{
|
|
private readonly Queue<T> _pool = new();
|
|
|
|
/// <summary>
|
|
/// Rent an object from the pool.
|
|
/// </summary>
|
|
public T Rent()
|
|
{
|
|
var instance = _pool.Count > 0 ? _pool.Dequeue() : new();
|
|
instance.OnRented();
|
|
return instance;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Return an object to the pool.
|
|
/// </summary>
|
|
public void Return( T instance )
|
|
{
|
|
instance.OnReturned();
|
|
_pool.Enqueue( instance );
|
|
}
|
|
}
|