2010-12-04 01:43:09 +00:00
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------
|
2011-02-01 08:04:16 +00:00
|
|
|
|
-- middleclass.lua - v1.3
|
2010-07-27 23:32:10 +00:00
|
|
|
|
-- Enrique Garc<72>a ( enrique.garcia.cota [AT] gmail [DOT] com ) - 19 Oct 2009
|
|
|
|
|
-- Based on YaciCode, from Julien Patte and LuaObject, from S<>bastien Rocca-Serra
|
2010-12-04 01:43:09 +00:00
|
|
|
|
-----------------------------------------------------------------------------------------------------------------------
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2010-12-04 01:43:09 +00:00
|
|
|
|
local _nilf = function() end -- empty function
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2011-03-30 18:07:04 +00:00
|
|
|
|
local _classes = setmetatable({}, {__mode = "k"}) -- keeps track of all the tables that are classes
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2010-12-04 01:43:09 +00:00
|
|
|
|
local _metamethods = { -- all metamethods except __index
|
|
|
|
|
'__add', '__call', '__concat', '__div', '__le', '__lt', '__mod', '__mul', '__pow', '__sub', '__tostring', '__unm'
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Object = { name = "Object", __modules = {} }
|
|
|
|
|
|
|
|
|
|
Object.__classDict = {
|
2010-12-05 00:17:57 +00:00
|
|
|
|
initialize = _nilf, destroy = _nilf, subclassed = _nilf,
|
|
|
|
|
__tostring = function(instance) return ("instance of ".. instance.class.name) end -- root of __tostring method
|
2010-12-04 01:43:09 +00:00
|
|
|
|
}
|
|
|
|
|
Object.__classDict.__index = Object.__classDict -- instances of Object need this
|
|
|
|
|
|
|
|
|
|
setmetatable(Object, {
|
|
|
|
|
__index = Object.__classDict, -- look up methods in the classDict
|
2011-02-01 08:04:16 +00:00
|
|
|
|
__newindex = Object.__classDict, -- any new Object methods will be defined in classDict
|
2010-12-04 01:43:09 +00:00
|
|
|
|
__call = Object.new, -- allows instantiation via Object()
|
|
|
|
|
__tostring = function() return "class Object" end -- allows tostring(obj)
|
|
|
|
|
})
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2011-03-30 18:07:04 +00:00
|
|
|
|
_classes[Object] = true -- register Object on the list of classes.
|
2010-12-05 00:17:57 +00:00
|
|
|
|
|
2011-03-30 08:46:12 +00:00
|
|
|
|
-- creates the instance based of the class, but doesn't initialize it
|
|
|
|
|
function Object.allocate(theClass)
|
2011-03-30 18:07:04 +00:00
|
|
|
|
assert(_classes[theClass], "Use Class:allocate instead of Class.allocate")
|
2011-03-30 10:47:13 +00:00
|
|
|
|
return setmetatable({ class = theClass }, theClass.__classDict)
|
2011-03-30 08:46:12 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- both creates and initializes an instance
|
|
|
|
|
function Object.new(theClass, ...)
|
2011-03-30 18:28:56 +00:00
|
|
|
|
local instance = theClass:allocate()
|
2010-07-27 23:32:10 +00:00
|
|
|
|
instance:initialize(...)
|
|
|
|
|
return instance
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- creates a subclass
|
2011-02-01 08:04:16 +00:00
|
|
|
|
function Object.subclass(theClass, name)
|
2011-03-30 18:07:04 +00:00
|
|
|
|
assert(_classes[theClass], "Use Class:subclass instead of Class.subclass")
|
2010-07-27 23:32:10 +00:00
|
|
|
|
assert( type(name)=="string", "You must provide a name(string) for your class")
|
|
|
|
|
|
2010-12-05 00:17:57 +00:00
|
|
|
|
local theSubClass = { name = name, superclass = theClass, __classDict = {}, __modules={} }
|
2010-12-04 01:43:09 +00:00
|
|
|
|
|
|
|
|
|
local dict = theSubClass.__classDict -- classDict contains all the [meta]methods of the class
|
|
|
|
|
dict.__index = dict -- It "points to itself" so instances can use it as a metatable.
|
|
|
|
|
local superDict = theClass.__classDict -- The superclass' classDict
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2010-12-05 00:17:57 +00:00
|
|
|
|
setmetatable(dict, superDict) -- when a method isn't found on classDict, 'escalate upwards'.
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2010-12-05 00:17:57 +00:00
|
|
|
|
for _,mmName in ipairs(_metamethods) do -- Creates the initial metamethods
|
2010-12-04 01:43:09 +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
|
|
|
|
|
assert( type(method)=='function', tostring(theSubClass) .. " doesn't implement metamethod '" .. mmName .. "'" )
|
|
|
|
|
return method(...)
|
2010-08-01 23:25:33 +00:00
|
|
|
|
end
|
2010-07-27 23:32:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2010-12-04 01:43:09 +00:00
|
|
|
|
setmetatable(theSubClass, {
|
|
|
|
|
__index = dict, -- look for stuff on the dict
|
2011-02-01 08:04:16 +00:00
|
|
|
|
__newindex = function(_, methodName, method) -- ensure that __index isn't modified by mistake
|
2010-12-07 22:46:35 +00:00
|
|
|
|
assert(methodName ~= '__index', "Can't modify __index. Include middleclass-extras.Indexable and use 'index' instead")
|
2010-12-04 01:43:09 +00:00
|
|
|
|
rawset(dict, methodName , method)
|
2010-07-27 23:32:10 +00:00
|
|
|
|
end,
|
2010-12-05 00:17:57 +00:00
|
|
|
|
__tostring = function() return ("class ".. name) end, -- allows tostring(MyClass)
|
|
|
|
|
__call = function(_, ...) return theSubClass:new(...) end -- allows MyClass(...) instead of MyClass:new(...)
|
2010-07-27 23:32:10 +00:00
|
|
|
|
})
|
|
|
|
|
|
2011-02-01 08:04:16 +00:00
|
|
|
|
theSubClass.initialize = function(instance,...) theClass.initialize(instance, ...) end -- default initialize method
|
2011-03-30 18:07:04 +00:00
|
|
|
|
_classes[theSubClass]= true -- registers the new class on the list of _classes
|
2010-12-04 01:43:09 +00:00
|
|
|
|
theClass:subclassed(theSubClass) -- hook method. By default it does nothing
|
2010-07-27 23:32:10 +00:00
|
|
|
|
|
2010-12-04 01:43:09 +00:00
|
|
|
|
return theSubClass
|
2010-07-27 23:32:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2010-12-04 01:43:09 +00:00
|
|
|
|
-- Mixin extension function - simulates very basically ruby's include. Receives a table table, probably with functions.
|
|
|
|
|
-- Its contents are copied to theClass, with one exception: the included() method will be called instead of copied
|
2011-02-01 08:04:16 +00:00
|
|
|
|
function Object.include(theClass, module, ... )
|
2011-03-30 18:07:04 +00:00
|
|
|
|
assert(_classes[theClass], "Use class:include instead of class.include")
|
2010-11-10 23:24:31 +00:00
|
|
|
|
assert(type(module=='table'), "module must be a table")
|
2010-07-27 23:32:10 +00:00
|
|
|
|
for methodName,method in pairs(module) do
|
|
|
|
|
if methodName ~="included" then theClass[methodName] = method end
|
|
|
|
|
end
|
|
|
|
|
if type(module.included)=="function" then module:included(theClass, ... ) end
|
2010-11-10 23:24:31 +00:00
|
|
|
|
theClass.__modules[module] = module
|
2010-10-23 18:16:53 +00:00
|
|
|
|
return theClass
|
2010-07-27 23:32:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Returns true if aClass is a subclass of other, false otherwise
|
|
|
|
|
function subclassOf(other, aClass)
|
2011-03-30 18:07:04 +00:00
|
|
|
|
if not _classes[aClass] or not _classes[other] then return false end
|
2010-07-27 23:32:10 +00:00
|
|
|
|
if aClass.superclass==nil then return false end -- aClass is Object, or a non-class
|
|
|
|
|
return aClass.superclass == other or subclassOf(other, aClass.superclass)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Returns true if obj is an instance of aClass (or one of its subclasses) false otherwise
|
|
|
|
|
function instanceOf(aClass, obj)
|
2011-03-30 18:07:04 +00:00
|
|
|
|
if not _classes[aClass] or type(obj)~='table' or not _classes[obj.class] then return false end
|
2010-07-27 23:32:10 +00:00
|
|
|
|
if obj.class==aClass then return true end
|
|
|
|
|
return subclassOf(aClass, obj.class)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- Returns true if the a module has already been included on a class (or a superclass of that class)
|
2010-08-02 14:51:30 +00:00
|
|
|
|
function includes(module, aClass)
|
2011-03-30 18:07:04 +00:00
|
|
|
|
if not _classes[aClass] then return false end
|
2010-11-10 23:24:31 +00:00
|
|
|
|
if aClass.__modules[module]==module then return true end
|
2010-08-02 14:51:30 +00:00
|
|
|
|
return includes(module, aClass.superclass)
|
2010-07-27 23:32:10 +00:00
|
|
|
|
end
|
|
|
|
|
|
2010-08-10 22:59:48 +00:00
|
|
|
|
-- Creates a new class named 'name'. Uses Object if no baseClass is specified. Additional parameters for compatibility
|
|
|
|
|
function class(name, baseClass, ...)
|
2010-07-27 23:32:10 +00:00
|
|
|
|
baseClass = baseClass or Object
|
2010-08-10 22:59:48 +00:00
|
|
|
|
return baseClass:subclass(name, ...)
|
2010-07-27 23:32:10 +00:00
|
|
|
|
end
|