Instanceof support for hump class
This commit is contained in:
parent
72dd42119d
commit
b914b5217c
1 changed files with 70 additions and 42 deletions
|
@ -54,6 +54,8 @@ local function clone(other)
|
|||
return setmetatable(include({}, other), getmetatable(other))
|
||||
end
|
||||
|
||||
local class_id = 1
|
||||
|
||||
local function new(class)
|
||||
-- mixins
|
||||
local inc = class.__includes or {}
|
||||
|
@ -69,6 +71,32 @@ local function new(class)
|
|||
class.include = class.include or include
|
||||
class.clone = class.clone or clone
|
||||
|
||||
if class.class_tree == nil then
|
||||
class.class_tree = {}
|
||||
else
|
||||
table.insert(class.class_tree, class_id)
|
||||
end
|
||||
class.class_id = class_id
|
||||
|
||||
class_id = class_id + 1
|
||||
|
||||
-- Check if object is instance of given class
|
||||
-- Note this will not work for child classes
|
||||
class.isinstance = function(self, class)
|
||||
if self.class_id == class.class_id then
|
||||
return true
|
||||
end
|
||||
|
||||
for _, val in pairs(self.class_tree) do
|
||||
if val == class.class_id then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
class_id = class_id + 1
|
||||
|
||||
-- constructor call
|
||||
return setmetatable(class, {__call = function(c, ...)
|
||||
local o = setmetatable({}, c)
|
||||
|
|
Loading…
Reference in a new issue