| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- --[[
- Class system
- @author : Siapran Candoris
- Supports memoized multiple inheritance
- ]]--
- local ipairs = ipairs
- do
- local function metatable_search( k, list )
- for _,e in ipairs(list) do
- local v = e[k]
- if v then return v end
- end
- end
- local function metatable_cache( self, k )
- local v = metatable_search(k, self.__parents)
- self[k] = v
- return v
- end
- -- genealogy is
- local function make_genealogy( self, res, has )
- res = res or {}
- has = has or {}
- local parents = self.__parents
- if has[self] then
- return
- end
- if parents and #parents > 0 then
- for _,parent in ipairs(parents) do
- make_genealogy(parent, res, has)
- end
- end
- res[#res + 1] = self
- has[self] = true
- return res
- end
- -- make a class with simple or multiple inheritance
- -- inheritance is implemented as cached first found
- -- do NOT change class methods at runtime, just don't
- function make_class( ... )
- local res = {}
- res.__parents = {...}
- res.__genealogy = make_genealogy(res)
- res.__instanceof_cache = {}
- -- inherited methods are cached to improve runtime performance
- -- caching is done per class, not per object
- if #res.__parents > 0 then
- setmetatable(res, {__index = metatable_cache})
- end
- res.__index = res
- return res
- end
- end
- return make_class
|