mirror of https://github.com/axmolengine/axmol.git
153 lines
3.6 KiB
C++
153 lines
3.6 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "NSAutoReleasePool.h"
|
|
#include <assert.h>
|
|
#include <stack>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
namespace cocos2d {
|
|
|
|
NSAutoreleasePool::NSAutoreleasePool(void)
|
|
{
|
|
m_pManagedObjectArray = new NSMutableArray<NSObject*>();
|
|
}
|
|
|
|
NSAutoreleasePool::~NSAutoreleasePool(void)
|
|
{
|
|
delete m_pManagedObjectArray;
|
|
}
|
|
|
|
void NSAutoreleasePool::addObject(NSObject *pObject)
|
|
{
|
|
m_pManagedObjectArray->addObject(pObject);
|
|
}
|
|
|
|
void NSAutoreleasePool::removeObject(NSObject *pObject)
|
|
{
|
|
m_pManagedObjectArray->removeObject(pObject);
|
|
}
|
|
|
|
void NSAutoreleasePool::clear(void)
|
|
{
|
|
if (m_pManagedObjectArray->count())
|
|
{
|
|
NSMutableArray<NSObject*>::NSMutableArrayIterator iter;
|
|
for (iter = m_pManagedObjectArray->begin(); iter != m_pManagedObjectArray->end(); ++iter)
|
|
{
|
|
if (*iter)
|
|
{
|
|
(*iter)->m_bManaged = false;
|
|
}
|
|
}
|
|
|
|
m_pManagedObjectArray->removeAllObjects();
|
|
}
|
|
}
|
|
|
|
|
|
// implementiation of NSPoolManager
|
|
NSPoolManager* NSPoolManager::m_pPoolManager = NULL;
|
|
|
|
NSPoolManager* NSPoolManager::getInstance(void)
|
|
{
|
|
static bool bInit = false;
|
|
|
|
if (bInit == false)
|
|
{
|
|
bInit = true;
|
|
m_pPoolManager = new NSPoolManager();
|
|
}
|
|
|
|
return m_pPoolManager;
|
|
}
|
|
|
|
NSPoolManager::NSPoolManager(void)
|
|
{
|
|
m_pReleasePoolStack = new stack<NSAutoreleasePool *>();
|
|
m_pCurReleasePool = NULL;
|
|
}
|
|
|
|
NSPoolManager::~NSPoolManager(void)
|
|
{
|
|
finalize();
|
|
|
|
delete m_pReleasePoolStack;
|
|
}
|
|
|
|
void NSPoolManager::finalize(void)
|
|
{
|
|
if (m_pReleasePoolStack->size() > 0)
|
|
{
|
|
NSAutoreleasePool *pTop;
|
|
while (pTop = m_pReleasePoolStack->top())
|
|
{
|
|
pTop->clear();
|
|
m_pReleasePoolStack->pop();
|
|
}
|
|
}
|
|
}
|
|
|
|
void NSPoolManager::push(void)
|
|
{
|
|
NSAutoreleasePool *pPool = new NSAutoreleasePool();
|
|
m_pCurReleasePool = pPool;
|
|
|
|
m_pReleasePoolStack->push(pPool);
|
|
}
|
|
|
|
void NSPoolManager::pop(void)
|
|
{
|
|
if (! m_pReleasePoolStack->empty())
|
|
{
|
|
m_pReleasePoolStack->pop();
|
|
}
|
|
}
|
|
|
|
void NSPoolManager::addObject(NSObject *pObject)
|
|
{
|
|
getCurReleasePool()->addObject(pObject);
|
|
}
|
|
|
|
void NSPoolManager::removeObject(NSObject *pObject)
|
|
{
|
|
assert(m_pCurReleasePool);
|
|
|
|
m_pCurReleasePool->removeObject(pObject);
|
|
}
|
|
|
|
NSAutoreleasePool* NSPoolManager::getCurReleasePool(void)
|
|
{
|
|
if (! m_pCurReleasePool)
|
|
{
|
|
push();
|
|
}
|
|
|
|
assert(m_pCurReleasePool);
|
|
|
|
return m_pCurReleasePool;
|
|
}
|
|
}//namespace cocos2d {
|