2010-09-19 13:37:55 +00:00
|
|
|
--[[
|
2013-02-20 15:54:57 +00:00
|
|
|
Copyright (c) 2010-2013 Matthias Richter
|
2010-09-19 13:37:55 +00:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
Except as contained in this notice, the name(s) of the above copyright holders
|
|
|
|
shall not be used in advertising or otherwise to promote the sale, use or
|
|
|
|
other dealings in this Software without prior written authorization.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
]]--
|
|
|
|
|
2013-02-20 14:53:13 +00:00
|
|
|
local function include_helper(to, from, seen)
|
2013-02-25 17:38:11 +00:00
|
|
|
if from == nil then
|
|
|
|
return to
|
|
|
|
elseif type(from) ~= 'table' then
|
2013-02-20 14:53:13 +00:00
|
|
|
return from
|
|
|
|
elseif seen[from] then
|
|
|
|
return seen[from]
|
|
|
|
end
|
2011-04-11 14:44:31 +00:00
|
|
|
|
2013-02-20 14:53:13 +00:00
|
|
|
seen[from] = to
|
|
|
|
for k,v in pairs(from) do
|
|
|
|
k = include_helper({}, k, seen) -- keys might also be tables
|
2015-05-05 16:26:52 +00:00
|
|
|
if to[k] == nil then
|
2013-02-20 14:53:13 +00:00
|
|
|
to[k] = include_helper({}, v, seen)
|
2011-04-11 14:44:31 +00:00
|
|
|
end
|
|
|
|
end
|
2013-02-20 14:53:13 +00:00
|
|
|
return to
|
|
|
|
end
|
2011-04-11 14:44:31 +00:00
|
|
|
|
2013-02-20 14:53:13 +00:00
|
|
|
-- deeply copies `other' into `class'. keys in `other' that are already
|
|
|
|
-- defined in `class' are omitted
|
|
|
|
local function include(class, other)
|
|
|
|
return include_helper(class, other, {})
|
2011-04-11 14:44:31 +00:00
|
|
|
end
|
|
|
|
|
2013-02-20 14:53:13 +00:00
|
|
|
-- returns a deep copy of `other'
|
|
|
|
local function clone(other)
|
2014-08-22 11:51:25 +00:00
|
|
|
return setmetatable(include({}, other), getmetatable(other))
|
2013-02-20 14:53:13 +00:00
|
|
|
end
|
2011-04-11 14:44:31 +00:00
|
|
|
|
2013-02-20 14:53:13 +00:00
|
|
|
local function new(class)
|
|
|
|
-- mixins
|
|
|
|
local inc = class.__includes or {}
|
|
|
|
if getmetatable(inc) then inc = {inc} end
|
|
|
|
|
2013-02-25 17:38:11 +00:00
|
|
|
for _, other in ipairs(inc) do
|
2015-05-07 01:05:19 +00:00
|
|
|
if type(other) == "string" then
|
2015-05-08 22:41:38 +00:00
|
|
|
other = _G[other]
|
2015-05-07 01:05:19 +00:00
|
|
|
end
|
2013-02-20 14:53:13 +00:00
|
|
|
include(class, other)
|
|
|
|
end
|
2010-08-09 15:52:48 +00:00
|
|
|
|
2013-02-25 17:38:11 +00:00
|
|
|
-- class implementation
|
|
|
|
class.__index = class
|
|
|
|
class.init = class.init or class[1] or function() end
|
|
|
|
class.include = class.include or include
|
|
|
|
class.clone = class.clone or clone
|
|
|
|
|
2013-02-20 14:53:13 +00:00
|
|
|
-- constructor call
|
|
|
|
return setmetatable(class, {__call = function(c, ...)
|
|
|
|
local o = setmetatable({}, c)
|
|
|
|
o:init(...)
|
|
|
|
return o
|
|
|
|
end})
|
2011-01-18 16:44:38 +00:00
|
|
|
end
|
|
|
|
|
2011-08-29 15:41:05 +00:00
|
|
|
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
|
2011-11-16 12:34:59 +00:00
|
|
|
if class_commons ~= false and not common then
|
|
|
|
common = {}
|
2011-08-29 15:41:05 +00:00
|
|
|
function common.class(name, prototype, parent)
|
2013-02-25 17:38:11 +00:00
|
|
|
return new{__includes = {prototype, parent}}
|
2011-08-29 15:41:05 +00:00
|
|
|
end
|
2011-11-16 12:34:59 +00:00
|
|
|
function common.instance(class, ...)
|
|
|
|
return class(...)
|
|
|
|
end
|
2011-08-29 15:41:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-07-06 12:19:31 +00:00
|
|
|
-- the module
|
2013-02-20 14:53:13 +00:00
|
|
|
return setmetatable({new = new, include = include, clone = clone},
|
2011-07-06 12:19:31 +00:00
|
|
|
{__call = function(_,...) return new(...) end})
|