axmol/cocos/2d/renderer/RenderCommandPool.h

85 lines
1.8 KiB
C
Raw Normal View History

2013-12-03 11:52:51 +08:00
//
// RenderCommandPool.h
// cocos2d_libs
//
// Created by Huabing on 11/28/13.
//
//
#ifndef __CC_RENDERCOMMANDPOOL_H__
#define __CC_RENDERCOMMANDPOOL_H__
#include <set>
#include <list>
#include "CCPlatformMacros.h"
NS_CC_BEGIN
template <class T>
class RenderCommandPool
{
public:
RenderCommandPool()
{
}
~RenderCommandPool()
{
// if( 0 != _usedPool.size())
// {
// CCLOG("All RenderCommand should not be used when Pool is released!");
// }
2013-12-04 10:39:34 +08:00
_freePool.clear();
for (typename std::list<T*>::iterator iter = _allocatedPoolBlocks.begin(); iter != _allocatedPoolBlocks.end(); ++iter)
2013-12-03 11:52:51 +08:00
{
2013-12-04 10:39:34 +08:00
delete[] *iter;
2013-12-03 11:52:51 +08:00
*iter = nullptr;
}
2013-12-04 10:39:34 +08:00
_allocatedPoolBlocks.clear();
2013-12-03 11:52:51 +08:00
}
public:
T* generateCommand()
{
T* result = nullptr;
2013-12-04 10:39:34 +08:00
if(_freePool.empty())
2013-12-03 11:52:51 +08:00
{
2013-12-04 10:39:34 +08:00
AllocateCommands();
2013-12-03 11:52:51 +08:00
}
2013-12-04 10:39:34 +08:00
result = _freePool.front();
_freePool.pop_front();
2013-12-03 11:52:51 +08:00
//_usedPool.insert(result);
return result;
}
void pushBackCommand(T* ptr)
{
// if(_usedPool.find(ptr) == _usedPool.end())
// {
// CCLOG("push Back Wrong command!");
// return;
// }
_freePool.push_back(ptr);
//_usedPool.erase(ptr);
}
private:
2013-12-04 10:39:34 +08:00
void AllocateCommands()
{
static const int COMMANDS_ALLOCATE_BLOCK_SIZE = 32;
T* commands = new T[COMMANDS_ALLOCATE_BLOCK_SIZE];
_allocatedPoolBlocks.push_back(commands);
for(int index = 0; index < COMMANDS_ALLOCATE_BLOCK_SIZE; ++index)
{
_freePool.push_back(commands+index);
}
}
private:
std::list<T*> _allocatedPoolBlocks;
2013-12-03 11:52:51 +08:00
std::list<T*> _freePool;
//std::set<T*> _usedPool;
};
NS_CC_END
#endif