-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool.lua
More file actions
54 lines (43 loc) · 1.02 KB
/
pool.lua
File metadata and controls
54 lines (43 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
local currentPath = (...):match('(.-)[^%./]+$')
--- @class IUILib
local iui = require(currentPath .. "iui")
--- @class IUIPool
local pool = {}
--- @class (exact) IUIPoolStack
--- @field top number
--- @field items any[]
--- @type table<string, IUIPoolStack>
local entries = {}
--- @param typename string
--- @return IUIPoolStack
local function getPool(typename)
local out = entries[typename]
if not out then
out = { top = 0, items = {} }
entries[typename] = out
end
return out
end
--- @generic T
--- @param typename string
--- @return T object
function pool.get(typename)
local stack = getPool(typename)
if stack.top == 0 then
return {
_typename = typename
}
else
local value = stack.items[stack.top]
stack.top = stack.top - 1
return value
end
end
--- @generic T
--- @param obj T
function pool.put(obj)
local stack = getPool(obj["_typename"])
stack.top = stack.top + 1
stack.items[stack.top] = obj
end
iui.pool = pool