-
Notifications
You must be signed in to change notification settings - Fork 0
/
prototype.lua
127 lines (90 loc) · 2.81 KB
/
prototype.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
--- File: Prototype Inheritance
--- See <http://en.wikipedia.org/wiki/Prototype_based_programming> for detailed information about
--- prototype inheritance.
--- Module: otlib
module( "otlib", package.seeall )
local function __call( self, ... )
local new = self:Clone( true )
if new.Init then
new:Init( ... )
end
return new
end
--[[
Function: Clone
Creates a clone of an object.
Parameters:
base - The *table* to clone from.
callable - An *optional boolean*. If true, __call is set on the clone and returns another
clone. If the function Init is defined, it will be called with whatever parameters are
passed to __call. Defaults to _false_.
clone - An *optional table* to set as a clone, this value is what is returned. Defaults to
an _empty table_.
Returns:
The *table* from the parameter clone.
Revisions:
v1.00 - Initial.
]]
function Clone( base, callable, clone )
clone = clone or {}
local mt = getmetatable( clone ) or {}
mt.__index = base
if callable then
mt.__call = __call
end
setmetatable( clone, mt )
return clone
end
--[[
Function: Parent
Gets the parent of a clone.
Parameters:
clone - The *table* clone that you want to know about.
Returns:
A *table* or *nil* specifying the parent, nil if no parent.
Revisions:
v1.00 - Initial.
]]
function Parent( clone )
local mt = getmetatable( clone )
return mt and mt.__index
end
--[[
Function: IsA
Check if a clone is inherited from another.
Parameters:
clone - The *table* clone that you want to know about.
base - The *table* clone to check against.
Returns:
A *boolean* specifying whether or not clone is inherited from base.
Notes:
* Returns true if the tables are equal to each other (since a derived class IS A derived
class, it makes sense).
Revisions:
v1.00 - Initial.
]]
function IsA( clone, base )
if clone == base then
return true
end
local mt = getmetatable( clone )
while mt ~= nil and mt.__index ~= nil do
local index = mt.__index
if index == base then
return true
end
mt = getmetatable( index )
end
return false
end
--[[
Object: otlib.object
Merely serves as a convenient wrapper and root prototype.
]]
object = Clone( table, false, { Clone = Clone, IsA = IsA, Parent=Parent } )
--[[
Functions: otlib.object
Clone - Exactly the same as <otlib.Clone>.
IsA - Exactly the same as <otlib.IsA>.
Parent - Exactly the same as <otlib.Parent>.
]]