CCAutoreleasePool.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __AUTORELEASEPOOL_H__
  23. #define __AUTORELEASEPOOL_H__
  24. #include <vector>
  25. #include <string>
  26. #include "base/CCRef.h"
  27. /**
  28. * @addtogroup base
  29. * @{
  30. */
  31. NS_CC_BEGIN
  32. /**
  33. * A pool for managing autorelease objects.
  34. * @js NA
  35. */
  36. class CC_DLL AutoreleasePool
  37. {
  38. public:
  39. /**
  40. * @warning Don't create an autorelease pool in heap, create it in stack.
  41. * @js NA
  42. * @lua NA
  43. */
  44. AutoreleasePool();
  45. /**
  46. * Create an autorelease pool with specific name. This name is useful for debugging.
  47. * @warning Don't create an autorelease pool in heap, create it in stack.
  48. * @js NA
  49. * @lua NA
  50. *
  51. * @param name The name of created autorelease pool.
  52. */
  53. AutoreleasePool(const std::string &name);
  54. /**
  55. * @js NA
  56. * @lua NA
  57. */
  58. ~AutoreleasePool();
  59. /**
  60. * Add a given object to this autorelease pool.
  61. *
  62. * The same object may be added several times to an autorelease pool. When the
  63. * pool is destructed, the object's `Ref::release()` method will be called
  64. * the same times as it was added.
  65. *
  66. * @param object The object to be added into the autorelease pool.
  67. * @js NA
  68. * @lua NA
  69. */
  70. void addObject(Ref *object);
  71. /**
  72. * Clear the autorelease pool.
  73. *
  74. * It will invoke each element's `release()` function.
  75. *
  76. * @js NA
  77. * @lua NA
  78. */
  79. void clear();
  80. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  81. /**
  82. * Whether the autorelease pool is doing `clear` operation.
  83. *
  84. * @return True if autorelease pool is clearing, false if not.
  85. *
  86. * @js NA
  87. * @lua NA
  88. */
  89. bool isClearing() const { return _isClearing; };
  90. #endif
  91. /**
  92. * Checks whether the autorelease pool contains the specified object.
  93. *
  94. * @param object The object to be checked.
  95. * @return True if the autorelease pool contains the object, false if not
  96. * @js NA
  97. * @lua NA
  98. */
  99. bool contains(Ref* object) const;
  100. /**
  101. * Dump the objects that are put into the autorelease pool. It is used for debugging.
  102. *
  103. * The result will look like:
  104. * Object pointer address object id reference count
  105. *
  106. * @js NA
  107. * @lua NA
  108. */
  109. void dump();
  110. private:
  111. /**
  112. * The underlying array of object managed by the pool.
  113. *
  114. * Although Array retains the object once when an object is added, proper
  115. * Ref::release() is called outside the array to make sure that the pool
  116. * does not affect the managed object's reference count. So an object can
  117. * be destructed properly by calling Ref::release() even if the object
  118. * is in the pool.
  119. */
  120. std::vector<Ref*> _managedObjectArray;
  121. std::string _name;
  122. #if defined(COCOS2D_DEBUG) && (COCOS2D_DEBUG > 0)
  123. /**
  124. * The flag for checking whether the pool is doing `clear` operation.
  125. */
  126. bool _isClearing;
  127. #endif
  128. };
  129. // end of base group
  130. /** @} */
  131. /**
  132. * @cond
  133. */
  134. class CC_DLL PoolManager
  135. {
  136. public:
  137. CC_DEPRECATED_ATTRIBUTE static PoolManager* sharedPoolManager() { return getInstance(); }
  138. static PoolManager* getInstance();
  139. CC_DEPRECATED_ATTRIBUTE static void purgePoolManager() { destroyInstance(); }
  140. static void destroyInstance();
  141. /**
  142. * Get current auto release pool, there is at least one auto release pool that created by engine.
  143. * You can create your own auto release pool at demand, which will be put into auto release pool stack.
  144. */
  145. AutoreleasePool *getCurrentPool() const;
  146. bool isObjectInPools(Ref* obj) const;
  147. friend class AutoreleasePool;
  148. private:
  149. PoolManager();
  150. ~PoolManager();
  151. void push(AutoreleasePool *pool);
  152. void pop();
  153. static PoolManager* s_singleInstance;
  154. std::vector<AutoreleasePool*> _releasePoolStack;
  155. };
  156. /**
  157. * @endcond
  158. */
  159. NS_CC_END
  160. #endif //__AUTORELEASEPOOL_H__