CCRef.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 __BASE_CCREF_H__
  23. #define __BASE_CCREF_H__
  24. #include "base/ccMacros.h"
  25. #include "base/ccConfig.h"
  26. #define CC_REF_LEAK_DETECTION 0
  27. /**
  28. * @addtogroup base
  29. * @{
  30. */
  31. NS_CC_BEGIN
  32. class Ref;
  33. /**
  34. * Interface that defines how to clone an Ref.
  35. * @lua NA
  36. * @js NA
  37. */
  38. class CC_DLL Clonable
  39. {
  40. public:
  41. /** Returns a copy of the Ref. */
  42. virtual Clonable* clone() const = 0;
  43. /**
  44. * @js NA
  45. * @lua NA
  46. */
  47. virtual ~Clonable() {};
  48. };
  49. /**
  50. * Ref is used for reference count management. If a class inherits from Ref,
  51. * then it is easy to be shared in different places.
  52. * @js NA
  53. */
  54. class CC_DLL Ref
  55. {
  56. public:
  57. /**
  58. * Retains the ownership.
  59. *
  60. * This increases the Ref's reference count.
  61. *
  62. * @see release, autorelease
  63. * @js NA
  64. */
  65. void retain();
  66. /**
  67. * Releases the ownership immediately.
  68. *
  69. * This decrements the Ref's reference count.
  70. *
  71. * If the reference count reaches 0 after the decrement, this Ref is
  72. * destructed.
  73. *
  74. * @see retain, autorelease
  75. * @js NA
  76. */
  77. void release();
  78. /**
  79. * Releases the ownership sometime soon automatically.
  80. *
  81. * This decrements the Ref's reference count at the end of current
  82. * autorelease pool block.
  83. *
  84. * If the reference count reaches 0 after the decrement, this Ref is
  85. * destructed.
  86. *
  87. * @returns The Ref itself.
  88. *
  89. * @see AutoreleasePool, retain, release
  90. * @js NA
  91. * @lua NA
  92. */
  93. Ref* autorelease();
  94. /**
  95. * Returns the Ref's current reference count.
  96. *
  97. * @returns The Ref's reference count.
  98. * @js NA
  99. */
  100. unsigned int getReferenceCount() const;
  101. protected:
  102. /**
  103. * Constructor
  104. *
  105. * The Ref's reference count is 1 after construction.
  106. * @js NA
  107. */
  108. Ref();
  109. /**
  110. * Destructor
  111. *
  112. * @js NA
  113. * @lua NA
  114. */
  115. virtual ~Ref();
  116. protected:
  117. /// count of references
  118. unsigned int _referenceCount;
  119. friend class AutoreleasePool;
  120. // Memory leak diagnostic data (only included when CC_REF_LEAK_DETECTION is defined and its value isn't zero)
  121. #if CC_REF_LEAK_DETECTION
  122. public:
  123. static void printLeaks();
  124. #endif
  125. };
  126. NS_CC_END
  127. // end of base group
  128. /** @} */
  129. #endif // __BASE_CCREF_H__