Example #1
0
    /// <summary>
    /// Gets an object out of the pool. Depending on the pool's configuration,
    /// THIS COULD BE NULL!
    ///
    /// Furthermore, the retrieved object should be used/activated immediately after
    /// calling this function. (What that means is up to the pooler; check its
    /// documentation.) Otherwise, subsequent method calls may yield control of the
    /// pooled object to someone else.
    ///
    /// Also, it's a bad idea to maintain a reference to a pooled object after it
    /// becomes inactive. Use repoolDelegate if you plan on maintaining a reference
    /// to the given object beyond the scope of one function.
    /// </summary>
    ///
    /// <returns>One of the objects ouf of the pool or null.</returns>
    ///
    /// <param name="repoolDelegate">
    /// Optional delegate which gets called as soon as the object is called up again.
    /// Please avoid calling GetPooledObject within the delegate.
    /// </param>
    public TObj GetPooledObject(ObjectRepooler repoolDelegate = null)
    {
        pooledObject grabbedObj = null;

        int index = 0;

        while (index < pool.Count && grabbedObj == null)
        {
            if (pool[index].obj == null)
            {
                // One of the elements of the list is null; repopulate that element.
                grabbedObj  = new pooledObject(CreateObject());
                pool[index] = grabbedObj;
            }
            else if (IsObjectInPool(pool[index].obj))
            {
                grabbedObj = pool[index];
            }
            else
            {
                index++;
            }
        }

        if (grabbedObj == null && willGrow)
        {
            grabbedObj = AddObjectToPool();
        }

        if (grabbedObj.returnToPool != null)
        {
            grabbedObj.returnToPool(grabbedObj.obj);
        }

        grabbedObj.returnToPool = repoolDelegate;

        return(grabbedObj.obj);
    }
Example #2
0
 public pooledObject(TObj newObj)
 {
     _obj         = newObj;
     returnToPool = null;
 }