-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.lua
More file actions
176 lines (134 loc) · 2.98 KB
/
class.lua
File metadata and controls
176 lines (134 loc) · 2.98 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
-- Source from http://lua-users.org/wiki/SimpleLuaClasses
function class(init, base)
local c = {} -- the class table
if type(base) == 'table' then
for k, v in pairs(base) do
c[k] = v
end
c._base = base
end
c.__index = c
c._abstract = false
-- constructor (class metatable)
local mt = {}
mt.__call = function (class_tbl, ...)
local obj = {} -- the new class instance
setmetatable(obj, c)
if init then
init(obj, ...)
elseif class_tbl.__init then
class_tbl.__init(obj, ...)
end
return obj
end
if init then c.__init = init
elseif base and base.__init then c.__init = base.__init end
c.__implementAbstract = function(clss, methodName, func)
clss[methodName] = func
end
c.isinstance = function(self, clss)
local m = getmetatable(self)
while m do
if m == clss then return true end
m = m._base
end
return false
end
setmetatable(c, mt)
return c
end
---- End
-- Abstract class
function abst_class(init, base)
local c = {} -- the class table
if type(base) == 'table' then
for k, v in pairs(base) do
c[k] = v
end
c._base = base
end
c.__index = c
c._abstract = true
-- constructor (class metatable)
local mt = {}
mt.__call = function (class_tbl, ...)
local obj = {} -- the new class instance
setmetatable(obj, c)
class_tbl.__init(obj, ...)
return obj
end
c.__init = function(obj, ...)
if getmetatable(obj)._abstract then
error("Cannot instanciate an abstract class")
end
if init then
init(obj, ...)
elseif c.__oinit then
c.__oinit(obj, ...)
end
end
if init then c.__oinit = init
elseif base and base.__init then c.__oinit = base.__init end
c.__implementAbstract = function(clss, methodName, func)
clss[methodName] = func
end
c.__addAbstract = function(clss, methodName, msg)
clss[methodName] = abst_method(msg)
end
c.isinstance = function(self, clss)
local m = getmetatable(self)
while m do
if m == clss then return true end
m = m._base
end
return false
end
setmetatable(c, mt)
return c
end
-- Abstract method
function abst_method(msg)
return function(...)
if msg then
error("Unimplemented abstract function (" .. msg .. ")")
else
error("Unimplemented abstract function")
end
end
end
function enum(init, base, instances)
local c = {} -- the class table
if type(base) == 'table' then
for k, v in pairs(base)
do
c[k] = v
end
c._base = base
end
c.__index = c
-- constructor (class metatable)
local mt = {}
instanciate = function (class_tbl, name, ...)
local obj = {} -- the new class instance
setmetatable(obj, c)
if init then
init(obj, name, ...)
elseif base and base.__init then
base.__init(obj, name, ...)
end
return obj
end
c.isinstance = function(self, clss)
local m = getmetatable(self)
while m do
if m == clss then return true end
m = m._base
end
return false
end
setmetatable(c, mt)
for k, v in pairs(instances) do
c[k] = instanciate(c, k, v)
end
return c
end