Last Updated: February 25, 2016
·
1.394K
· designerkamal

Haxe ObjectPooling

package;

import nme.errors.Error;
class ObjectPool<T>
{
private var pool:Array<T>;
private var counter:Int;

public function new(_pooledType:Class<T>,len:Int)
{
    pool = new Array();
    counter = len;

    var i:Int = len;
    while(--i > -1)
    {
        pool[i] = Type.createInstance(_pooledType,[]) ;
    }
}

public function getObject():T
{
    if(counter > 0)
        return pool[--counter];
    else
        throw new Error("You exhausted the pool!");
}

public function disposeObject(s:T):Void
{
    pool[counter++] = s;
}

}