-
Notifications
You must be signed in to change notification settings - Fork 495
Expand file tree
/
Copy pathmultiple.lua
More file actions
executable file
·105 lines (101 loc) · 4.29 KB
/
Copy pathmultiple.lua
File metadata and controls
executable file
·105 lines (101 loc) · 4.29 KB
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
--------------------------------------------------------------------------------
---------------------- ## ##### ##### ###### -----------------------
---------------------- ## ## ## ## ## ## ## -----------------------
---------------------- ## ## ## ## ## ###### -----------------------
---------------------- ## ## ## ## ## ## -----------------------
---------------------- ###### ##### ##### ## -----------------------
---------------------- -----------------------
----------------------- Lua Object-Oriented Programming ------------------------
--------------------------------------------------------------------------------
-- Project: LOOP - Lua Object-Oriented Programming --
-- Release: 2.3 beta --
-- Title : Multiple Inheritance Class Model --
-- Author : Renato Maia <[email protected]> --
--------------------------------------------------------------------------------
-- Exported API: --
-- class(class, ...) --
-- new(class, ...) --
-- classof(object) --
-- isclass(class) --
-- instanceof(object, class) --
-- memberof(class, name) --
-- members(class) --
-- superclass(class) --
-- subclassof(class, super) --
-- supers(class) --
--------------------------------------------------------------------------------
local unpack = unpack
local require = require
local ipairs = ipairs
local select = select
local table = require "loop.table"
module "loop.multiple"
--------------------------------------------------------------------------------
local base = require "loop.simple"
--------------------------------------------------------------------------------
table.copy(base, _M)
--------------------------------------------------------------------------------
local MultipleClass = {
__call = new,
__index = function (self, field)
self = base.classof(self)
for _, super in ipairs(self) do
local value = super[field]
if value ~= nil then return value end
end
end,
}
function class(class, ...)
if select("#", ...) > 1
then return base.rawnew(table.copy(MultipleClass, {...}), initclass(class))
else return base.class(class, ...)
end
end
--------------------------------------------------------------------------------
function isclass(class)
local metaclass = base.classof(class)
if metaclass then
return metaclass.__index == MultipleClass.__index or
base.isclass(class)
end
end
--------------------------------------------------------------------------------
function superclass(class)
local metaclass = base.classof(class)
if metaclass then
local indexer = metaclass.__index
if (indexer == MultipleClass.__index)
then return unpack(metaclass)
else return metaclass.__index
end
end
end
--------------------------------------------------------------------------------
local function isingle(single, index)
if single and not index then
return 1, single
end
end
function supers(class)
local metaclass = classof(class)
if metaclass then
local indexer = metaclass.__index
if indexer == MultipleClass.__index
then return ipairs(metaclass)
else return isingle, indexer
end
end
return isingle
end
--------------------------------------------------------------------------------
function subclassof(class, super)
if class == super then return true end
for _, superclass in supers(class) do
if subclassof(superclass, super) then return true end
end
return false
end
--------------------------------------------------------------------------------
function instanceof(object, class)
return subclassof(classof(object), class)
end