using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace QFramework
{
#region API
///
/// Unity 游戏框架搭建 (十九) 简易对象池:http://qframework.io/post/24/ 的例子
///
///
internal class SimpleObjectPool : Pool
{
readonly Action mResetMethod;
public SimpleObjectPool(Func factoryMethod, Action resetMethod = null,int initCount = 0)
{
mFactory = new CustomObjectFactory(factoryMethod);
mResetMethod = resetMethod;
for (int i = 0; i < initCount; i++)
{
mCacheStack.Push(mFactory.Create());
}
}
public override bool Recycle(T obj)
{
if (mResetMethod != null)
{
mResetMethod.Invoke(obj);
}
mCacheStack.Push(obj);
return true;
}
}
///
/// Object pool.
///
internal class SafeObjectPool : Pool, ISingleton where T : IPoolable, new()
{
#region Singleton
void ISingleton.OnSingletonInit() {}
protected SafeObjectPool()
{
mFactory = new DefaultObjectFactory();
}
public static SafeObjectPool Instance
{
get { return SingletonProperty>.Instance; }
}
public void Dispose()
{
SingletonProperty>.Dispose();
}
#endregion
///
/// Init the specified maxCount and initCount.
///
/// Max Cache count.
/// Init Cache count.
public void Init(int maxCount, int initCount)
{
MaxCacheCount = maxCount;
if (maxCount > 0)
{
initCount = Math.Min(maxCount, initCount);
}
if (CurCount < initCount)
{
for (var i = CurCount; i < initCount; ++i)
{
Recycle(new T());
}
}
}
///
/// Gets or sets the max cache count.
///
/// The max cache count.
public int MaxCacheCount
{
get { return mMaxCount; }
set
{
mMaxCount = value;
if (mCacheStack != null)
{
if (mMaxCount > 0)
{
if (mMaxCount < mCacheStack.Count)
{
int removeCount = mCacheStack.Count - mMaxCount;
while (removeCount > 0)
{
mCacheStack.Pop();
--removeCount;
}
}
}
}
}
}
///
/// Allocate T instance.
///
public override T Allocate()
{
var result = base.Allocate();
result.IsRecycled = false;
return result;
}
///
/// Recycle the T instance
///
/// T.
public override bool Recycle(T t)
{
if (t == null || t.IsRecycled)
{
return false;
}
if (mMaxCount > 0)
{
if (mCacheStack.Count >= mMaxCount)
{
t.OnRecycled();
return false;
}
}
t.IsRecycled = true;
t.OnRecycled();
mCacheStack.Push(t);
return true;
}
}
///
/// Object pool 4 class who no public constructor
/// such as SingletonClass.QEventSystem
///
internal class NonPublicObjectPool :Pool,ISingleton where T : class,IPoolable
{
#region Singleton
public void OnSingletonInit(){}
public static NonPublicObjectPool Instance
{
get { return SingletonProperty>.Instance; }
}
protected NonPublicObjectPool()
{
mFactory = new NonPublicObjectFactory();
}
public void Dispose()
{
SingletonProperty>.Dispose();
}
#endregion
///
/// Init the specified maxCount and initCount.
///
/// Max Cache count.
/// Init Cache count.
public void Init(int maxCount, int initCount)
{
if (maxCount > 0)
{
initCount = Math.Min(maxCount, initCount);
}
if (CurCount >= initCount) return;
for (var i = CurCount; i < initCount; ++i)
{
Recycle(mFactory.Create());
}
}
///
/// Gets or sets the max cache count.
///
/// The max cache count.
public int MaxCacheCount
{
get { return mMaxCount; }
set
{
mMaxCount = value;
if (mCacheStack == null) return;
if (mMaxCount <= 0) return;
if (mMaxCount >= mCacheStack.Count) return;
var removeCount = mMaxCount - mCacheStack.Count;
while (removeCount > 0)
{
mCacheStack.Pop();
--removeCount;
}
}
}
///
/// Allocate T instance.
///
public override T Allocate()
{
var result = base.Allocate();
result.IsRecycled = false;
return result;
}
///
/// Recycle the T instance
///
/// T.
public override bool Recycle(T t)
{
if (t == null || t.IsRecycled)
{
return false;
}
if (mMaxCount > 0)
{
if (mCacheStack.Count >= mMaxCount)
{
t.OnRecycled();
return false;
}
}
t.IsRecycled = true;
t.OnRecycled();
mCacheStack.Push(t);
return true;
}
}
internal abstract class AbstractPool where T : AbstractPool, new()
{
private static Stack mPool = new Stack(10);
protected bool mInPool = false;
public static T Allocate()
{
var node = mPool.Count == 0 ? new T() : mPool.Pop();
node.mInPool = false;
return node;
}
public void Recycle2Cache()
{
OnRecycle();
mInPool = true;
mPool.Push(this as T);
}
protected abstract void OnRecycle();
}
#endregion
#region interfaces
///
/// 对象池接口
///
///
internal interface IPool
{
///
/// 分配对象
///
///
T Allocate();
///
/// 回收对象
///
///
///
bool Recycle(T obj);
}
///
/// I pool able.
///
internal interface IPoolable
{
void OnRecycled();
bool IsRecycled { get; set; }
}
///
/// I cache type.
///
internal interface IPoolType
{
void Recycle2Cache();
}
///
/// 对象池
///
///
internal abstract class Pool : IPool
{
#region ICountObserverable
///
/// Gets the current count.
///
/// The current count.
public int CurCount
{
get { return mCacheStack.Count; }
}
#endregion
protected IObjectFactory mFactory;
///
/// 存储相关数据的栈
///
protected readonly Stack mCacheStack = new Stack();
///
/// default is 5
///
protected int mMaxCount = 12;
public virtual T Allocate()
{
return mCacheStack.Count == 0
? mFactory.Create()
: mCacheStack.Pop();
}
public abstract bool Recycle(T obj);
}
#endregion
#region DataStructurePool
///
/// 字典对象池:用于存储相关对象
///
///
///
internal class DictionaryPool
{
///
/// 栈对象:存储多个字典
///
static Stack> mListStack = new Stack>(8);
///
/// 出栈:从栈中获取某个字典数据
///
///
public static Dictionary Get()
{
if (mListStack.Count == 0)
{
return new Dictionary(8);
}
return mListStack.Pop();
}
///
/// 入栈:将字典数据存储到栈中
///
///
public static void Release(Dictionary toRelease)
{
toRelease.Clear();
mListStack.Push(toRelease);
}
}
///
/// 对象池字典 拓展方法类
///
internal static class DictionaryPoolExtensions
{
///
/// 对字典拓展 自身入栈 的方法
///
///
///
///
public static void Release2Pool(this Dictionary toRelease)
{
DictionaryPool.Release(toRelease);
}
}
///
/// 链表对象池:存储相关对象
///
///
internal static class ListPool
{
///
/// 栈对象:存储多个List
///
static Stack> mListStack = new Stack>(8);
///
/// 出栈:获取某个List对象
///
///
public static List Get()
{
if (mListStack.Count == 0)
{
return new List(8);
}
return mListStack.Pop();
}
///
/// 入栈:将List对象添加到栈中
///
///
public static void Release(List toRelease)
{
toRelease.Clear();
mListStack.Push(toRelease);
}
}
///
/// 链表对象池 拓展方法类
///
internal static class ListPoolExtensions
{
///
/// 给List拓展 自身入栈 的方法
///
///
///
public static void Release2Pool(this List toRelease)
{
ListPool.Release(toRelease);
}
}
#endregion
#region Factories
///
/// 对象工厂
///
internal class ObjectFactory
{
///
/// 动态创建类的实例:创建有参的构造函数
///
///
///
///
public static object Create(Type type, params object[] constructorArgs)
{
return Activator.CreateInstance(type, constructorArgs);
}
///
/// 动态创建类的实例:泛型扩展
///
///
///
///
public static T Create(params object[] constructorArgs)
{
return (T) Create(typeof(T), constructorArgs);
}
///
/// 动态创建类的实例:创建无参/私有的构造函数
///
///
///
public static object CreateNonPublicConstructorObject(Type type)
{
// 获取私有构造函数
var constructorInfos = type.GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic);
// 获取无参构造函数
var ctor = Array.Find(constructorInfos, c => c.GetParameters().Length == 0);
if (ctor == null)
{
throw new Exception("Non-Public Constructor() not found! in " + type);
}
return ctor.Invoke(null);
}
///
/// 动态创建类的实例:创建无参/私有的构造函数 泛型扩展
///
///
///
public static T CreateNonPublicConstructorObject()
{
return (T) CreateNonPublicConstructorObject(typeof(T));
}
///
/// 创建带有初始化回调的 对象
///
///
///
///
///
public static object CreateWithInitialAction(Type type, Action