先看看定義該類的頭文件——CCRef.hnode
1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2013-2014 Chukong Technologies 4 5 http://www.cocos2d-x.org 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 ****************************************************************************/ 25 26 #ifndef __CCREF_H__ 27 #define __CCREF_H__ 28 29 #include "CCPlatformMacros.h" 30 #include "ccConfig.h" 31 32 NS_CC_BEGIN 33 34 /** 35 * @addtogroup base_nodes 36 * @{ 37 */ 38 39 class Ref; 40 41 /** Interface that defines how to clone an Ref */ 42 class CC_DLL Clonable 43 { 44 public: 45 /** returns a copy of the Ref */ 46 virtual Clonable* clone() const = 0; 47 /** 48 * @js NA 49 * @lua NA 50 */ 51 virtual ~Clonable() {}; 52 53 /** returns a copy of the Ref. 54 @deprecated Use clone() instead 55 */ 56 CC_DEPRECATED_ATTRIBUTE Ref* copy() const 57 { 58 // use "clone" instead 59 CC_ASSERT(false); 60 return nullptr; 61 } 62 }; 63 64 class CC_DLL Ref 65 { 66 public: 67 /** 68 * Retains the ownership. 69 * 70 * This increases the Ref's reference count. 71 * 72 * @see release, autorelease 73 * @js NA 74 */ 75 void retain(); 76 77 /** 78 * Release the ownership immediately. 79 * 80 * This decrements the Ref's reference count. 81 * 82 * If the reference count reaches 0 after the descrement, this Ref is 83 * destructed. 84 * 85 * @see retain, autorelease 86 * @js NA 87 */ 88 void release(); 89 90 /** 91 * Release the ownership sometime soon automatically. 92 * 93 * This descrements the Ref's reference count at the end of current 94 * autorelease pool block. 95 * 96 * If the reference count reaches 0 after the descrement, this Ref is 97 * destructed. 98 * 99 * @returns The Ref itself. 100 * 101 * @see AutoreleasePool, retain, release 102 * @js NA 103 * @lua NA 104 */ 105 Ref* autorelease(); 106 107 /** 108 * Returns the Ref's current reference count. 109 * 110 * @returns The Ref's reference count. 111 * @js NA 112 */ 113 unsigned int getReferenceCount() const; 114 115 protected: 116 /** 117 * Constructor 118 * 119 * The Ref's reference count is 1 after construction. 120 * @js NA 121 */ 122 Ref(); 123 124 public: 125 /** 126 * @js NA 127 * @lua NA 128 */ 129 virtual ~Ref(); 130 131 protected: 132 /// count of references 133 unsigned int _referenceCount; 134 135 friend class AutoreleasePool; 136 137 #if CC_ENABLE_SCRIPT_BINDING 138 public: 139 /// object id, ScriptSupport need public _ID 140 unsigned int _ID; 141 /// Lua reference id 142 int _luaID; 143 #endif 144 }; 145 146 class Node; 147 148 typedef void (Ref::*SEL_CallFunc)(); 149 typedef void (Ref::*SEL_CallFuncN)(Node*); 150 typedef void (Ref::*SEL_CallFuncND)(Node*, void*); 151 typedef void (Ref::*SEL_CallFuncO)(Ref*); 152 typedef void (Ref::*SEL_MenuHandler)(Ref*); 153 typedef void (Ref::*SEL_SCHEDULE)(float); 154 155 #define callfunc_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFunc>(&_SELECTOR) 156 #define callfuncN_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncN>(&_SELECTOR) 157 #define callfuncND_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncND>(&_SELECTOR) 158 #define callfuncO_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncO>(&_SELECTOR) 159 #define menu_selector(_SELECTOR) static_cast<cocos2d::SEL_MenuHandler>(&_SELECTOR) 160 #define schedule_selector(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR) 161 162 // end of base_nodes group 163 /// @} 164 165 NS_CC_END 166 167 #endif // __CCREF_H__
頭文件中主要有如下內容:ide
一、Clonable接口的定義;函數
二、Ref類的定義;this
三、六個回調函數宏定義(包括相應類型定義);lua
Clonable接口成員以下:spa
由接口的名稱含義就能夠知道,該接口的主要做用就是克隆Ref類的成員,因爲該頭文件以前還未有Ref類的定義,而接口函數中有使用了Ref,因此又一個Ref類的前向引用(C++好像有這麼個概念)rest
clone()和copy()都可以完成Ref對象的克隆,不過新版本中copy()已經廢棄,建議使用clone();code
Ref類的函數成員以下:orm
retain();每調用一次引用次數+1;對象
release();每調用一次引用次數-1;
getReferenceCount();獲取引用的次數;
autorelease();每調用一次autorelease pool 中的Ref引用數-1;
Ref類的數據成員中,有一個無符號整型成員 _referenceCount,用來記錄引用次數的;
值得注意的是Ref類還有一個友元類AutoreleasePool;
回調函數及相應類型定義
typedef void (Ref::*SEL_CallFunc)(); // typedef void (Ref::*SEL_CallFuncN)(Node*); typedef void (Ref::*SEL_CallFuncND)(Node*, void*); typedef void (Ref::*SEL_CallFuncO)(Ref*); typedef void (Ref::*SEL_MenuHandler)(Ref*); typedef void (Ref::*SEL_SCHEDULE)(float); #define callfunc_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFunc>(&_SELECTOR) #define callfuncN_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncN>(&_SELECTOR) #define callfuncND_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncND>(&_SELECTOR) #define callfuncO_selector(_SELECTOR) static_cast<cocos2d::SEL_CallFuncO>(&_SELECTOR) #define menu_selector(_SELECTOR) static_cast<cocos2d::SEL_MenuHandler>(&_SELECTOR) #define schedule_selector(_SELECTOR) static_cast<cocos2d::SEL_SCHEDULE>(&_SELECTOR)
關於回調函數宏定義及類型定義,下篇中會詳細說明,由於引擎中涉及到太多太多,並且還不是很好理解。