Files
sbox-public/engine/Sandbox.Engine/Scene/Networking/DeltaSnapshots/NetworkObjectPool.cs
s&box team 71f266059a Open source release
This commit imports the C# engine code and game files, excluding C++ source code.

[Source-Commit: ceb3d758046e50faa6258bc3b658a30c97743268]
2025-11-24 09:05:18 +00:00

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 );
}
}