2011-08-07 22:07:02 +00:00
|
|
|
-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
-- middleclass.lua - v2.0 (2011-08)
|
|
|
|
-- Enrique Garcia Cota - enrique.garcia.cota [AT] gmail [DOT] com
|
|
|
|
-- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local _classes = setmetatable({}, {__mode = "k"})
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
local function _initializeClass(klass, super)
|
|
|
|
|
|
|
|
local dict = klass.__instanceDict
|
|
|
|
|
|
|
|
if super then
|
|
|
|
setmetatable(dict, { __index = super.__instanceDict })
|
|
|
|
setmetatable(klass.static, { __index = function(_,k) return dict[k] or super[k] end })
|
|
|
|
else
|
|
|
|
setmetatable(klass.static, { __index = function(_,k) return dict[k] end })
|
|
|
|
end
|
|
|
|
|
|
|
|
setmetatable(klass, {
|
|
|
|
__tostring = function() return "class " .. klass.name end,
|
|
|
|
__index = klass.static,
|
2011-08-10 19:45:15 +00:00
|
|
|
__newindex = klass.__instanceDict,
|
|
|
|
__call = function(_, ...) return klass:new(...) end
|
2011-08-08 21:18:15 +00:00
|
|
|
})
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
_classes[klass] = true
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
Object = {
|
2011-08-08 21:18:15 +00:00
|
|
|
name = "Object",
|
|
|
|
static = {},
|
2011-08-08 20:29:18 +00:00
|
|
|
__mixins = {},
|
|
|
|
__instanceDict = {},
|
2011-08-07 22:07:02 +00:00
|
|
|
__metamethods = { '__add', '__call', '__concat', '__div', '__le', '__lt',
|
|
|
|
'__mod', '__mul', '__pow', '__sub', '__tostring', '__unm' }
|
|
|
|
}
|
|
|
|
|
2011-08-08 21:18:15 +00:00
|
|
|
_initializeClass(Object)
|
|
|
|
|
|
|
|
Object.initialize = function() end
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-08 20:29:18 +00:00
|
|
|
function Object.static:allocate()
|
2011-08-08 05:38:11 +00:00
|
|
|
assert(_classes[self], "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
|
2011-08-08 21:18:15 +00:00
|
|
|
return setmetatable({ class = self }, {__index = self.__instanceDict })
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
|
|
|
|
2011-08-08 21:18:15 +00:00
|
|
|
function Object.static:new(...)
|
|
|
|
local instance = self:allocate()
|
2011-08-07 22:07:02 +00:00
|
|
|
instance:initialize(...)
|
|
|
|
return instance
|
|
|
|
end
|
|
|
|
|
2011-08-08 20:29:18 +00:00
|
|
|
function Object.static:subclass(name)
|
2011-08-08 05:38:11 +00:00
|
|
|
assert(_classes[self], "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
|
2011-08-08 21:18:15 +00:00
|
|
|
assert(type(name) == "string", "You must provide a name(string) for your class")
|
2011-08-08 20:29:18 +00:00
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
local subclass = { name = name, superclass = self, static = {}, __mixins = {}, __instanceDict={} }
|
2011-08-08 20:29:18 +00:00
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
_initializeClass(subclass, self)
|
2011-08-08 20:29:18 +00:00
|
|
|
|
|
|
|
return subclass
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
-- creates a subclass
|
2011-08-08 21:59:23 +00:00
|
|
|
function Object.subclass(klass, name)
|
|
|
|
assert(_classes[klass], "Use Class:subclass instead of Class.subclass")
|
2011-08-07 22:07:02 +00:00
|
|
|
assert( type(name)=="string", "You must provide a name(string) for your class")
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
local thesubclass = { name = name, super = klass, __classDict = {}, __mixins={} }
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
local dict = thesubclass.__classDict -- classDict contains all the [meta]methods of the class
|
2011-08-07 22:07:02 +00:00
|
|
|
dict.__index = dict -- It "points to itself" so instances can use it as a metatable.
|
2011-08-08 21:59:23 +00:00
|
|
|
local superDict = klass.__classDict -- The super' classDict
|
2011-08-07 22:07:02 +00:00
|
|
|
|
|
|
|
setmetatable(dict, superDict) -- when a method isn't found on classDict, 'escalate upwards'.
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
setmetatable(thesubclass, {
|
2011-08-07 22:07:02 +00:00
|
|
|
__index = dict, -- look for stuff on the dict
|
|
|
|
__newindex = function(_, methodName, method) -- ensure that __index isn't modified by mistake
|
|
|
|
assert(methodName ~= '__index', "Can't modify __index. Include middleclass-extras.Indexable and use 'index' instead")
|
|
|
|
rawset(dict, methodName , method)
|
|
|
|
end,
|
|
|
|
__tostring = function() return ("class ".. name) end, -- allows tostring(MyClass)
|
2011-08-08 21:59:23 +00:00
|
|
|
__call = function(_, ...) return thesubclass:new(...) end -- allows MyClass(...) instead of MyClass:new(...)
|
2011-08-07 22:07:02 +00:00
|
|
|
})
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
for _,mmName in ipairs(klass.__metamethods) do -- Creates the initial metamethods
|
2011-08-07 22:07:02 +00:00
|
|
|
dict[mmName]= function(...) -- by default, they just 'look up' for an implememtation
|
|
|
|
local method = superDict[mmName] -- and if none found, they throw an error
|
2011-08-08 21:59:23 +00:00
|
|
|
assert( type(method)=='function', tostring(thesubclass) .. " doesn't implement metamethod '" .. mmName .. "'" )
|
2011-08-07 22:07:02 +00:00
|
|
|
return method(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
thesubclass.initialize = function(instance,...) klass.initialize(instance, ...) end
|
|
|
|
_classes[thesubclass]= true -- registers the new class on the list of _classes
|
|
|
|
klass:subclassed(thesubclass) -- hook method. By default it does nothing
|
2011-08-07 22:07:02 +00:00
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
return thesubclass
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Mixin extension function - simulates very basically ruby's include. Receives a table table, probably with functions.
|
2011-08-08 21:59:23 +00:00
|
|
|
-- Its contents are copied to klass, with one exception: the included() method will be called instead of copied
|
|
|
|
function Object.include(klass, mixin, ... )
|
|
|
|
assert(_classes[klass], "Use class:include instead of class.include")
|
2011-08-07 22:07:02 +00:00
|
|
|
assert(type(mixin)=='table', "mixin must be a table")
|
|
|
|
for methodName,method in pairs(mixin) do
|
2011-08-08 21:59:23 +00:00
|
|
|
if methodName ~="included" then klass[methodName] = method end
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
2011-08-08 21:59:23 +00:00
|
|
|
if type(mixin.included)=="function" then mixin:included(klass, ... ) end
|
|
|
|
klass.__mixins[mixin] = mixin
|
|
|
|
return klass
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
-- Returns true if the mixin has already been included on a class (or a super)
|
2011-08-07 22:07:02 +00:00
|
|
|
function includes(mixin, aClass)
|
|
|
|
if not _classes[aClass] then return false end
|
|
|
|
if aClass.__mixins[mixin]==mixin then return true end
|
2011-08-08 21:59:23 +00:00
|
|
|
return includes(mixin, aClass.super)
|
2011-08-07 22:07:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
2011-08-08 21:59:23 +00:00
|
|
|
function class(name, super, ...)
|
|
|
|
super = super or Object
|
|
|
|
return super:subclass(name, ...)
|
2011-08-08 21:18:15 +00:00
|
|
|
end
|
|
|
|
|
2011-08-10 19:45:15 +00:00
|
|
|
function instanceOf(aClass, obj)
|
|
|
|
if not _classes[aClass] or type(obj) ~= 'table' or not _classes[obj.class] then return false end
|
|
|
|
if obj.class == aClass then return true end
|
|
|
|
return subclassOf(aClass, obj.class)
|
|
|
|
end
|
|
|
|
|
|
|
|
function subclassOf(other, aClass)
|
|
|
|
if not _classes[aClass] or not _classes[other] or aClass.superclass == nil then return false end
|
|
|
|
return aClass.superclass == other or subclassOf(other, aClass.superclass)
|
|
|
|
end
|
|
|
|
|
2011-08-08 05:38:11 +00:00
|
|
|
|