[+] TEngineServer

[+] TEngineServer
This commit is contained in:
ALEXTANG
2023-07-13 17:17:26 +08:00
parent a69f53592e
commit 0c8f3a5f92
790 changed files with 52737 additions and 2533 deletions

View File

@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
namespace TEngine
{
/// <summary>
/// 静态通用对象池
/// </summary>
/// <typeparam name="T"></typeparam>
public static class PoolWithDisposable<T> where T : IDisposable
{
private static readonly Queue<T> PoolQueue = new Queue<T>();
public static int Count => PoolQueue.Count;
public static T Rent()
{
return PoolQueue.Count == 0 ? Activator.CreateInstance<T>() : PoolQueue.Dequeue();
}
public static T Rent(Func<T> generator)
{
return PoolQueue.Count == 0 ? Activator.CreateInstance<T>() : PoolQueue.Dequeue();
}
public static void Return(T t)
{
if (t == null)
{
return;
}
PoolQueue.Enqueue(t);
t.Dispose();
}
public static void Return(T t, Action<T> reset)
{
if (t == null)
{
return;
}
reset(t);
PoolQueue.Enqueue(t);
t.Dispose();
}
public static void Clear()
{
PoolQueue.Clear();
}
}
}