using System;
using System.Collections;
using System.Collections.Generic;
namespace MotionFramework.Reference
{
///
/// 引用池
///
public static class ReferencePool
{
private static readonly Dictionary _collectors = new Dictionary();
///
/// 对象池初始容量
///
public static int InitCapacity { get; set; } = 100;
///
/// 对象池的数量
///
public static int Count
{
get
{
return _collectors.Count;
}
}
///
/// 清除所有对象池
///
public static void ClearAll()
{
_collectors.Clear();
}
///
/// 申请引用对象
///
public static IReference Spawn(Type type)
{
if (_collectors.ContainsKey(type) == false)
{
_collectors.Add(type, new ReferenceCollector(type, InitCapacity));
}
return _collectors[type].Spawn();
}
///
/// 申请引用对象
///
public static T Spawn() where T : class, IReference, new()
{
Type type = typeof(T);
return Spawn(type) as T;
}
///
/// 回收引用对象
///
public static void Release(IReference item)
{
Type type = item.GetType();
if (_collectors.ContainsKey(type) == false)
{
_collectors.Add(type, new ReferenceCollector(type, InitCapacity));
}
_collectors[type].Release(item);
}
///
/// 批量回收列表集合
///
public static void Release(List items) where T : class, IReference, new()
{
Type type = typeof(T);
if (_collectors.ContainsKey(type) == false)
{
_collectors.Add(type, new ReferenceCollector(type, InitCapacity));
}
for (int i = 0; i < items.Count; i++)
{
_collectors[type].Release(items[i]);
}
}
///
/// 批量回收数组集合
///
public static void Release(T[] items) where T : class, IReference, new()
{
Type type = typeof(T);
if (_collectors.ContainsKey(type) == false)
{
_collectors.Add(type, new ReferenceCollector(type, InitCapacity));
}
for (int i = 0; i < items.Length; i++)
{
_collectors[type].Release(items[i]);
}
}
#region 调试专属方法
internal static Dictionary GetAllCollectors
{
get { return _collectors; }
}
#endregion
}
}