1
0

CCRef.h 5.3 KB

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