CCObject -> CCCopying
class CC_DLL CCObject : public CCCopying { public: // object id, CCScriptSupport need public m_uID unsigned int m_uID; // Lua reference id int m_nLuaID; protected: // count of references unsigned int m_uReference; // count of autorelease unsigned int m_uAutoReleaseCount; public: CCObject(void); /** * @lua NA */ virtual ~CCObject(void); void release(void); void retain(void); CCObject* autorelease(void); CCObject* copy(void); bool isSingleReference(void) const; unsigned int retainCount(void) const; virtual bool isEqual(const CCObject* pObject); virtual void acceptVisitor(CCDataVisitor &visitor); virtual void update(float dt) {CC_UNUSED_PARAM(dt);}; friend class CCAutoreleasePool; };
unsigned int m_uID;
对象ID,由CCObject管理的全局唯一对象ID,用于CCScriptSupport类。每生成一个CCObject或者其子类对象,id自动增长1。
int m_nLuaID;
LuaID,从名字上看就知道是Lua脚本使用的ID,该ID由CCObject定义,但是CCObject并不管理该ID,由LuaCocos2d模块使用。
unsigned int m_uReference;
对象计数器,构造时为1,当调用对象的release成员函数时,这个值将自减,如果减少到0,那么对象被自己删除。
unsigned int m_uAutoReleaseCount;
调用autorelease的次数记录,初始化为0,调用一次自增1,用于析构函数检查是否需要从对象池中清除自身。
CCObject(void);
构造函数,主要的功能是将成员函数初始化。
构造函数实现中,用一个静态变量来管理m_uID。
CCObject::CCObject(void) : m_nLuaID(0) , m_uReference(1) // when the object is created, the reference count of it is 1 , m_uAutoReleaseCount(0) { static unsigned int uObjectCount = 0; m_uID = ++uObjectCount; }
virtual ~CCObject(void);
析构函数,主要的功能是清除该对象与对象池、脚本对象管理之间的关系
CCObject::~CCObject(void) { // if the object is managed, we should remove it // from pool manager if (m_uAutoReleaseCount > 0) { CCPoolManager::sharedPoolManager()->removeObject(this); } // if the object is referenced by Lua engine, remove it if (m_nLuaID) { CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptObjectByCCObject(this); } else { CCScriptEngineProtocol* pEngine = CCScriptEngineManager::sharedManager()->getScriptEngine(); if (pEngine != NULL && pEngine->getScriptType() == kScriptTypeJavascript) { pEngine->removeScriptObjectByCCObject(this); } } }
void release(void);
release函数被调用时,计数器减一,如果计数器到0,那么
void CCObject::release(void) { CCAssert(m_uReference > 0, "reference count should greater than 0"); --m_uReference; if (m_uReference == 0) { delete this; } }
void retain(void);
增加计数器,保证对象不被删除
void CCObject::retain(void) { CCAssert(m_uReference > 0, "reference count should greater than 0"); ++m_uReference; }
CCObject* autorelease(void);
让对象池管理器管理本对象,这样程序员可以不用去delete该对象内存
CCObject* CCObject::autorelease(void) { CCPoolManager::sharedPoolManager()->addObject(this); return this; }
CCObject* copy(void);
对象拷贝,这里调用了CCCopying的copyWithZone,那么copy也是和CCCopying一样没有做任何事情,拷贝工作还是需要子类做具体的实现,这里只是留有接口罢了
CCObject* CCObject::copy() { return copyWithZone(0); }
bool isSingleReference(void) const;
这个函数只是简单判断m_uReference是否为1而已
bool CCObject::isSingleReference(void) const { return m_uReference == 1; }
unsigned int retainCount(void) const;
返回引用个数
unsigned int CCObject::retainCount(void) const { return m_uReference; }
virtual bool isEqual(const CCObject* pObject);
判断是否相等
bool CCObject::isEqual(const CCObject *pObject) { return this == pObject; }
virtual void acceptVisitor(CCDataVisitor& visitor);
接受一个CCDataVisitor,可以通过该接口实现序列化等工作
void CCObject::acceptVisitor(CCDataVisitor &visitor) { visitor.visitObject(this); }
virtual void update(float dt) ;
更新函数,CCObject没有实现,只是预留接口
virtual void update(float dt) {CC_UNUSED_PARAM(dt);};
typedef void (CCObject::*SEL_SCHEDULE)(float); typedef void (CCObject::*SEL_CallFunc)(); typedef void (CCObject::*SEL_CallFuncN)(CCNode*); typedef void (CCObject::*SEL_CallFuncND)(CCNode*, void*); typedef void (CCObject::*SEL_CallFuncO)(CCObject*); typedef void (CCObject::*SEL_MenuHandler)(CCObject*); typedef void (CCObject::*SEL_EventHandler)(CCEvent*); typedef int (CCObject::*SEL_Compare)(CCObject*); #define schedule_selector(_SELECTOR) (SEL_SCHEDULE)(&_SELECTOR) #define callfunc_selector(_SELECTOR) (SEL_CallFunc)(&_SELECTOR) #define callfuncN_selector(_SELECTOR) (SEL_CallFuncN)(&_SELECTOR) #define callfuncND_selector(_SELECTOR) (SEL_CallFuncND)(&_SELECTOR) #define callfuncO_selector(_SELECTOR) (SEL_CallFuncO)(&_SELECTOR) #define menu_selector(_SELECTOR) (SEL_MenuHandler)(&_SELECTOR) #define event_selector(_SELECTOR) (SEL_EventHandler)(&_SELECTOR) #define compare_selector(_SELECTOR) (SEL_Compare)(&_SELECTOR)
CCObject作为cocos2dx中大部分类的父类,提供了
1)内存管理功能
2)为脚本提供对象ID管理
3)序列化接口
4)提供update接口