CObjectPool.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 async void Enqueue(CObject entity)
  14. {
  15. if (entity == null)
  16. {
  17. return;
  18. }
  19. await entity.AwaitTask();
  20. if (!entity.isActive)
  21. {
  22. return;
  23. }
  24. entity.isActive = false;
  25. this._queue.Enqueue(entity);
  26. entity.DormancyObj();
  27. }
  28. public CObject Dequeue()
  29. {
  30. return this._queue.Dequeue();
  31. }
  32. public CObject Peek()
  33. {
  34. return this._queue.Peek();
  35. }
  36. public Queue<CObject> Queue => this._queue;
  37. public int Count => this._queue.Count;
  38. public override void Dispose()
  39. {
  40. while (this._queue.Count > 0)
  41. {
  42. CObject component = this._queue.Dequeue();
  43. component.Dispose();
  44. }
  45. }
  46. public override void ActiveObj()
  47. {
  48. }
  49. public override void DormancyObj()
  50. {
  51. }
  52. }
  53. public class CObjectPool : CObject
  54. {
  55. private static CObjectPool _instance;
  56. public static CObjectPool Instance
  57. {
  58. get
  59. {
  60. if (_instance == null)
  61. {
  62. _instance = new CObjectPool();
  63. }
  64. return _instance;
  65. }
  66. }
  67. private readonly Dictionary<Type, ComponentQueue> _dictionary = new Dictionary<Type, ComponentQueue>();
  68. public CObject Fetch(Type type)
  69. {
  70. CObject obj;
  71. if (!this._dictionary.TryGetValue(type, out ComponentQueue queue))
  72. {
  73. obj = (CObject)Activator.CreateInstance(type);
  74. }
  75. else if (queue.Count == 0)
  76. {
  77. obj = (CObject)Activator.CreateInstance(type);
  78. }
  79. else
  80. {
  81. obj = queue.Dequeue();
  82. }
  83. obj.isActive = true;
  84. obj.ActiveObj();
  85. return obj;
  86. }
  87. public T Fetch<T>() where T : CObject
  88. {
  89. T t = (T)this.Fetch(typeof(T));
  90. return t;
  91. }
  92. public void Recycle(CObject obj)
  93. {
  94. if (obj == null)
  95. {
  96. return;
  97. }
  98. Type type = obj.GetType();
  99. ComponentQueue queue;
  100. if (!this._dictionary.TryGetValue(type, out queue))
  101. {
  102. queue = new ComponentQueue(type.Name);
  103. this._dictionary.Add(type, queue);
  104. }
  105. queue.Enqueue(obj);
  106. }
  107. public void Clear()
  108. {
  109. foreach (KeyValuePair<Type, ComponentQueue> kv in this._dictionary)
  110. {
  111. kv.Value.Dispose();
  112. }
  113. this._dictionary.Clear();
  114. }
  115. public override void Dispose()
  116. {
  117. foreach (KeyValuePair<Type, ComponentQueue> kv in this._dictionary)
  118. {
  119. kv.Value.Dispose();
  120. }
  121. this._dictionary.Clear();
  122. _instance = null;
  123. }
  124. public override void ActiveObj()
  125. {
  126. }
  127. public override void DormancyObj()
  128. {
  129. }
  130. }
  131. }