-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathOO.lua
More file actions
245 lines (215 loc) · 6.08 KB
/
OO.lua
File metadata and controls
245 lines (215 loc) · 6.08 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
--[[
AdiBags - Adirelle's bag addon.
Copyright 2010-2011 Adirelle (adirelle@tagada-team.net)
All rights reserved.
--]]
local addonName, addon = ...
local safecall = addon.safecall
--<GLOBALS
local _G = _G
local assert = _G.assert
local CreateFrame = _G.CreateFrame
local format = _G.format
local next = _G.next
local pairs = _G.pairs
local print = _G.print
local select = _G.select
local setmetatable = _G.setmetatable
local SlashCmdList = _G.SlashCmdList
local tostring = _G.tostring
--GLOBALS>
--------------------------------------------------------------------------------
-- Classes
--------------------------------------------------------------------------------
local classes = {}
local function Meta_ToString(self)
return self:ToString()
end
local function Class_Create(class, ...)
class.serial = class.serial + 1
local self = CreateFrame(class.frameType, addonName..class.name..class.serial, nil, class.frameTemplate)
setmetatable(self, class.metatable)
self:ClearAllPoints()
self:Hide()
safecall(self, "OnCreate", ...)
return self
end
local function NewClass(name, parent, ...)
local prototype, mixins = {}, {}
local class = {
name = name,
prototype = prototype,
parent = parent,
mixins = mixins,
serial = 0,
metatable = {
__index = prototype,
__tostring = Meta_ToString
},
Create = Class_Create,
}
if parent.mixins then
setmetatable(mixins, { __index = parent.mixins })
end
for i = 1, select('#', ...) do
local name = select(i, ...)
if not mixins[name] then
local mixin = LibStub(name)
mixins[name] = mixin
safecall(mixin, "Embed", prototype)
end
end
prototype.class = class
prototype.Debug = addon.Debug
if parent.prototype then
setmetatable(class, { __index = parent })
setmetatable(prototype, { __index = parent.prototype })
return class, prototype, parent.prototype
else
setmetatable(prototype, { __index = parent })
return class, prototype, parent
end
end
local function NewRootClass(name, frameType, frameTemplate, ...)
local class, prototype, parent
if frameTemplate and LibStub(frameTemplate, true) then
class, prototype, parent = NewClass(name, CreateFrame(frameType), frameTemplate, ...)
frameTemplate = nil
else
class, prototype, parent = NewClass(name, CreateFrame(frameType), ...)
end
class.frameType = frameType
class.frameTemplate = frameTemplate
prototype.ToString = parent.GetName
return class, prototype, parent
end
function addon:NewClass(name, frameType, ...)
local class, prototype, parent
if classes[frameType] then
class, prototype, parent = NewClass(name, classes[frameType], ...)
else
class, prototype, parent = NewRootClass(name, frameType, ...)
end
classes[name] = class
return class, prototype, parent
end
function addon:GetClass(name)
return name and classes[name]
end
--------------------------------------------------------------------------------
-- Object pools
--------------------------------------------------------------------------------
local pools = {}
local poolProto = {}
local poolMeta = { __index = poolProto }
function poolProto:Acquire(...)
local object = next(self.heap)
if object then
assert(not object.acquired, "Found an acquired object in the pool")
self.heap[object] = nil
else
object = self.class:Create()
end
self.actives[object] = true
object.acquired = true
for name, mixin in pairs(self.class.mixins) do
safecall(mixin, "OnEmbedEnable", object)
end
safecall(object, "OnAcquire", ...)
return object
end
function poolProto:Release(object)
assert(object.acquired, "Trying to release an object that wasn't acquired")
object:Hide()
object:ClearAllPoints()
object:SetParent(nil)
safecall(object, "OnRelease")
for name, mixin in pairs(self.class.mixins) do
safecall(mixin, "OnEmbedDisable", object)
end
object.acquired = nil
self.actives[object] = nil
self.heap[object] = true
end
function poolProto:PreSpawn(number)
for i = 1, number do
local object = self.class:Create()
self.heap[object] = true
end
end
local function PoolIterator(data, current)
current = next(data.pool[data.attribute], current)
if current == nil and data.attribute == "heap" then
data.attribute = "actives"
return next(data.pool.actives)
end
return current
end
function poolProto:IterateAllObjects()
return PoolIterator, { pool = self, attribute = "heap" }
end
function poolProto:IterateHeap()
return pairs(self.heap)
end
function poolProto:IterateActiveObjects()
return pairs(self.actives)
end
local function Instance_Release(self)
return self.class.pool:Release(self)
end
function addon:CreatePool(class, acquireMethod)
local pool = setmetatable({
heap = {},
actives = {},
class = class,
}, poolMeta)
class.pool = pool
class.prototype.Release = Instance_Release
pools[class.name] = pool
if acquireMethod then
self[acquireMethod] = function(self, ...) return pool:Acquire(...) end
end
return pool
end
function addon:GetPool(name)
return name and pools[name]
end
--[===[@debug@
-- Globals: SLASH_ADIBAGSOODEBUG1
SLASH_ADIBAGSOODEBUG1 = "/aboo"
function SlashCmdList.ADIBAGSOODEBUG()
print('Classes:')
for name, class in pairs(classes) do
print(format("- %s: type: %s, template: %s, serial: %d", name, class.frameType, tostring(class.frameTemplate), class.serial))
end
print('Pools:')
for name, pool in pairs(pools) do
local heapSize, numActives = 0, 0
for k in pairs(pool.activtes) do
numActives = numActives + 1
end
for k in pairs(pool.heap) do
heapSize = heapSize + 1
end
print(format("- %s: heap size: %d, number of active objects: %d", name, heapSize, numActives))
end
end
--@end-debug@]===]
-- Define a global flag to keep track of debug state
addon.debugEnabled = false
-- Slash command to toggle debug
SLASH_ADIBAGSOODEBUG1 = "/aboo"
function SlashCmdList.ADIBAGSOODEBUG()
-- Toggle the debug state
if addon.debugEnabled then
-- Disable debugging
addon.debugEnabled = false
addon.Debug = function() end -- Set it to a no-op function
print("Debugging is now disabled.")
else
-- Enable debugging
addon.debugEnabled = true
addon.Debug = function(...) print("AdiBags Debug:", ...) end -- Enable debug printing
print("Debugging is now enabled.")
end
end