BaseObject.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2012-2017 DragonBones team and other contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  7. * this software and associated documentation files (the "Software"), to deal in
  8. * the Software without restriction, including without limitation the rights to
  9. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10. * the Software, and to permit persons to whom the Software is furnished to do so,
  11. * subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. using System.Collections.Generic;
  24. namespace DragonBones
  25. {
  26. /// <summary>
  27. /// - The BaseObject is the base class for all objects in the DragonBones framework.
  28. /// All BaseObject instances are cached to the object pool to reduce the performance consumption of frequent requests for memory or memory recovery.
  29. /// </summary>
  30. /// <version>DragonBones 4.5</version>
  31. /// <language>en_US</language>
  32. /// <summary>
  33. /// - 基础对象,通常 DragonBones 的对象都继承自该类。
  34. /// 所有基础对象的实例都会缓存到对象池,以减少频繁申请内存或内存回收的性能消耗。
  35. /// </summary>
  36. /// <version>DragonBones 4.5</version>
  37. /// <language>zh_CN</language>
  38. public abstract class BaseObject
  39. {
  40. private static uint _hashCode = 0;
  41. private static uint _defaultMaxCount = 3000;
  42. private static readonly Dictionary<System.Type, uint> _maxCountMap = new Dictionary<System.Type, uint>();
  43. private static readonly Dictionary<System.Type, List<BaseObject>> _poolsMap = new Dictionary<System.Type, List<BaseObject>>();
  44. private static void _ReturnObject(BaseObject obj)
  45. {
  46. var classType = obj.GetType();
  47. var maxCount = _maxCountMap.ContainsKey(classType) ? _maxCountMap[classType] : _defaultMaxCount;
  48. var pool = _poolsMap.ContainsKey(classType) ? _poolsMap[classType] : _poolsMap[classType] = new List<BaseObject>();
  49. if (pool.Count < maxCount)
  50. {
  51. if (!pool.Contains(obj))
  52. {
  53. pool.Add(obj);
  54. }
  55. else
  56. {
  57. Helper.Assert(false, "The object is already in the pool.");
  58. }
  59. }
  60. else
  61. {
  62. }
  63. }
  64. /// <summary>
  65. /// - Set the maximum cache count of the specify object pool.
  66. /// </summary>
  67. /// <param name="objectConstructor">- The specify class. (Set all object pools max cache count if not set)</param>
  68. /// <param name="maxCount">- Max count.</param>
  69. /// <version>DragonBones 4.5</version>
  70. /// <language>en_US</language>
  71. /// <summary>
  72. /// - 设置特定对象池的最大缓存数量。
  73. /// </summary>
  74. /// <param name="objectConstructor">- 特定的类。 (不设置则设置所有对象池的最大缓存数量)</param>
  75. /// <param name="maxCount">- 最大缓存数量。</param>
  76. /// <version>DragonBones 4.5</version>
  77. /// <language>zh_CN</language>
  78. public static void SetMaxCount(System.Type classType, uint maxCount)
  79. {
  80. if (classType != null)
  81. {
  82. if (_poolsMap.ContainsKey(classType))
  83. {
  84. var pool = _poolsMap[classType];
  85. if (pool.Count > maxCount)
  86. {
  87. pool.ResizeList((int)maxCount, null);
  88. }
  89. }
  90. _maxCountMap[classType] = maxCount;
  91. }
  92. else
  93. {
  94. _defaultMaxCount = maxCount;
  95. foreach (var key in _poolsMap.Keys)
  96. {
  97. var pool = _poolsMap[key];
  98. if (pool.Count > maxCount)
  99. {
  100. pool.ResizeList((int)maxCount, null);
  101. }
  102. if (_maxCountMap.ContainsKey(key))
  103. {
  104. _maxCountMap[key] = maxCount;
  105. }
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// - Clear the cached instances of a specify object pool.
  111. /// </summary>
  112. /// <param name="objectConstructor">- Specify class. (Clear all cached instances if not set)</param>
  113. /// <version>DragonBones 4.5</version>
  114. /// <language>en_US</language>
  115. /// <summary>
  116. /// - 清除特定对象池的缓存实例。
  117. /// </summary>
  118. /// <param name="objectConstructor">- 特定的类。 (不设置则清除所有缓存的实例)</param>
  119. /// <version>DragonBones 4.5</version>
  120. /// <language>zh_CN</language>
  121. public static void ClearPool(System.Type classType)
  122. {
  123. if (classType != null)
  124. {
  125. if (_poolsMap.ContainsKey(classType))
  126. {
  127. var pool = _poolsMap[classType];
  128. if (pool != null)
  129. {
  130. pool.Clear();
  131. }
  132. }
  133. }
  134. else
  135. {
  136. foreach (var pair in _poolsMap)
  137. {
  138. var pool = _poolsMap[pair.Key];
  139. if (pool != null)
  140. {
  141. pool.Clear();
  142. }
  143. }
  144. }
  145. }
  146. /// <summary>
  147. /// - Get an instance of the specify class from object pool.
  148. /// </summary>
  149. /// <param name="objectConstructor">- The specify class.</param>
  150. /// <version>DragonBones 4.5</version>
  151. /// <language>en_US</language>
  152. /// <summary>
  153. /// - 从对象池中获取特定类的实例。
  154. /// </summary>
  155. /// <param name="objectConstructor">- 特定的类。</param>
  156. /// <version>DragonBones 4.5</version>
  157. /// <language>zh_CN</language>
  158. public static T BorrowObject<T>() where T : BaseObject, new()
  159. {
  160. var type = typeof(T);
  161. var pool = _poolsMap.ContainsKey(type) ? _poolsMap[type] : null;
  162. if (pool != null && pool.Count > 0)
  163. {
  164. var index = pool.Count - 1;
  165. var obj = pool[index];
  166. pool.RemoveAt(index);
  167. return (T)obj;
  168. }
  169. else
  170. {
  171. var obj = new T();
  172. obj._OnClear();
  173. return obj;
  174. }
  175. }
  176. /// <summary>
  177. /// - A unique identification number assigned to the object.
  178. /// </summary>
  179. /// <version>DragonBones 4.5</version>
  180. /// <language>en_US</language>
  181. /// <summary>
  182. /// - 分配给此实例的唯一标识号。
  183. /// </summary>
  184. /// <version>DragonBones 4.5</version>
  185. /// <language>zh_CN</language>
  186. public readonly uint hashCode = _hashCode++;
  187. protected BaseObject()
  188. {
  189. }
  190. /// <private/>
  191. protected abstract void _OnClear();
  192. /// <summary>
  193. /// - Clear the object and return it back to object pool。
  194. /// </summary>
  195. /// <version>DragonBones 4.5</version>
  196. /// <language>en_US</language>
  197. /// <summary>
  198. /// - 清除该实例的所有数据并将其返还对象池。
  199. /// </summary>
  200. /// <version>DragonBones 4.5</version>
  201. /// <language>zh_CN</language>
  202. public void ReturnToPool()
  203. {
  204. _OnClear();
  205. _ReturnObject(this);
  206. }
  207. // public static implicit operator bool(BaseObject exists)
  208. // {
  209. // return exists != null;
  210. // }
  211. }
  212. }