CObjectPool.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Fort23.Core
  4. {
  5. public class ComponentQueue : CObject
  6. {
  7. public string TypeName { get; }
  8. private readonly Queue<CObject> _queue = new Queue<CObject>();
  9. public ComponentQueue(string typeName)
  10. {
  11. this.TypeName = typeName;
  12. }
  13. public void Enqueue(CObject entity)
  14. {
  15. this._queue.Enqueue(entity);
  16. entity.DormancyObj();
  17. }
  18. public CObject Dequeue()
  19. {
  20. return this._queue.Dequeue();
  21. }
  22. public CObject Peek()
  23. {
  24. return this._queue.Peek();
  25. }
  26. public Queue<CObject> Queue => this._queue;
  27. public int Count => this._queue.Count;
  28. public override void Dispose()
  29. {
  30. while (this._queue.Count > 0)
  31. {
  32. CObject component = this._queue.Dequeue();
  33. component.Dispose();
  34. }
  35. }
  36. public override void ActiveObj()
  37. {
  38. }
  39. public override void DormancyObj()
  40. {
  41. }
  42. }
  43. public class CObjectPool : CObject
  44. {
  45. private static CObjectPool _instance;
  46. public static CObjectPool Instance
  47. {
  48. get
  49. {
  50. if (_instance == null)
  51. {
  52. _instance = new CObjectPool();
  53. }
  54. return _instance;
  55. }
  56. }
  57. private readonly Dictionary<Type, ComponentQueue> _dictionary = new Dictionary<Type, ComponentQueue>();
  58. public CObject Fetch(Type type)
  59. {
  60. CObject obj;
  61. if (!this._dictionary.TryGetValue(type, out ComponentQueue queue))
  62. {
  63. obj = (CObject)Activator.CreateInstance(type);
  64. }
  65. else if (queue.Count == 0)
  66. {
  67. obj = (CObject)Activator.CreateInstance(type);
  68. }
  69. else
  70. {
  71. obj = queue.Dequeue();
  72. }
  73. obj.ActiveObj();
  74. return obj;
  75. }
  76. public T Fetch<T>() where T : CObject
  77. {
  78. T t = (T)this.Fetch(typeof(T));
  79. return t;
  80. }
  81. public void Recycle(CObject obj)
  82. {
  83. Type type = obj.GetType();
  84. ComponentQueue queue;
  85. if (!this._dictionary.TryGetValue(type, out queue))
  86. {
  87. queue = new ComponentQueue(type.Name);
  88. this._dictionary.Add(type, queue);
  89. }
  90. queue.Enqueue(obj);
  91. }
  92. public void Clear()
  93. {
  94. foreach (KeyValuePair<Type, ComponentQueue> kv in this._dictionary)
  95. {
  96. kv.Value.Dispose();
  97. }
  98. this._dictionary.Clear();
  99. }
  100. public override void Dispose()
  101. {
  102. foreach (KeyValuePair<Type, ComponentQueue> kv in this._dictionary)
  103. {
  104. kv.Value.Dispose();
  105. }
  106. this._dictionary.Clear();
  107. _instance = null;
  108. }
  109. public override void ActiveObj()
  110. {
  111. }
  112. public override void DormancyObj()
  113. {
  114. }
  115. }
  116. }