-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.lua
More file actions
323 lines (260 loc) · 7.45 KB
/
class.lua
File metadata and controls
323 lines (260 loc) · 7.45 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
local unpack = table.unpack or unpack
return setmetatable({
includeHelper = function(self, tClass, tOther, tSeen)
if tOther == nil then
return tClass
end
if type(tOther) ~= "table" then
return tOther
end
if tSeen[tOther] then
return tSeen[tOther]
end
tSeen[tOther] = tClass
for sKey, vValue in pairs(tOther) do
if not tSeen[sKey] and sKey ~= "__parents" and tClass[sKey] == nil then
tClass[sKey] = self:includeHelper({}, vValue, tSeen)
end
end
return tClass
end,
debugInfo = function(self, tSelf)
local tMt = (type(tSelf) == "userdata") and getmetatable(tSelf) or tSelf
local tMethodSource = tMt
if type(tSelf) == "table" and rawget(tSelf, "__private") then
tMethodSource = getmetatable(tSelf) or tSelf
end
local tStr = {
"==========================",
"=== DEBUG OBJECT INFO ===",
"==========================",
"\nType: " .. (tMt.__type or "Unknown"),
"\n# Private Data:",
}
local tPrivateData = {}
if tMt.__private then
for k, v in pairs(tMt.__private) do
table.insert(tPrivateData, string.format(" - %s = %s", k, tostring(v)))
end
end
table.insert(tStr, #tPrivateData > 0 and table.concat(tPrivateData, "\n") or " (empty)")
table.insert(tStr, "\n# Methods:")
table.insert(
tStr,
table.concat(
(function()
local tMethods = {}
for sKey, vValue in pairs(tMethodSource) do
if type(vValue) == "function" then
table.insert(
tMethods,
string.format(" - %s type : %s", sKey, type(vValue))
)
end
end
return tMethods
end)(),
"\n"
)
)
table.insert(tStr, "\n==========================")
return table.concat(tStr, "\n")
end,
accessor = function(self, tSelf, tVarName, tName, tDefaultValue)
tSelf["Get" .. tName] = function(tSelf)
return tSelf.__private[tVarName]
end
tSelf["Set" .. tName] = function(tSelf, tV)
tSelf.__private[tVarName] = tV ~= nil and tV or tDefaultValue
end
end,
include = function(self, tClass, tOther)
return self:includeHelper(tClass, tOther, {})
end,
clone = function(self, tOther)
return setmetatable(
self:include({}, tOther),
assert(getmetatable(tOther), "Cannot clone an object without a metatable.")
)
end,
is = function(self, vValue, vExpected)
local sValueType = type(vValue)
local tValueMt = (sValueType == "table" or sValueType == "userdata") and getmetatable(vValue) or nil
local tParents = nil
if type(vExpected) == "string" then
if (tValueMt and tValueMt.__type == vExpected)
or (sValueType == "table" and rawget(vValue, "__type") == vExpected) then
return true
end
tParents = (tValueMt and rawget(tValueMt, "__parents"))
or (sValueType == "table" and rawget(vValue, "__parents"))
or nil
if tParents then
for tParent in pairs(tParents) do
if tParent.__type == vExpected then
return true
end
end
end
return false
end
if type(vExpected) ~= "table" then
return false
end
if vValue == vExpected or tValueMt == vExpected then
return true
end
tParents = (tValueMt and rawget(tValueMt, "__parents"))
or (sValueType == "table" and rawget(vValue, "__parents"))
or nil
return (tParents and tParents[vExpected]) and true or false
end,
assertIs = function(self, vValue, vExpected, sName)
if self:is(vValue, vExpected) then
return vValue
end
local sExpected = type(vExpected) == "table" and (vExpected.__type or "Unknown") or tostring(vExpected)
local sValueType = type(vValue)
local tValueMt = (sValueType == "table" or sValueType == "userdata") and getmetatable(vValue) or nil
local sValue = (tValueMt and tValueMt.__type)
or (sValueType == "table" and rawget(vValue, "__type"))
or sValueType
error("[CLASS] " .. (sName or "value") .. " must be " .. sExpected .. ", got " .. sValue .. ".", 2)
end,
overloadOperators = function(self, tClass)
assert(type(tClass) == "table", "[CLASS] ...")
tClass.__add = function(tA, tB)
assert(
tA.__type == tClass.__type,
"[CLASS] Attempted to add incompatible types: "
.. tA.__type
.. " and "
.. tB.__type
)
assert(
tB.__type == tClass.__type,
"[CLASS] Attempted to add incompatible types: "
.. tA.__type
.. " and "
.. tB.__type
)
local tResult = {}
for tKey, tValue in pairs(tA.__private) do
tResult[tKey] = tValue
end
for tKey, tValue in pairs(tB.__private) do
tResult[tKey] = tResult[tKey] or tValue
end
for tKey, tValue in pairs(tA.__privateMethods) do
tResult[tKey] = tValue
end
for tKey, tValue in pairs(tB.__privateMethods) do
tResult[tKey] = tResult[tKey] or tValue
end
return setmetatable(tResult, {
__type = tA.__type,
__privateMethods = tResult.__privateMethods or {},
})
end
end,
new = function(self, tClass)
tClass = tClass or {}
local tInc = getmetatable(tClass.__includes) and { tClass.__includes }
or tClass.__includes
or {}
tClass.__parents = tClass.__parents or {}
for _, tOther in ipairs(tInc) do
if type(tOther) == "string" then
tOther = _G[tOther]
end
if type(tOther) == "table" then
self:include(tClass, tOther)
tClass.__parents[tOther] = true
if type(tOther.__parents) == "table" then
for tParent in pairs(tOther.__parents) do
tClass.__parents[tParent] = true
end
end
end
end
tClass.__type = tClass.__type or "Class"
tClass.__privateMethods = tClass.__privateMethods or {}
local function classIndex(tSelf, tKey)
local tPrivateMethods = rawget(tSelf, "__privateMethods") or tClass.__privateMethods
if tKey == "__privateMethods" then
return nil
end
if tPrivateMethods and tPrivateMethods[tKey] then
return function(_, ...)
return tPrivateMethods[tKey](tSelf, ...)
end
end
return rawget(tClass, tKey)
end
tClass.__index = classIndex
local tMethodsToHide = {
init = tClass.init or tClass[1] or function() end,
include = tClass.include or function(...)
return self:include(...)
end,
clone = tClass.clone or function(...)
return self:clone(...)
end,
Is = tClass.Is or tClass.__privateMethods.Is or function(tSelf, vExpected)
return self:is(tSelf, vExpected)
end,
AssertIs = tClass.AssertIs or tClass.__privateMethods.AssertIs or function(tSelf, vExpected, sName)
return self:assertIs(tSelf, vExpected, sName)
end,
DebugInfos = tClass.__privateMethods.DebugInfos or function(...)
return self:debugInfo(...)
end,
}
for sKey, vValue in pairs(tMethodsToHide) do
if vValue then
tClass.__privateMethods[sKey] = vValue
tClass[sKey] = nil
end
end
return setmetatable(tClass, {
__call = function(tC, ...)
local tO = setmetatable({}, tC)
local tArgs = { ... }
local iArgsCount = select("#", ...)
tO.__private = {}
tO.__type = tC.__type
tO.__class = tC
tO.__privateMethods = tC.__privateMethods
assert(xpcall(
function()
tO.__privateMethods.init(tO, unpack(tArgs, 1, iArgsCount))
end,
function(tErr)
return "Init Error: " .. tErr
end
))
return tO
end,
__gc = function(tO)
if tO.destroy then
pcall(function()
tO:destroy()
end)
else
tO = nil
end
end,
__index = classIndex,
})
end,
registerClass = function(self, tName, tPrototype, tParent)
local tCls = self:new({ __includes = { tPrototype, tParent } })
tCls.__type = tName
self:overloadOperators(tCls)
return tCls
end,
}, {
__call = function(self, ...)
return self:new(...)
end,
})