Clean up table pool modules.
Johnny C. Lam [10-23-13 - 07:18]
Clean up table pool modules.
Modify constructors so that prototype inheritance works properly.
Remove the Reset() method and just call Drain() from the constructor to
empty out the pool to start.
git-svn-id: svn://svn.curseforge.net/wow/ovale/mainline/trunk@1092 d5049fe3-3747-40f7-a4b5-f36d6801af5f
diff --git a/OvalePool.lua b/OvalePool.lua
index e150157..f6536d0 100644
--- a/OvalePool.lua
+++ b/OvalePool.lua
@@ -29,18 +29,19 @@ OvalePool.unused = 0
OvalePool.__index = OvalePool
--</public-static-properties>
---<private-static-methods>
+--<public-static-methods>
do
- local function NewPool(...)
- local obj = setmetatable({ name = ... }, OvalePool)
- obj:Reset()
- return obj
- end
- setmetatable(OvalePool, { __call = function(_, ...) return NewPool(...) end })
+ -- Class constructor
+ setmetatable(OvalePool, { __call = function(self, ...) return self:NewPool(...) end })
+end
+
+function OvalePool:NewPool(name)
+ name = name or self.name
+ local obj = setmetatable({ name = name }, self)
+ obj:Drain()
+ return obj
end
---</private-static-methods>
---<public-static-methods>
function OvalePool:Get()
assert(self.pool)
local item = tremove(self.pool)
@@ -61,19 +62,8 @@ function OvalePool:Release(item)
end
function OvalePool:Drain()
- assert(self.pool)
- while true do
- if not tremove(self.pool) then
- break
- end
- end
- self.size = self.size - self.unused
- self.unused = 0
-end
-
-function OvalePool:Reset()
self.pool = {}
- self.size = 0
+ self.size = self.size - self.unused
self.unused = 0
end
diff --git a/OvalePoolGC.lua b/OvalePoolGC.lua
index 3f8d480..acb904d 100644
--- a/OvalePoolGC.lua
+++ b/OvalePoolGC.lua
@@ -23,29 +23,28 @@ OvalePoolGC.size = 0
OvalePoolGC.__index = OvalePoolGC
--</public-static-properties>
---<private-static-methods>
+--<public-static-methods>
do
- local function NewPool(...)
- local obj = setmetatable({ name = ... }, OvalePoolGC)
- obj:Reset()
- return obj
- end
- setmetatable(OvalePoolGC, { __call = function(_, ...) return NewPool(...) end })
+ -- Class constructor
+ setmetatable(OvalePoolGC, { __call = function(self, ...) return self:NewPool(...) end })
+end
+
+function OvalePoolGC:NewPool(name)
+ name = name or self.name
+ return setmetatable({ name = name }, self)
end
---</private-static-methods>
---<public-static-methods>
function OvalePoolGC:Get()
-- Keep running count of total number of tables allocated.
self.size = self.size + 1
return {}
end
--- The Release and Drain methods are no-ops.
-function OvalePoolGC:Release(item) end
-function OvalePoolGC:Drain() end
+function OvalePoolGC:Release(item)
+ -- no-op
+end
-function OvalePoolGC:Reset()
+function OvalePoolGC:Drain()
self.size = 0
end