diff options
Diffstat (limited to 'acf/object.lua')
-rw-r--r-- | acf/object.lua | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/acf/object.lua b/acf/object.lua new file mode 100644 index 0000000..cbccf70 --- /dev/null +++ b/acf/object.lua @@ -0,0 +1,64 @@ +--[[ +Copyright (c) 2012 Kaarle Ritvanen +See LICENSE file for license details +--]] + +module(..., package.seeall) + +function class(base) + local cls = {} + + local mt = { + __call=function(self, ...) + local obj = {} + setmetatable(obj, {__index=cls, class=cls}) + obj:init(unpack(arg)) + return obj + end + } + + if not base and Object then base = Object end + if base then + cls._base = base + mt.__index = base + end + + return setmetatable(cls, mt) +end + + +Object = class() + +function Object:init(...) end + + +function super(obj, cls) + assert(isinstance(obj, cls)) + + local mt = {} + + function mt.__index(t, k) + local v = cls._base[k] + if type(v) ~= 'function' then return v end + return function(...) + arg[1] = obj + return v(unpack(arg)) + end + end + + return setmetatable({}, mt) +end + + +function issubclass(cls1, cls2) + if cls1 == cls2 then return true end + if cls1._base then return issubclass(cls1._base, cls2) end + return false +end + +function isinstance(obj, cls) + if not obj or type(obj) ~= 'table' then return false end + local mt = getmetatable(obj) + if not mt then return false end + return type(obj) == 'table' and mt.class and issubclass(mt.class, cls) +end |