-
Notifications
You must be signed in to change notification settings - Fork 20
/
classes.lua
186 lines (153 loc) · 7.83 KB
/
classes.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
--[[
Copyright © 2024, Tylas
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of XivParty nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <your name> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
]]
local classes = {}
-- Baseclass of all objects.
classes.Object = {}
classes.Object.class = classes.Object
--- Nullary constructor.
function classes.Object:init(...)
end
--- Base alloc method.
function classes.Object.alloc(mastertable)
return setmetatable({}, {__index = classes.Object, __newindex = mastertable})
end
--- Base new method.
function classes.Object.new(...)
return classes.Object.alloc({}):init(...)
end
--- Checks if this object is an instance of class.
-- @param class The class object to check.
-- @return Returns true if this object is an instance of class, false otherwise.
function classes.Object:instanceOf(class)
-- Recurse up the supertypes until class is found, or until the supertype is not part of the inheritance tree.
if self.class == class then
return true
end
if self.super then
return self.super.__base:instanceOf(class)
end
return false
end
--- Creates a new class.
-- @param baseclass The Baseclass of this class, or nil.
-- @return A new class reference.
function classes.class(baseclass)
-- Create the class definition and metatable.
local classdef = {}
-- Find the super class, either Object or user-defined.
baseclass = baseclass or classes.Object
-- If this class definition does not know of a function, it will 'look up' to the Baseclass via the __index of the metatable.
setmetatable(classdef, {__index = baseclass})
-- All class instances have a reference to the class object.
classdef.class = classdef
-- Looks up a key 'k' by traversing up the inheritance tree until a match is found.
local function lookup(t, k, rootInstance, fromSuper)
-- Quick value lookup, usually anything that is not a function will be set on the root instance.
local val = rawget(rootInstance, k)
if val ~= nil then return val end
-- When calling via the 'super' reference, we start traversing from that superclass.
-- Otherwise always start at the root of the inheritance tree.
local current
if fromSuper then
current = rawget(t, '__base')
else
current = rootInstance
end
repeat
val = rawget(current.class, k)
if val ~= nil then
if type(val) == 'function' and val ~= classes.Object.instanceOf then
-- Instead of returning the found function (which would be run with the original instance as self),
-- we pack it in another function which calls it with the 'current' level of the inheritance tree.
return function(tt, ...)
return val(current, ...)
end
else -- When calling instanceOf (which requires the root instance to work) or anything that's not function.
return val
end
end
-- We did not find the function in the class definition so we repeat the lookup on super.
if current.super then
current = rawget(current.super, '__base')
else
current = nil
end
until val ~= nil or current == nil
end
--- Recursively allocates the inheritance tree of the instance.
-- @param rootInstance The root of the inheritance tree.
-- @return Returns the instance with the allocated inheritance tree.
function classdef.alloc(rootInstance)
local instance = {
class = classdef,
super = { __base = baseclass.alloc(rootInstance) }
}
setmetatable(instance.super, {__index = function(t, k) return lookup(t, k, rootInstance, true) end, __newindex = rootInstance})
setmetatable(instance, {__index = function(t, k) return lookup(t, k, rootInstance, false) end, __newindex = rootInstance})
return instance
end
--- Constructs a new instance from this class definition.
-- @param arg Arguments to this class' constructor
-- @return Returns a new instance of this class.
function classdef.new(...)
-- Create the empty object.
local instance = {}
instance.class = classdef
-- Store the reference to the superclass instance in a separate table with its own metatable.
-- This way we can tell accesses via self.super apart (e.g. self:function() vs. self.super:function()).
instance.super = { __base = baseclass.alloc(instance) }
-- Accesses/reads (__index) get redirected to a lookup function which will traverse up the inheritance tree.
-- Assignment/writes (__newindex) get directed to the root instance itself.
setmetatable(instance.super, { __index = function(t, k) return lookup(t, k, instance, true) end })
setmetatable(instance, {__index = function(t, k) return lookup(t, k, instance, false) end})
-- Finally, init the object, it is up to the programmer to choose to call the super init method.
instance:init(...)
return instance
end
-- Finally, return the class we created.
return classdef
end
return classes
-- CREDITS: based on Paul Moore's classes library
--[[
The classes library enables simple OOP constructs using prototypes and meta-tables.
@author Paul Moore
Copyright (C) 2011 by Strange Ideas Software
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.
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.
]]