First commit.
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
------------------------------------------------------------------------------
|
||||
-- Methods
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
--- Code from http://lua-users.org/wiki/ObjectOrientationTutorial
|
||||
---@param ... table Optional base classes
|
||||
---@return table cls A class definition
|
||||
local function make_class(...)
|
||||
-- "cls" is the new class
|
||||
local cls, bases = {}, {...}
|
||||
|
||||
-- copy base class contents into the new class
|
||||
for _, 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 _, 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
|
||||
|
||||
|
||||
------------------------------------------------------------------------------
|
||||
-- Module return
|
||||
------------------------------------------------------------------------------
|
||||
|
||||
return make_class
|
||||
Reference in New Issue
Block a user