|
|
@@ -3,61 +3,112 @@ using System.Collections.Generic;
|
|
|
|
|
|
namespace ETHotfix
|
|
|
{
|
|
|
- public class ObjectPool: Component
|
|
|
+ public class ComponentQueue: Component
|
|
|
{
|
|
|
- private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
|
|
|
+ public string TypeName { get; }
|
|
|
+
|
|
|
+ private readonly Queue<Component> queue = new Queue<Component>();
|
|
|
|
|
|
- public Component Fetch(Type type)
|
|
|
+ public ComponentQueue(string typeName)
|
|
|
{
|
|
|
- Queue<Component> queue;
|
|
|
- if (!this.dictionary.TryGetValue(type, out queue))
|
|
|
- {
|
|
|
- queue = new Queue<Component>();
|
|
|
- this.dictionary.Add(type, queue);
|
|
|
- }
|
|
|
- Component obj;
|
|
|
- if (queue.Count > 0)
|
|
|
- {
|
|
|
- obj = queue.Dequeue();
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- obj = (Component)Activator.CreateInstance(type);
|
|
|
- }
|
|
|
- obj.IsFromPool = true;
|
|
|
- return obj;
|
|
|
+ this.TypeName = typeName;
|
|
|
}
|
|
|
|
|
|
- public T Fetch<T>() where T: Component
|
|
|
+ public void Enqueue(Component component)
|
|
|
{
|
|
|
- T t = (T) this.Fetch(typeof(T));
|
|
|
- return t;
|
|
|
+ component.Parent = this;
|
|
|
+ this.queue.Enqueue(component);
|
|
|
}
|
|
|
-
|
|
|
- public void Recycle(Component obj)
|
|
|
+
|
|
|
+ public Component Dequeue()
|
|
|
+ {
|
|
|
+ return this.queue.Dequeue();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Component Peek()
|
|
|
{
|
|
|
- obj.Parent = this;
|
|
|
- Type type = obj.GetType();
|
|
|
- Queue<Component> queue;
|
|
|
- if (!this.dictionary.TryGetValue(type, out queue))
|
|
|
+ return this.queue.Peek();
|
|
|
+ }
|
|
|
+
|
|
|
+ public int Count
|
|
|
+ {
|
|
|
+ get
|
|
|
{
|
|
|
- queue = new Queue<Component>();
|
|
|
- this.dictionary.Add(type, queue);
|
|
|
+ return this.queue.Count;
|
|
|
}
|
|
|
- queue.Enqueue(obj);
|
|
|
}
|
|
|
-
|
|
|
- public void Clear()
|
|
|
+
|
|
|
+ public override void Dispose()
|
|
|
{
|
|
|
- foreach (var kv in this.dictionary)
|
|
|
+ if (this.IsDisposed)
|
|
|
{
|
|
|
- foreach (Component component in kv.Value)
|
|
|
- {
|
|
|
- component.IsFromPool = false;
|
|
|
- component.Dispose();
|
|
|
- }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ base.Dispose();
|
|
|
+
|
|
|
+ while (this.queue.Count > 0)
|
|
|
+ {
|
|
|
+ Component component = this.queue.Dequeue();
|
|
|
+ component.IsFromPool = false;
|
|
|
+ component.Dispose();
|
|
|
}
|
|
|
- this.dictionary.Clear();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public class ObjectPool: Component
|
|
|
+ {
|
|
|
+ public string Name { get; set; }
|
|
|
+
|
|
|
+ private readonly Dictionary<Type, ComponentQueue> dictionary = new Dictionary<Type, ComponentQueue>();
|
|
|
+
|
|
|
+ public Component Fetch(Type type)
|
|
|
+ {
|
|
|
+ Component obj;
|
|
|
+ if (!this.dictionary.TryGetValue(type, out ComponentQueue queue))
|
|
|
+ {
|
|
|
+ obj = (Component)Activator.CreateInstance(type);
|
|
|
+ }
|
|
|
+ else if (queue.Count == 0)
|
|
|
+ {
|
|
|
+ obj = (Component)Activator.CreateInstance(type);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ obj = queue.Dequeue();
|
|
|
+ }
|
|
|
+
|
|
|
+ obj.IsFromPool = true;
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
+ public T Fetch<T>() where T: Component
|
|
|
+ {
|
|
|
+ T t = (T) this.Fetch(typeof(T));
|
|
|
+ return t;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Recycle(Component obj)
|
|
|
+ {
|
|
|
+ obj.Parent = this;
|
|
|
+ Type type = obj.GetType();
|
|
|
+ ComponentQueue queue;
|
|
|
+ if (!this.dictionary.TryGetValue(type, out queue))
|
|
|
+ {
|
|
|
+ queue = new ComponentQueue(type.Name);
|
|
|
+ queue.Parent = this;
|
|
|
+ this.dictionary.Add(type, queue);
|
|
|
+ }
|
|
|
+ queue.Enqueue(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void Clear()
|
|
|
+ {
|
|
|
+ foreach (var kv in this.dictionary)
|
|
|
+ {
|
|
|
+ kv.Value.IsFromPool = false;
|
|
|
+ kv.Value.Dispose();
|
|
|
+ }
|
|
|
+ this.dictionary.Clear();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|