Here’s mine, good sir. Rather than worry about the way that someone makes classes, I made it so that you pass in a reference to a function that returns a new object. There are also functions for destroying an object, and others that get called when the object is “borrowed” and “returned” (for re-initialization, if you need it). Only createObject is a required param for new().
Usage:
[lua]local objectpool = require(“objectpool”)
local myPool = objectpool.new(
{
– Ensures that no more than 10 objects are cached in the pool (optional)
maxSize = 10,
– Creates the object (required)
createObject = function()
{
local image = display.newImage(“test.png”)
image:setReferencePoint(display.TopLeftReferencePoint)
},
– Initializes (or re-initializes) the object (optional)
onBorrow = function(image)
{
image.x = 0
image.y = 0
}
– Not used here, but available: destroyObject, onReturn (both optional)
})
– Borrow an object from the pool. Triggers createObject, if required, and onBorrow, if it exists.
local image = myPool:borrowObject()
– Return the object to the pool. Always triggers onReturn, if it exists. May trigger destroyObject, if the pool’s cache is full (maxSize).
myPool:returnObject(image)
– Fills the pool’s cache, up to maxSize. Optional single argument will override maxSize (useful if maxSize isn’t set)
myPool:populate() – or myPool.populate(5)
– Empties the pool’s cache, destroying all objects.
myPool:clear()[/lua]
objectpool.lua:
[lua]–[[
objectpool.lua
Copyright © 2011 Josh Tynjala
Released under the MIT license.
]]–
module(…, package.seeall)
function new(params)
local pool = {}
pool.maxSize = params.maxSize
pool.createObject = params.createObject
if pool.createObject == nil then
error(“createObject function required by ObjectPool”)
end
pool.destroyObject = params.destroyObject
pool.onBorrow = params.onBorrow
pool.onReturn = params.onReturn
local cache = {}
function pool:borrowObject()
local object
if # cache > 0 then
object = table.remove(cache, 1)
else
object = self.createObject()
if object == nil then
error(“ObjectPool:createObject() doesn’t return a value”)
end
end
if self.onBorrow ~= nil then
self.onBorrow(object)
end
return object
end
function pool:returnObject(object)
if object == nil then
return
end
if self.onReturn ~= nil then
self.onReturn(object)
end
if self.maxSize ~= nil and # cache >= self.maxSize then
if self.destroyObject ~= nil then
self.destroyObject(object)
end
return
end
table.insert(cache, object)
end
function pool:populate(size)
if size == nil and self.maxSize == nil then
error(“ObjectPool:populate requires size parameter or maxSize property.”)
end
if size == nil then
size = self.maxSize
end
local objects = {}
for i = 1,size do
table.insert(objects, self:borrowObject())
end
for i,object in ipairs(objects) do
self:returnObject(object)
end
end
function pool:clear()
while # cache > 0 do
local object = table.remove(cache, 1)
if self.destroyObject ~= nil then
self.destroyObject(object)
end
end
end
return pool
end[/lua] [import]uid: 38950 topic_id: 14258 reply_id: 52582[/import]