From: Daniel Kolesa Date: Wed, 25 Feb 2015 13:03:00 +0000 (+0000) Subject: elua: delegative multiple inheritance support in util object system X-Git-Tag: v1.14.0-alpha1~394 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=c18b1704a94f6a6d6db552a0e2c5f23b720fca82;p=platform%2Fupstream%2Fefl.git elua: delegative multiple inheritance support in util object system --- diff --git a/src/scripts/elua/core/util.lua b/src/scripts/elua/core/util.lua index b273054..7b7120f 100644 --- a/src/scripts/elua/core/util.lua +++ b/src/scripts/elua/core/util.lua @@ -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,