elua: delegative multiple inheritance support in util object system

This commit is contained in:
Daniel Kolesa 2015-02-25 13:03:00 +00:00
parent f0bf599435
commit c18b1704a9
1 changed files with 24 additions and 0 deletions

View File

@ -13,6 +13,18 @@ local M = {}
local getmetatable, setmetatable = getmetatable, setmetatable
-- multiple inheritance index with depth-first search
local multi_index = function(self, name)
local protos = self.__protos
for i = 1, #protos do
local proto = protos[i]
local v = proto[name]
if v ~= nil then
return v
end
end
end
M.Object = {
__call = function(self, ...)
local r = self:clone()
@ -41,6 +53,18 @@ M.Object = {
return is
end,
add_parent = function(self, parent)
local protos = self.__protos
if protos then
-- we have multiple inheritance set up
protos[#protos + 1] = parent
else
self.__protos = { self.__proto, parent }
self.__proto = nil
self.__index = multi_index
end
end,
mixin = function(self, obj)
for k, v in pairs(obj) do self[k] = v end
end,