1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * <p>
 29  *  cc.pool is a singleton object serves as an object cache pool.<br/>
 30  *  It can helps you to improve your game performance for objects which need frequent release and recreate operations<br/>
 31  *  Some common use case is :
 32  *      1. Bullets in game (die very soon, massive creation and recreation, no side effect on other objects)
 33  *      2. Blocks in candy crash (massive creation and recreation)
 34  *      etc...
 35  * </p>
 36  *
 37  * @example
 38  * var sp = new cc.Sprite("a.png");
 39  * this.addChild(sp);
 40  * cc.pool.putInPool(sp);
 41  *
 42  * cc.pool.getFromPool(cc.Sprite, "a.png");
 43  * @namespace
 44  * @name cc.pool
 45  */
 46 cc.pool = /** @lends cc.pool# */{
 47     _pool: {},
 48 
 49     /**
 50      * Put the obj in pool
 51      * @param obj
 52      */
 53     putInPool: function (obj) {
 54         if (obj instanceof cc.Node) {
 55             var pid = obj.constructor.prototype.__pid;
 56             if (!pid) {
 57                 var desc = { writable: true, enumerable: false, configurable: true };
 58                 desc.value = ClassManager.getNewID();
 59                 Object.defineProperty(obj.constructor.prototype, '__pid', desc);
 60             }
 61             if (!this._pool[pid]) {
 62                 this._pool[pid] = [];
 63             }
 64             obj.unuse();
 65             obj.retain();//use for jsb
 66             this._pool[pid].push(obj);
 67         }
 68     },
 69 
 70     /**
 71      * Check if this kind of obj has already in pool
 72      * @param objClass
 73      * @returns {boolean} if this kind of obj is already in pool return true,else return false;
 74      */
 75     hasObj: function (objClass) {
 76         var pid = objClass.prototype.__pid;
 77         var list = this._pool[pid];
 78         if (!list || list.length == 0) {
 79             return false;
 80         }
 81         return true;
 82     },
 83 
 84     /**
 85      * Remove the obj if you want to delete it;
 86      * @param obj
 87      */
 88     removeObj: function (obj) {
 89         var pid = obj.constructor.prototype.__pid;
 90         if (pid) {
 91             var list = this._pool[pid];
 92             if (list) {
 93                 for (var i = 0; i < list.length; i++) {
 94                     if (obj === list[i]) {
 95                         obj.release()//use for jsb
 96                         list.splice(i, 1);
 97                     }
 98                 }
 99             }
100         }
101     },
102 
103     /**
104      * Get the obj from pool
105      * @param args
106      * @returns {*} call the reuse function an return the obj
107      */
108     getFromPool: function (objClass/*,args*/) {
109         if (this.hasObj(objClass)) {
110             var pid = objClass.prototype.__pid;
111             var list = this._pool[pid];
112             var args = Array.prototype.slice.call(arguments);
113             args.shift();
114             var obj = list.pop();
115             obj.reuse.apply(obj, args);
116             return obj;
117         }
118     },
119 
120     /**
121      *  remove all objs in pool and reset the pool
122      */
123     drainAllPools: function () {
124         for (var i in this._pool) {
125             for (var j = 0; j < this._pool[i].length; j++) {
126                 var obj = this._pool[i][j];
127                 obj.release()
128             }
129         }
130         this._pool = {};
131     }
132 };