Optimized luci.util.class
This commit is contained in:
parent
ba6928dce0
commit
9e2759ec34
1 changed files with 15 additions and 20 deletions
|
@ -59,6 +59,17 @@ end
|
||||||
-- Class helper routines
|
-- Class helper routines
|
||||||
--
|
--
|
||||||
|
|
||||||
|
-- Instantiates a class
|
||||||
|
local function _instantiate(class, ...)
|
||||||
|
local inst = setmetatable({}, {__index = class})
|
||||||
|
|
||||||
|
if inst.__init__ then
|
||||||
|
inst:__init__(...)
|
||||||
|
end
|
||||||
|
|
||||||
|
return inst
|
||||||
|
end
|
||||||
|
|
||||||
--- Create a Class object (Python-style object model).
|
--- Create a Class object (Python-style object model).
|
||||||
-- The class object can be instantiated by calling itself.
|
-- The class object can be instantiated by calling itself.
|
||||||
-- Any class functions or shared parameters can be attached to this object.
|
-- Any class functions or shared parameters can be attached to this object.
|
||||||
|
@ -74,26 +85,10 @@ end
|
||||||
-- @see instanceof
|
-- @see instanceof
|
||||||
-- @see clone
|
-- @see clone
|
||||||
function class(base)
|
function class(base)
|
||||||
local class = {}
|
return setmetatable({}, {
|
||||||
|
__call = _instantiate,
|
||||||
local create = function(class, ...)
|
__index = base
|
||||||
local inst = setmetatable({}, {__index = class})
|
})
|
||||||
|
|
||||||
if inst.__init__ then
|
|
||||||
inst:__init__(...)
|
|
||||||
end
|
|
||||||
|
|
||||||
return inst
|
|
||||||
end
|
|
||||||
|
|
||||||
local classmeta = {__call = create}
|
|
||||||
|
|
||||||
if base then
|
|
||||||
classmeta.__index = base
|
|
||||||
end
|
|
||||||
|
|
||||||
setmetatable(class, classmeta)
|
|
||||||
return class
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Test whether the given object is an instance of the given class.
|
--- Test whether the given object is an instance of the given class.
|
||||||
|
|
Loading…
Reference in a new issue