Debian Codegraph @

CorsixTH/Lua/class.lua

raw
1 --[[ Copyright (c) 2009 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 ---- Primitive class system ---------------------------------------------------
22 -- Declaring a class:
23 -- class "Name"
24 -- OR
25 -- class "Name" (SuperclassName)
26 --
27 -- ---@type Name
28 -- local Name = _G["Name"]
29 --
30 -- function Name:Name(arguments)
31 -- self:SuperclassName(arguments) -- required when there is a superclass
32 -- --(constructor)--
33 -- end
34 -- function Name:method(arguments)
35 -- --(generic method)--
36 -- end
37 --
38 -- Creating a class instance:
39 -- variable = Name(constructor_arguments)
40 --
41 -- Classes can also be made by "adopting" a table - taking an existing table
42 -- and turning it into a class instance, rather than always creating a new
43 -- table when the constructor is called. To enable this, put "{}" after the
44 -- class declaration, as in:
45 -- class "NameWhichAdopts" {}
46 -- Then the first argument to the constructor is treated as a table to turn
47 -- into an instance of the class, as in:
48 -- variable = NameWhichAdopts{named_constructor_argument = 42, another = 43}
49 --
50 -- Using a class instance:
51 -- variable:method(arguments)
52 -- OR
53 -- Name.method(variable, arguments)
54 --
55 -- The latter form can be used to call a method of a superclass when the
56 -- subclass overrides it with a method of the same name.
57 -- C code does not use this file, but does use the same syntax for creating a
58 -- class instance and calling methods on it.
59
60 local setmetatable, getmetatable, type
61 = setmetatable, getmetatable, type
62
63 local function define_class(name, super, adopts_self)
64 local mt = {}
65
66 local function new_class(methods, ...)
67 local constructor = methods[name]
68 local self
69 if adopts_self and ... then
70 self = setmetatable(..., mt)
71 constructor(...)
72 else
73 self = setmetatable({}, mt)
74 constructor(self, ...)
75 end
76 return self
77 end
78
79 local methods = {}
80 local methods_mt = {}
81
82 mt.__index = methods
83 setmetatable(methods, methods_mt)
84 if super ~= nil then
85 methods_mt.__index = super
86 end
87 methods_mt.__call = new_class
88 methods_mt.__class_name = name
89
90 methods._metatable = mt
91 _G[name] = methods
92 end
93
94 strict_declare_global "class"
95 class = destrict(function(_, name)
96 define_class(name)
97 local adopts_self = false
98 local super = nil
99
100 local function extend(arg)
101 if type(arg) == "table" and next(arg) == nil and not getmetatable(arg) then
102 -- {} decorator
103 adopts_self = true
104 else
105 -- (Superclass) decorator
106 if arg == nil then
107 error("Superclass not defined at subclass definition")
108 end
109 super = arg
110 end
111 define_class(name, super, adopts_self)
112 return extend
113 end
114 return extend
115 end)
116 class = setmetatable({}, {__call = class})
117
118 -- class.is - Tests if a given class object (first parameter) is an instance
119 -- of (something derived from) a given type (second parameter).
120 -- For example:
121 -- class "something" (base)
122 -- class "something_else"
123 -- variable = something()
124 -- class.is(variable, something) --> true
125 -- class.is(variable, base) --> true
126 -- class.is(variable, something_else) --> false
127 function class.is(instance, class)
128 local typ = type(instance)
129 if typ ~= "table" and typ ~= "userdata" then
130 return false
131 end
132 local methods = instance
133 while methods do
134 if methods == class then
135 return true
136 end
137 local mt = getmetatable(methods)
138 methods = mt and mt.__index
139 end
140 return false
141 end
142
143 -- class.name - Get the name of a class
144 -- For example:
145 -- class "something"
146 -- class.name(something) --> "something"
147 function class.name(class)
148 local mt = getmetatable(class)
149 return mt and mt.__class_name
150 end
151
152 -- class.superclass - Get the superclass of a class
153 -- For example:
154 -- class "something" (base)
155 -- class.superclass(something) --> base
156 function class.superclass(class)
157 return getmetatable(class).__index
158 end
159
160 -- class.type - Get the typename of a class instance
161 -- For example:
162 -- class "something" (base)
163 -- variable = something()
164 -- class.type(variable) --> "something"
165 function class.type(instance)
166 local mt = getmetatable(instance)
167 if not mt then
168 return nil
169 end
170 local methods_mt = getmetatable(mt.__index)
171 if not methods_mt then
172 return nil
173 end
174 return methods_mt.__class_name
175 end