2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
barebones oop basics
|
|
|
|
supports basic inheritance and means you don't have to build/set your own metatable each time
|
|
|
|
|
|
|
|
todo: collect some stats on classes/optional global class registry
|
|
|
|
]]
|
|
|
|
|
2020-03-15 10:22:22 +00:00
|
|
|
local function class(inherits)
|
2020-01-29 03:26:28 +00:00
|
|
|
local c = {}
|
2020-05-11 23:26:11 +00:00
|
|
|
c.__mt = {
|
|
|
|
__index = c,
|
|
|
|
}
|
|
|
|
setmetatable(c, {
|
|
|
|
--wire up call as ctor
|
|
|
|
__call = function(self, ...)
|
|
|
|
return self:new(...)
|
|
|
|
end,
|
|
|
|
--handle single inheritence
|
|
|
|
__index = inherits,
|
|
|
|
})
|
2020-01-29 03:26:28 +00:00
|
|
|
--common class functions
|
|
|
|
|
|
|
|
--internal initialisation
|
|
|
|
--sets up an initialised object with a default value table
|
|
|
|
--performing a super construction if necessary and assigning the right metatable
|
|
|
|
function c:init(t, ...)
|
2020-05-14 05:27:24 +00:00
|
|
|
if inherits and inherits.new then
|
2020-03-24 03:57:42 +00:00
|
|
|
--construct superclass instance, then overlay args table
|
|
|
|
local ct = inherits:new(...)
|
|
|
|
for k,v in pairs(t) do
|
|
|
|
ct[k] = v
|
|
|
|
end
|
|
|
|
t = ct
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
--upgrade to this class and return
|
|
|
|
return setmetatable(t, self.__mt)
|
|
|
|
end
|
|
|
|
|
|
|
|
--constructor
|
|
|
|
--generally to be overridden
|
|
|
|
function c:new()
|
|
|
|
return self:init({})
|
|
|
|
end
|
|
|
|
|
|
|
|
--get the inherited class for super calls if/as needed
|
|
|
|
--allows overrides that still refer to superclass behaviour
|
|
|
|
function c:super()
|
|
|
|
return inherits
|
|
|
|
end
|
|
|
|
|
2020-05-11 23:26:11 +00:00
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--done
|
|
|
|
return c
|
|
|
|
end
|
2020-03-15 10:22:22 +00:00
|
|
|
|
|
|
|
return class
|