#ifndef __CCOBJECT_H__
#define __CCOBJECT_H__
#include "CCDataVisitor.h"
#ifdef EMSCRIPTEN
#include <GLES2/gl2.h>
#endif // EMSCRIPTEN
NS_CC_BEGIN
class CCZone;
class CCObject;
class CCNode;
class CCEvent;
class CC_DLL CCCopying
{
public:
virtual CCObject* copyWithZone(CCZone* pZone);
};
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; //引用次数 每次引用+1 引用计数的内存管理模式
// count of autorelease
unsigned int m_uAutoReleaseCount; //初始0 托管(加入自动释放池)后>0 (每加入一次+1)
public:
CCObject(void);
virtual ~CCObject(void);
void release(void);
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);
CCObject* CCObject::autorelease(void)
{
CCPoolManager::sharedPoolManager()->addObject(this);
return this;
}
CCObject* copy(void);
bool isSingleReference(void) const;
unsigned int retainCount(void) const;
virtual bool isEqual(const CCObject* pObject);
virtual void acceptVisitor(CCDataVisitor &visitor);
void CCObject::acceptVisitor(CCDataVisitor &visitor)
{
visitor.visitObject(this);
}
virtual void update(float dt) {CC_UNUSED_PARAM(dt);};
friend class CCAutoreleasePool; //需要用到protected的m_uReference 和 m_uAutoReleaseCount
//
友元函数百度百科:
友元函数是指某些虽然不是类成员却能够访问类的所有成员的函数。。类授予它的友元特别的访问权。通常同一个开发者会出于技术和非技术的原因,控制类的友元和成员函数(否则当你想更新你的类时,还要征得其它部分的拥有者的同意)。
友元类百度百科:
友元类的所有成员函数都是另一个类的友元函数,都可以访问另一个类中的隐藏信息(包括私有成员和保护成员)。
- class A
- {
- int n;
- public:
- A() { n=5; };
- friend class B; // 说明友元类B
- };
- class B
- {
- public:
- void display(A temp)
- {
- cout << "n=" <<temp.n<<endl;
- }
- };
};
//回调函数
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)
// end of base_nodes group
/// @}
NS_CC_END
#endif // __CCOBJECT_H__