From 7d9c43916b0600ac4879dfe9793eab807a83ab2b Mon Sep 17 00:00:00 2001 From: Kaarle Ritvanen Date: Mon, 10 Mar 2014 22:45:18 +0200 Subject: rename ACF2 to Alpine Configurator (aconf) --- aconf/object.lua | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 aconf/object.lua (limited to 'aconf/object.lua') diff --git a/aconf/object.lua b/aconf/object.lua new file mode 100644 index 0000000..64fbb9c --- /dev/null +++ b/aconf/object.lua @@ -0,0 +1,68 @@ +--[[ +Copyright (c) 2012-2013 Kaarle Ritvanen +See LICENSE file for license details +--]] + +local M = {} + +function M.class(base) + local cls = {} + + local mt = { + __call=function(self, ...) + local obj = {} + setmetatable(obj, {__index=cls, class=cls}) + obj:init(...) + return obj + end + } + + if not base and M.Object then base = M.Object end + if base then + cls._base = base + mt.__index = base + end + + return setmetatable(cls, mt) +end + + +M.Object = M.class() + +function M.Object:init(...) end + + +function M.super(obj, cls) + assert(M.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(...) + local arg = {...} + arg[1] = obj + return v(unpack(arg)) + end + end + + return setmetatable({}, mt) +end + + +function M.issubclass(cls1, cls2) + if cls1 == cls2 then return true end + if cls1._base then return M.issubclass(cls1._base, cls2) end + return false +end + +function M.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 M.issubclass(mt.class, cls) +end + + +return M -- cgit v1.2.3