2022-06-08 02:09:48 +02:00
|
|
|
---@class core.object
|
|
|
|
---@field super core.object
|
2019-12-28 12:16:32 +01:00
|
|
|
local Object = {}
|
|
|
|
Object.__index = Object
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---Can be overrided by child objects to implement a constructor.
|
|
|
|
function Object:new() end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---@return core.object
|
2019-12-28 12:16:32 +01:00
|
|
|
function Object:extend()
|
|
|
|
local cls = {}
|
|
|
|
for k, v in pairs(self) do
|
|
|
|
if k:find("__") == 1 then
|
|
|
|
cls[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
cls.__index = cls
|
|
|
|
cls.super = self
|
|
|
|
setmetatable(cls, self)
|
|
|
|
return cls
|
|
|
|
end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---Check if the object is strictly of the given type.
|
|
|
|
---@param T any
|
|
|
|
---@return boolean
|
2019-12-28 12:16:32 +01:00
|
|
|
function Object:is(T)
|
2022-05-15 22:10:57 +02:00
|
|
|
return getmetatable(self) == T
|
2022-05-13 02:33:01 +02:00
|
|
|
end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---Check if the object inherits from the given type.
|
|
|
|
---@param T any
|
|
|
|
---@return boolean
|
2022-05-13 02:33:01 +02:00
|
|
|
function Object:extends(T)
|
2019-12-28 12:16:32 +01:00
|
|
|
local mt = getmetatable(self)
|
|
|
|
while mt do
|
|
|
|
if mt == T then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
mt = getmetatable(mt)
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---Metamethod to get a string representation of an object.
|
|
|
|
---@return string
|
2019-12-28 12:16:32 +01:00
|
|
|
function Object:__tostring()
|
|
|
|
return "Object"
|
|
|
|
end
|
|
|
|
|
2022-06-08 02:09:48 +02:00
|
|
|
---Methamethod to allow using the object call as a constructor.
|
|
|
|
---@return core.object
|
2019-12-28 12:16:32 +01:00
|
|
|
function Object:__call(...)
|
|
|
|
local obj = setmetatable({}, self)
|
|
|
|
obj:new(...)
|
|
|
|
return obj
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return Object
|