forked from ShortTailLab/ph-open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathObjectPool.h
82 lines (70 loc) · 1.35 KB
/
ObjectPool.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// ObjectPool.h
// HelloCpp
//
// Created by zql on 13-4-11.
//
//
#pragma once
#include <cocos2d.h>
#include "BattleScene.h"
#include "JiangHu.h"
USING_NS_CC;
namespace PH
{
template<class T>
class ObjectPool
{
public:
//T hint a CCNode or its children
T* getObject()
{
T* t = NULL;
if(freeObjs.size() > 0)
{
t = *(freeObjs.erase(freeObjs.end()-1));
}
else
{
t = T::create();
t->retain();
}
inUseObjs.push_back(t);
return t;
}
T* getObject(Stage& stage, bool progressive);
void collectObj(T* obj);
void collect()
{
for(T* t : inUseObjs)
{
if(t->getParent())
t->removeFromParentAndCleanup(true);
freeObjs.push_back(t);
}
//freeObjs.insert(freeObjs.end(), inUseObjs.begin(), inUseObjs.end());
inUseObjs.clear();
}
void clear()
{
for(T* t : inUseObjs)
t->release();
for(T* t : freeObjs)
t->release();
inUseObjs.clear();
freeObjs.clear();
}
static ObjectPool *getInstance()
{
static ObjectPool *o = NULL;
if(!o)
{
o = new ObjectPool();
}
return o;
}
public:
std::vector<T*> freeObjs;
std::vector<T*> inUseObjs;
};
}