-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.lua
37 lines (37 loc) · 1 KB
/
class.lua
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
-- This convenience function is copied from http://lua-users.org/wiki/ObjectOrientationTutorial.
function class(...)
-- "cls" is the new class
local cls, bases = {}, {...}
-- copy base class contents into the new class
for i, base in ipairs(bases) do
for k, v in pairs(base) do
cls[k] = v
end
end
-- set the class's __index, and start filling an "is_a" table that contains this class and all of its bases
-- so you can do an "instance of" check using my_instance.is_a[MyClass]
cls.__index, cls.is_a = cls, {[cls] = true}
for i, base in ipairs(bases) do
for c in pairs(base.is_a) do
cls.is_a[c] = true
end
cls.is_a[base] = true
end
-- the class's __call metamethod
setmetatable(
cls,
{
__call = function(c, ...)
local instance = setmetatable({}, c)
-- run the init method if it's there
local init = instance._init
if init then
init(instance, ...)
end
return instance
end
}
)
-- return the new class table, that's ready to fill with methods
return cls
end