-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClass.lua
108 lines (85 loc) · 3.3 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
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
function Class(name, baseClass )
local new_class = {}
new_class.__index = new_class
local new_class_mt = { __tostring = function() return name .. " Class" end, __call = function (c, ...)
local newinst = {}
if baseClass then
newinst.super = {}
local supermt = {}
newinst.super.__stack = 1
newinst.super.inst = newinst
function supermt.__index(t, k)
local superclass = new_class
for i = 1, t.__stack do
local s = superclass:getSuperClass()
local foundSuper = false
if s then
if s[k] then
superclass = s
if i == t.__stack then
t.__stack = t.__stack + 1
break
end
foundSuper = true
end
end
assert(foundSuper, "Cannot find super class in current Class")
end
return function(s, ...) local r = superclass[k](s.inst, ...) s.__stack = s.__stack -1 return r end
end
setmetatable(newinst.super, supermt)
end
setmetatable( newinst, c )
if newinst.init then newinst.init(newinst, ...) end
return newinst
end}
if baseClass then
new_class_mt.__index = baseClass
local metaevents = {"__tostring","__len","__gc","__unm","__add","__sub","__mul","__div","__mod","__pow","__concat","__eq","__lt","__le"}
for k, v in pairs(baseClass) do
if type(v) == "function" then
for _, event in pairs(metaevents) do
if k == event then
new_class[k] = v
end
end
end
end
end
setmetatable( new_class, new_class_mt)
-- Implementation of additional OO properties starts here --
-- Return the class object of the instance
function new_class:getClass()
return new_class
end
function new_class:getSuperClass()
return baseClass
end
function new_class:__tostring()
return name
end
function new_class:getName(v)
return name
end
function new_class:made( theInstance )
if type( theInstance ) == "table" then
if type(theInstance.typeOf) == "function" then
return theInstance:typeOf(self)
end
end
end
-- Return true if the caller is an instance of theClass
function new_class:typeOf( theClass )
local is_a = false
local cur_class = new_class
while ( nil ~= cur_class ) and ( false == is_a ) do
if cur_class == theClass then
is_a = true
else
cur_class = cur_class:getSuperClass()
end
end
return is_a
end
return new_class
end