CCAutoreleasePool.h 5.1 KB

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