cocos2d-x 3.1.1源码阅读过程的注释

前端之家收集整理的这篇文章主要介绍了cocos2d-x 3.1.1源码阅读过程的注释前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
cocos2d-x 3.1.1源码阅读过程的注释
Ref
@H_404_25@每个类的基类是Ref 也就是2.0的CCObject 调用继承下来的下面的那个函数

class CC_DLL Ref
@H_404_25@{
public @H_404_25@:
@H_404_25@ /**
引用计数+1
*/
@H_404_25@ void @H_404_25@retain();
@H_404_25@{
@H_404_25@ CCASSERT @H_404_25@(_referenceCount > 0, "reference count should greater than 0" @H_404_25@);
@H_404_25@ ++_referenceCount;
@H_404_25@}
@H_404_25@ /**
引用计数-1
*/
@H_404_25@ void @H_404_25@release();
@H_404_25@{
@H_404_25@ CCASSERT @H_404_25@(_referenceCount > 0, "reference count should greater than 0" @H_404_25@);
@H_404_25@ --_referenceCount;
@H_404_25@ if @H_404_25@(_referenceCount == 0)
@H_404_25@ {
#if defined @H_404_25@( COCOS2D_DEBUG @H_404_25@) && ( COCOS2D_DEBUG @H_404_25@> 0)
@H_404_25@ auto @H_404_25@poolManager = PoolManager @H_404_25@::getInstance();
@H_404_25@ if @H_404_25@(!poolManager->getCurrentPool()->isClearing() && poolManager->isObjectInPools( this @H_404_25@))
@H_404_25@ {
@H_404_25@ // 错误实例1:
@H_404_25@ // auto obj = Node::create();
@H_404_25@ // obj->autorelease();
//create函数里面封装了 autorelease
@H_404_25@
@H_404_25@ // 错误实例2:
@H_404_25@ // auto obj = Node::create();
@H_404_25@ // obj->release(); // Wrong: obj is an autorelease Ref,it will be released when clearing current pool.

@H_404_25@ CCASSERT @H_404_25@( false @H_404_25@, "The reference shouldn't be 0 because it is still in autorelease pool." @H_404_25@);
@H_404_25@ }
#endif
@H_404_25@
#if CC_USE_MEM_LEAK_DETECTION
@H_404_25@ untrackRef( this @H_404_25@);
#endif
@H_404_25@ delete this @H_404_25@;
@H_404_25@ }
@H_404_25@ /**
* 自动释放 也就是当前的AutoReleasePool被调用了析构函数之后 引擎全局有一个 AutoReleasePool
*/
@H_404_25@ Ref @H_404_25@* autorelease();
@H_404_25@{
@H_404_25@ PoolManager @H_404_25@::getInstance()->getCurrentPool()->addObject( this @H_404_25@);
@H_404_25@ return this @H_404_25@;
@H_404_25@}
@H_404_25@ /**
* 返回引用数量
*/
@H_404_25@ unsigned int @H_404_25@getReferenceCount() const @H_404_25@;
@H_404_25@
protected @H_404_25@:
@H_404_25@ /**
* 在构造了之后引用计数是1!!!!!
*/
@H_404_25@ Ref()
@H_404_25@: _referenceCount(1) // when the Ref is created,the reference count of it is 1
@H_404_25@{
#if CC_ENABLE_SCRIPT_BINDING
@H_404_25@ static unsigned int @H_404_25@uObjectCount = 0;
@H_404_25@ _luaID = 0;
@H_404_25@ _ID = ++uObjectCount;
#endif
#if CC_USE_MEM_LEAK_DETECTION
@H_404_25@ trackRef( this @H_404_25@);
#endif
@H_404_25@}
@H_404_25@
public @H_404_25@:
@H_404_25@ virtual @H_404_25@~Ref();
protected @H_404_25@:
@H_404_25@ /// count of references
@H_404_25@ unsigned int @H_404_25@_referenceCount;
@H_404_25@ friend class AutoreleasePool @H_404_25@;
#if CC_ENABLE_SCRIPT_BINDING
public @H_404_25@:
@H_404_25@ /// object id,ScriptSupport need public _ID
@H_404_25@ unsigned int @H_404_25@ _ID;
@H_404_25@ /// Lua reference id
@H_404_25@ int @H_404_25@ _luaID;
#endif
#if CC_USE_MEM_LEAK_DETECTION
public @H_404_25@:
@H_404_25@ static void @H_404_25@printLeaks();
#endif
@H_404_25@};
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@
PoolManager类的解析:
class CC_DLL PoolManager
@H_404_25@{
public @H_404_25@:
@H_404_25@ //这个是表示这个接口以后要丢弃
@H_404_25@ CC_DEPRECATED_ATTRIBUTE static PoolManager @H_404_25@* sharedPoolManager() { return @H_404_25@getInstance(); }
@H_404_25@
@H_404_25@ /**
这里也是十分重要的 整个程序只有一个单例的PoolManager PoolManager初始化的时候就添加了一个 AutoreleasePool
*/
@H_404_25@ static PoolManager @H_404_25@* getInstance();
@H_404_25@{
@H_404_25@ if @H_404_25@(s_singleInstance == nullptr @H_404_25@)
@H_404_25@ {
@H_404_25@ s_singleInstance = new PoolManager @H_404_25@();
@H_404_25@ // Add the first auto release pool
@H_404_25@ s_singleInstance->_curReleasePool = new AutoreleasePool @H_404_25@( "cocos2d autorelease pool" @H_404_25@);
@H_404_25@ s_singleInstance->_releasePoolStack.push_back(s_singleInstance->_curReleasePool);
@H_404_25@ }
@H_404_25@ return @H_404_25@s_singleInstance;
@H_404_25@}
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@ CC_DEPRECATED_ATTRIBUTE static void @H_404_25@purgePoolManager() { destroyInstance(); }
@H_404_25@ static void @H_404_25@destroyInstance();
@H_404_25@
@H_404_25@ /**
* 获得现在的释放池,引擎自己创建了一个autoreleasePool
* 你可以创建自己的释放池 会放进自动释放池的盏变量里面
*/
@H_404_25@ AutoreleasePool @H_404_25@*getCurrentPool() const @H_404_25@;
@H_404_25@{
@H_404_25@ return @H_404_25@_curReleasePool;
@H_404_25@}
@H_404_25@ bool @H_404_25@isObjectInPools( Ref @H_404_25@* obj) const @H_404_25@;
@H_404_25@
@H_404_25@ friend class AutoreleasePool @H_404_25@;
private @H_404_25@:
@H_404_25@ PoolManager();
@H_404_25@ ~PoolManager();
@H_404_25@ void @H_404_25@push( AutoreleasePool @H_404_25@*pool);
@H_404_25@{
@H_404_25@ _releasePoolStack.push_back( pool @H_404_25@);
@H_404_25@ _curReleasePool = pool @H_404_25@;
@H_404_25@}
@H_404_25@ void @H_404_25@pop();
@H_404_25@{
@H_404_25@ // 如果是弹出第一个
@H_404_25@ CC_ASSERT @H_404_25@(_releasePoolStack.size() >= 1);
@H_404_25@
@H_404_25@ _releasePoolStack.pop_back();
@H_404_25@
@H_404_25@ // 应该更新 _curReleasePool
@H_404_25@ if @H_404_25@(_releasePoolStack.size() > 1)
@H_404_25@ {
@H_404_25@ _curReleasePool = _releasePoolStack.back();
@H_404_25@ }
@H_404_25@}
@H_404_25@
@H_404_25@ static PoolManager @H_404_25@* s_singleInstance;//单例模式
@H_404_25@ std:: deque @H_404_25@< AutoreleasePool @H_404_25@*> _releasePoolStack;//管理用户创建的自动释放池用的盏
@H_404_25@ AutoreleasePool @H_404_25@*_curReleasePool;//现在的自动释放池
@H_404_25@};
@H_404_25@
AutoreleasePool
class CC_DLL AutoreleasePool
@H_404_25@{
public @H_404_25@:
@H_404_25@ /**
* 提示自动释放池对象要创建在栈里面 不能再堆里面
* 创建的时候就自动push进PoolManager里面
*/
@H_404_25@ AutoreleasePool();
@H_404_25@{
@H_404_25@ _managedObjectArray.reserve(150);//vector扩大容量
@H_404_25@ PoolManager @H_404_25@::getInstance()->push( this @H_404_25@);
@H_404_25@}
@H_404_25@ /**
* 用引用来创建 是为了调试
*/
@H_404_25@ AutoreleasePool( const @H_404_25@std:: string @H_404_25@&name);
@H_404_25@ {
@H_404_25@ _managedObjectArray.reserve(150);
@H_404_25@ PoolManager @H_404_25@::getInstance()->push( this @H_404_25@);
@H_404_25@}

// 枚举每一个加进对象池的obj去调用release 并且清空
@H_404_25@ ~AutoreleasePool();
@H_404_25@{
@H_404_25@ CCLOGINFO @H_404_25@( "deallocing AutoreleasePool: %p" @H_404_25@, this @H_404_25@);
@H_404_25@ clear();//
@H_404_25@ PoolManager @H_404_25@::getInstance()->pop();
@H_404_25@}
@H_404_25@ /**
* 添加对象到对象池中
* 对象池销毁的时候会调用
*/
@H_404_25@ void @H_404_25@addObject( Ref @H_404_25@*object);
@H_404_25@{
@H_404_25@ _managedObjectArray.push_back( object @H_404_25@);
@H_404_25@}
@H_404_25@
@H_404_25@ /**
清理对象池 析构函数调用
*/
@H_404_25@ void @H_404_25@clear();
@H_404_25@ {
#if defined @H_404_25@( COCOS2D_DEBUG @H_404_25@) && ( COCOS2D_DEBUG @H_404_25@> 0)
@H_404_25@ _isClearing = true @H_404_25@;
#endif
@H_404_25@ for @H_404_25@( const auto @H_404_25@&obj : _managedObjectArray)
@H_404_25@ {
@H_404_25@ //枚举每一个加进对象池的obj去调用release
@H_404_25@ obj->release();
@H_404_25@ }
@H_404_25@
@H_404_25@ //把vector清空
@H_404_25@ _managedObjectArray.clear();
#if defined @H_404_25@( COCOS2D_DEBUG @H_404_25@) && ( COCOS2D_DEBUG @H_404_25@> 0)
@H_404_25@ _isClearing = false @H_404_25@;
#endif
@H_404_25@}
@H_404_25@
@H_404_25@
@H_404_25@
#if defined @H_404_25@( COCOS2D_DEBUG @H_404_25@) && ( COCOS2D_DEBUG @H_404_25@> 0)
@H_404_25@ /**
* Whether the pool is doing `clear` operation.
*/
@H_404_25@ bool @H_404_25@isClearing() const @H_404_25@{ return @H_404_25@_isClearing; };
#endif
@H_404_25@
@H_404_25@ /**
* 检查是否包含
* 枚举一遍这个vector
*/
@H_404_25@ bool @H_404_25@contains( Ref @H_404_25@* object) const @H_404_25@;
@H_404_25@{
@H_404_25@ for @H_404_25@( const auto @H_404_25@& obj : _managedObjectArray)
@H_404_25@ {
@H_404_25@ if @H_404_25@(obj == object @H_404_25@)
@H_404_25@ return true @H_404_25@;
@H_404_25@ }
@H_404_25@ return false @H_404_25@;
@H_404_25@}
@H_404_25@
@H_404_25@
@H_404_25@ /**
* 用来调试
*
*/
@H_404_25@ void @H_404_25@dump();
@H_404_25@ {
@H_404_25@ CCLOG @H_404_25@( "autorelease pool: %s,number of managed object %d\n" @H_404_25@,_name.c_str(), static_cast @H_404_25@< int @H_404_25@>(_managedObjectArray.size()));
@H_404_25@ CCLOG @H_404_25@( "%20s%20s%20s" @H_404_25@, "Object pointer" @H_404_25@, "Object id" @H_404_25@, "reference count" @H_404_25@);
@H_404_25@ for @H_404_25@( const auto @H_404_25@&obj : _managedObjectArray)
@H_404_25@ {
@H_404_25@ CC_UNUSED_PARAM @H_404_25@(obj);
@H_404_25@ CCLOG @H_404_25@( "%20p%20u\n" @H_404_25@,obj,obj->getReferenceCount());
@H_404_25@ }
@H_404_25@}
@H_404_25@
@H_404_25@
private @H_404_25@:
@H_404_25@ /**
* The underlying array of object managed by the pool.
*
* Although Array retains the object once when an object is added,proper
* Ref::release() is called outside the array to make sure that the pool
* does not affect the managed object's reference count. So an object can
* be destructed properly by calling Ref::release() even if the object
* is in the pool.
*/
@H_404_25@ std:: vector @H_404_25@< Ref @H_404_25@*> _managedObjectArray;
@H_404_25@ std:: string @H_404_25@_name;
@H_404_25@
#if defined @H_404_25@( COCOS2D_DEBUG @H_404_25@) && ( COCOS2D_DEBUG @H_404_25@> 0)
@H_404_25@ /**
* The flag for checking whether the pool is doing `clear` operation.
*/
@H_404_25@ bool @H_404_25@_isClearing;
#endif
@H_404_25@};
@H_404_25@
@H_404_25@
@H_404_25@
@H_404_25@下面这张图是网上找的类图继承图:
@H_404_25@
@H_404_25@
原文链接:https://www.f2er.com/cocos2dx/347082.html

猜你在找的Cocos2d-x相关文章