local _class={}
local all_cloned_obs = {}
setmetatable(all_cloned_obs, { __mode = "kv" })
function get_all_cloned_obs()
return all_cloned_obs
end
function get_all_destructed_obs()
local list = {}
for _, ob in pairs(all_cloned_obs) do
if is_table(ob) and ob.destructed == true then
table.insert(list, ob)
end
end
return list
end
function get_class_func(c, func_name)
if not _class[c] then
return;
end
return _class[c][func_name]
end
function nil_func()
return nil
end
local function search(k, plist)
for i = 1, #plist do
local v = _class[plist[i]][k]
if v ~= nil_func then
return v
end
end
return
end
function tdcls(...)
local class_type={}
class_type.create=false
class_type.destruct=false
class_type.name = ""
class_type.super={...}
class_type.ob_list = {}
setmetatable(class_type.ob_list, { __mode = "v" })
class_type.new=function(...)
local obj={ is_clone = true, destructed = false }
setmetatable(obj,{ __index= _class[class_type] })
do
local _create
_create = function(c,...)
if table.getn(c.super) > 0 then
for i, v in ipairs(c.super) do
_create(v,...)
end
end
if c.create then
c.create(obj,...)
end
end
_create(class_type,...)
end
class_type.ob_list[#class_type.ob_list + 1] = obj
all_cloned_obs[#all_cloned_obs + 1] = obj
return obj
end
class_type.get_func_list=function(c)
local func_list = {}
local _find
_find = function(c, func_list)
if table.getn(c.super) > 0 then
for i, v in pairs(c.super) do
_find(v, func_list)
end
end
if _class[c] then
for k, v in pairs(_class[c]) do
if v ~= nil_func then
func_list[k] = v
end
end
end
func_list["is_clone"] = nil
end
_find(c, func_list)
return func_list
end
local vtbl = { }
_class[class_type]=vtbl
setmetatable(class_type,{__newindex=
function(t,k,v)
vtbl[k]=v
end
})
vtbl.destruct_object=function(obj)
do
local _destruct
_destruct = function(c)
if c.destruct then
local status, e = pcall(c.destruct, obj)
if not status then
error_handle(tostring(e))
end
end
if table.getn(c.super) > 0 then
for i = #c.super, 1, -1 do
_destruct(c.super[i])
end
end
end
_destruct(class_type)
end
end
vtbl.base=function(obj, c, f, ...)
local k = string.format("%s%s", c.name, f)
local ret = vtbl[k]
if ret ~= nil_func then
local a, b, c, d, e = ret(obj, ...)
return a, b, c, d, e
end
if table.getn(c.super) > 0 then
for i = #c.super, 1, -1 do
ret = search(f, c.super)
if ret then
vtbl[k] = ret
local a, b, c, d, e = ret(obj, ...)
return a, b, c, d, e
end
end
end
end
if table.getn(class_type.super) > 0 then
setmetatable(vtbl,{__index=
function(t,k)
local ret
if k == "class_type" then
ret = class_type.name
else
ret = search(k, class_type.super)
end
if not ret then
ret = nil_func
end
vtbl[k]=ret
return ret
end
})
else
setmetatable(vtbl,{__index=
function(t,k)
local ret = nil_func
if k == "class_type" then
ret = class_type.name
end
vtbl[k]=ret
return ret
end
})
end
return class_type
end