Last active
September 6, 2017 11:47
-
-
Save troysandal/6678407 to your computer and use it in GitHub Desktop.
A somewhat Gideros Core.class() compatible class system. You can't mix the two system, e.g. local A = JCTCore.class() and B = JCTCore.class(A) causes strange behavior as does local MyCalss = JCTCore.class(EventDispatcher) . If you use JCTCore.class() for model objects and Core.class() for UI classes you should be ok. Write your own XYZEventDispa…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
-- OO Classes (API compat with Gideros so we can use busted unit testing) | |
-- You can't mix and match, e.g. MyClass = TCore.class(EventDisatpch) | |
-- doesn't work. That's about the only class you'll need to implement | |
-- on your own for model objects. | |
-- | |
-- @usage | |
-- local MyClass = TCore.class() | |
-- | |
local TCore = {} | |
local function callCtors(instance, ...) | |
local mt = getmetatable(instance) | |
local stack = {} | |
while mt ~= nil do | |
if mt.init then | |
table.insert(stack, mt.init) | |
end | |
mt = getmetatable(mt) | |
end | |
for i=#stack,1,-1 do | |
local init = stack[i] | |
init(instance, unpack(arg)) | |
end | |
end | |
-- An alternative to Core.class() | |
function TCore.class(super) | |
local kclass = {} | |
if super then | |
kclass.super = super | |
setmetatable(kclass, super) | |
end | |
kclass.__index = kclass | |
kclass.new = function(...) | |
local instance = {} | |
setmetatable(instance, kclass) | |
callCtors(instance, unpack(arg)) | |
return instance | |
end | |
return kclass | |
end | |
return TCore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment