首页 > 代码库 > Cocos2d-X学习之Ref类
Cocos2d-X学习之Ref类
先看看定义该类的头文件——CCRef.h
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__
头文件中主要有以下内容:
1、Clonable接口的定义;
2、Ref类的定义;
3、六个回调函数宏定义(包括相应类型定义);
Clonable接口成员如下:
由接口的名称含义就可以知道,该接口的主要作用就是克隆Ref类的成员,由于该头文件之前还未有Ref类的定义,而接口函数中有使用了Ref,所以又一个Ref类的前向引用(C++好像有这么个概念)
clone()和copy()均可以完成Ref对象的克隆,不过新版本中copy()已经废弃,建议使用clone();
Ref类的函数成员如下:
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)
关于回调函数宏定义及类型定义,下篇中会详细说明,因为引擎中涉及到太多太多,而且还不是很好理解。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。