CCRef.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies
  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 "platform/CCPlatformMacros.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. /** Returns a copy of the Ref.
  49. * @deprecated Use clone() instead.
  50. */
  51. CC_DEPRECATED_ATTRIBUTE Ref* copy() const
  52. {
  53. // use "clone" instead
  54. CC_ASSERT(false);
  55. return nullptr;
  56. }
  57. };
  58. /**
  59. * Ref is used for reference count management. If a class inherits from Ref,
  60. * then it is easy to be shared in different places.
  61. * @js NA
  62. */
  63. class CC_DLL Ref
  64. {
  65. public:
  66. /**
  67. * Retains the ownership.
  68. *
  69. * This increases the Ref's reference count.
  70. *
  71. * @see release, autorelease
  72. * @js NA
  73. */
  74. void retain();
  75. /**
  76. * Releases the ownership immediately.
  77. *
  78. * This decrements the Ref's reference count.
  79. *
  80. * If the reference count reaches 0 after the decrement, this Ref is
  81. * destructed.
  82. *
  83. * @see retain, autorelease
  84. * @js NA
  85. */
  86. void release();
  87. /**
  88. * Releases the ownership sometime soon automatically.
  89. *
  90. * This decrements the Ref's reference count at the end of current
  91. * autorelease pool block.
  92. *
  93. * If the reference count reaches 0 after the decrement, this Ref is
  94. * destructed.
  95. *
  96. * @returns The Ref itself.
  97. *
  98. * @see AutoreleasePool, retain, release
  99. * @js NA
  100. * @lua NA
  101. */
  102. Ref* autorelease();
  103. /**
  104. * Returns the Ref's current reference count.
  105. *
  106. * @returns The Ref's reference count.
  107. * @js NA
  108. */
  109. unsigned int getReferenceCount() const;
  110. protected:
  111. /**
  112. * Constructor
  113. *
  114. * The Ref's reference count is 1 after construction.
  115. * @js NA
  116. */
  117. Ref();
  118. public:
  119. /**
  120. * Destructor
  121. *
  122. * @js NA
  123. * @lua NA
  124. */
  125. virtual ~Ref();
  126. protected:
  127. /// count of references
  128. unsigned int _referenceCount;
  129. friend class AutoreleasePool;
  130. #if CC_ENABLE_SCRIPT_BINDING
  131. public:
  132. /// object id, ScriptSupport need public _ID
  133. unsigned int _ID;
  134. /// Lua reference id
  135. int _luaID;
  136. /// scriptObject, support for swift
  137. void* _scriptObject;
  138. /**
  139. When true, it means that the object was already rooted.
  140. */
  141. bool _rooted;
  142. #endif
  143. // Memory leak diagnostic data (only included when CC_REF_LEAK_DETECTION is defined and its value isn't zero)
  144. #if CC_REF_LEAK_DETECTION
  145. public:
  146. static void printLeaks();
  147. #endif
  148. };
  149. class Node;
  150. typedef void (Ref::*SEL_CallFunc)();
  151. typedef void (Ref::*SEL_CallFuncN)(Node*);
  152. typedef void (Ref::*SEL_CallFuncND)(Node*, void*);
  153. typedef void (Ref::*SEL_CallFuncO)(Ref*);
  154. typedef void (Ref::*SEL_MenuHandler)(Ref*);
  155. typedef void (Ref::*SEL_SCHEDULE)(float);
  156. #define CC_CALLFUNC_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_CallFunc>(&_SELECTOR)
  157. #define CC_CALLFUNCN_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_CallFuncN>(&_SELECTOR)
  158. #define CC_CALLFUNCND_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_CallFuncND>(&_SELECTOR)
  159. #define CC_CALLFUNCO_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_CallFuncO>(&_SELECTOR)
  160. #define CC_MENU_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_MenuHandler>(&_SELECTOR)
  161. #define CC_SCHEDULE_SELECTOR(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)
  162. // Deprecated
  163. #define callfunc_selector(_SELECTOR) CC_CALLFUNC_SELECTOR(_SELECTOR)
  164. #define callfuncN_selector(_SELECTOR) CC_CALLFUNCN_SELECTOR(_SELECTOR)
  165. #define callfuncND_selector(_SELECTOR) CC_CALLFUNCND_SELECTOR(_SELECTOR)
  166. #define callfuncO_selector(_SELECTOR) CC_CALLFUNCO_SELECTOR(_SELECTOR)
  167. #define menu_selector(_SELECTOR) CC_MENU_SELECTOR(_SELECTOR)
  168. #define schedule_selector(_SELECTOR) CC_SCHEDULE_SELECTOR(_SELECTOR)
  169. NS_CC_END
  170. // end of base group
  171. /** @} */
  172. #endif // __BASE_CCREF_H__