-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluoop.lua
More file actions
executable file
·170 lines (143 loc) · 6.66 KB
/
luoop.lua
File metadata and controls
executable file
·170 lines (143 loc) · 6.66 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
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
-----
-- Luoop - easy and flexible object oriented library for Lua
-- Author: Teddy Engel <engel.teddy[at]gmail.com> / @Teddy_Engel
-- Version: 1.2
--
-- This is an implementation of a object-oriented Lua module, coded entirely in Lua.
-- It is meant to be simple and flexile, since the main initial requirement was multiple inheritance and overloading + the ability
-- to call constructors / destructors with custom parameters.
--
-- MIT License (MIT)
-- Copyright (c) 2013 Teddy Engel
-- 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.
--
--- PRIVATE FUNCTIONS ---
local function _createSuperclass(oClassDefinition, oSuperclass)
assert(type(oSuperclass) == 'table', '_createSuperclass expects a valid superclass')
assert(oSuperclass.init, '_createSuperclass expects a superclass with a constructor defined')
-- We copy all methods / variables from the superclass to the current class
for sMethodName, oMethod in pairs(oSuperclass) do
-- Exceptions for internally used variables
if sMethodName ~= '__aParents'
and sMethodName ~= '__aAllParents'
and sMethodName ~= '__index'
and sMethodName ~= '__bSingleton'
and sMethodName ~= 'isSingleton'
and sMethodName ~= 'enableSingleton'
and sMethodName ~= 'disableSingleton'
and sMethodName ~= 'newSingleton'
and sMethodName ~= 'destroySingleton' then
oClassDefinition[sMethodName] = oMethod
end
end
if oSuperclass.__aAllParents then
for oParentKey, oParentValue in pairs(oSuperclass.__aAllParents) do
oClassDefinition.__aAllParents[oParentKey] = oParentValue
end
end
oClassDefinition.__aAllParents[oSuperclass] = oSuperclass
oClassDefinition.__aParents[oSuperclass] = oSuperclass
end
--- MAIN CALL ---
function class(init, ...)
local oSuperClasses = {...}
local oClassDefinition = {} -- a new class instance
-- Parameter to say if the class is a singleton or not
oClassDefinition.__bSingleton = false
-- Array to store the superclasses directly above this one
oClassDefinition.__aParents = {}
-- Array to store the full hierarchy of superclasses
oClassDefinition.__aAllParents = {}
-- Creating all superclasses
for oKey, oSuperClass in pairs(oSuperClasses) do _createSuperclass(oClassDefinition, oSuperClass) end
oClassDefinition.__index = oClassDefinition
local function __createNewObject(...)
local oObject = {}
setmetatable(oObject, oClassDefinition)
-- Use this function to know if the class has the passed class has a parent
oObject._hasParentClass = function(oObject, oSuperclass)
return oObject.__aAllParents[oSuperclass] ~= nil
end
-- Use this method to call a parent's method if implemented
oObject._parentMethod = function(oObject, oSuperclass, sMethodName, ...)
assert(type(oSuperclass) == 'table', 'expects a valid superclass')
assert(oObject._hasParentClass(oObject, oSuperclass) == true, '_parentConstructor passed super class must be valid')
assert(oSuperclass[sMethodName] ~= nil, '_parentMethod passed super class must implement the method')
return oSuperclass[sMethodName](oObject, ...)
end
-- Use this function to call the constructor on specific object you created, passing the superclass and variable parameters
oObject._parentConstructor = function(oObject, oSuperclass, ...)
assert(type(oSuperclass) == 'table', 'expects a valid superclass')
assert(oObject._hasParentClass(oObject, oSuperclass) == true, '_parentConstructor passed super class must be valid')
if oSuperclass.init then
oSuperclass.init(oObject, ...)
end
end
-- Use this function to call the destructor on specific object you created, passing the superclass and variable parameters
oObject._parentDestructor = function (oObject, oSuperclass, ...)
assert(type(oSuperclass) == 'table', 'expects a valid superclass')
assert(oObject._hasParentClass(oObject, oSuperclass) == true, '_parentDestructor passed super class must be valid')
if oSuperclass.destroy then
oSuperclass.destroy(oObject, ...)
end
end
-- Then the child constructor
if init then
init(oObject, ...)
end
return oObject
end
-- Exposes a constructor which can be called by classname(<args>)
local mt = {}
mt.__call = function(class_tbl, ...)
local oObject = nil
if oClassDefinition.__bSingleton == false then
oObject = __createNewObject(...)
end
return oObject
end
-- Singleton handling
local oSingleton = nil
oClassDefinition.isSingleton = function(self)
return oClassDefinition.__bSingleton
end
oClassDefinition.enableSingleton = function(self)
self.__bSingleton = true
end
oClassDefinition.disableSingleton = function()
assert(oSingleton == nil, 'disableSingleton cannot be called once a singleton has been instantiated')
oClassDefinition.__bSingleton = false
end
oClassDefinition.newSingleton = function(...)
if oClassDefinition:isSingleton() and oSingleton == nil then
oSingleton = __createNewObject(...)
end
return oSingleton
end
oClassDefinition.destroySingleton = function(...)
if oSingleton ~= nil then
if oSingleton.destroy then
oSingleton:destroy(...)
end
oSingleton = nil
end
end
oClassDefinition.init = init
-- We expose the constructor with the method new() to allow instantiating from an existing object
oClassDefinition.newInstance = mt.__call
setmetatable(oClassDefinition, mt)
return oClassDefinition
end