-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.lua
More file actions
54 lines (40 loc) · 1.12 KB
/
class.lua
File metadata and controls
54 lines (40 loc) · 1.12 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
-- TODO: research about this and make better version
local function class(base)
local cls = {}
if type(base) == 'table' then
for k, v in pairs(base) do
cls[k] = v
end
-- setmetatable(cls, {__index = base})
cls.base = base -- TODO: make use of it or remove
end
cls.__index = cls
function cls.isInstance(obj)
if type(obj) ~= 'table' then return false end
local mt = getmetatable(obj)
while mt do
if mt == cls then return true end
-- if mt.base then
-- mt = mt.base
-- else
-- mt = nil
-- end
end
return false
end
setmetatable(cls, {
__call = function(self_, ...)
local obj = setmetatable({}, cls)
if self_.__init then
self_.__init(obj, ...)
elseif base ~= nil and base.__init ~= nil then
base.__init(obj, ...)
end
return obj
end
})
return cls
end
-- ----------------------------------------------------------------------------
LibImplex = LibImplex or {}
LibImplex.class = class