]> git.r.bdr.sh - rbdr/custom_event_support.lua/blame - lib/vendor/middleclass.lua
I forgot how to markdown
[rbdr/custom_event_support.lua] / lib / vendor / middleclass.lua
CommitLineData
b4238901
BB
1-- middleclass.lua - v2.0 (2011-09)
2-- Copyright (c) 2011 Enrique GarcĂ­a Cota
3-- 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:
4-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5-- 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.
6-- Based on YaciCode, from Julien Patte and LuaObject, from Sebastien Rocca-Serra
7
8local _classes = setmetatable({}, {__mode = "k"})
9
10local function _setClassDictionariesMetatables(klass)
11 local dict = klass.__instanceDict
12 dict.__index = dict
13
14 local super = klass.super
15 if super then
16 local superStatic = super.static
17 setmetatable(dict, super.__instanceDict)
18 setmetatable(klass.static, { __index = function(_,k) return dict[k] or superStatic[k] end })
19 else
20 setmetatable(klass.static, { __index = function(_,k) return dict[k] end })
21 end
22end
23
24local function _setClassMetatable(klass)
25 setmetatable(klass, {
26 __tostring = function() return "class " .. klass.name end,
27 __index = klass.static,
28 __newindex = klass.__instanceDict,
29 __call = function(self, ...) return self:new(...) end
30 })
31end
32
33local function _createClass(name, super)
34 local klass = { name = name, super = super, static = {}, __mixins = {}, __instanceDict={} }
35 klass.subclasses = setmetatable({}, {__mode = "k"})
36
37 _setClassDictionariesMetatables(klass)
38 _setClassMetatable(klass)
39 _classes[klass] = true
40
41 return klass
42end
43
44local function _createLookupMetamethod(klass, name)
45 return function(...)
46 local method = klass.super[name]
47 assert( type(method)=='function', tostring(klass) .. " doesn't implement metamethod '" .. name .. "'" )
48 return method(...)
49 end
50end
51
52local function _setClassMetamethods(klass)
53 for _,m in ipairs(klass.__metamethods) do
54 klass[m]= _createLookupMetamethod(klass, m)
55 end
56end
57
58local function _setDefaultInitializeMethod(klass, super)
59 klass.initialize = function(instance, ...)
60 return super.initialize(instance, ...)
61 end
62end
63
64local function _includeMixin(klass, mixin)
65 assert(type(mixin)=='table', "mixin must be a table")
66 for name,method in pairs(mixin) do
67 if name ~= "included" and name ~= "static" then klass[name] = method end
68 end
69 if mixin.static then
70 for name,method in pairs(mixin.static) do
71 klass.static[name] = method
72 end
73 end
74 if type(mixin.included)=="function" then mixin:included(klass) end
75 klass.__mixins[mixin] = true
76end
77
78Object = _createClass("Object", nil)
79
80Object.static.__metamethods = { '__add', '__call', '__concat', '__div', '__le', '__lt',
81 '__mod', '__mul', '__pow', '__sub', '__tostring', '__unm' }
82
83function Object.static:allocate()
84 assert(_classes[self], "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
85 return setmetatable({ class = self }, self.__instanceDict)
86end
87
88function Object.static:new(...)
89 local instance = self:allocate()
90 instance:initialize(...)
91 return instance
92end
93
94function Object.static:subclass(name)
95 assert(_classes[self], "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
96 assert(type(name) == "string", "You must provide a name(string) for your class")
97
98 local subclass = _createClass(name, self)
99 _setClassMetamethods(subclass)
100 _setDefaultInitializeMethod(subclass, self)
101 self.subclasses[subclass] = true
102 self:subclassed(subclass)
103
104 return subclass
105end
106
107function Object.static:subclassed(other) end
108
109function Object.static:include( ... )
110 assert(_classes[self], "Make sure you that you are using 'Class:include' instead of 'Class.include'")
111 for _,mixin in ipairs({...}) do _includeMixin(self, mixin) end
112 return self
113end
114
115function Object:initialize() end
116
117function Object:__tostring() return "instance of " .. tostring(self.class) end
118
119function class(name, super, ...)
120 super = super or Object
121 return super:subclass(name, ...)
122end
123
124function instanceOf(aClass, obj)
125 if not _classes[aClass] or type(obj) ~= 'table' or not _classes[obj.class] then return false end
126 if obj.class == aClass then return true end
127 return subclassOf(aClass, obj.class)
128end
129
130function subclassOf(other, aClass)
131 if not _classes[aClass] or not _classes[other] or aClass.super == nil then return false end
132 return aClass.super == other or subclassOf(other, aClass.super)
133end
134
135function includes(mixin, aClass)
136 if not _classes[aClass] then return false end
137 if aClass.__mixins[mixin] then return true end
138 return includes(mixin, aClass.super)
139end