Skip to content

Instantly share code, notes, and snippets.

@ghoomfrog
Last active January 17, 2020 15:16
Show Gist options
  • Save ghoomfrog/e6048a7b5e6428abc25568c21e3903a3 to your computer and use it in GitHub Desktop.
Save ghoomfrog/e6048a7b5e6428abc25568c21e3903a3 to your computer and use it in GitHub Desktop.
Simple OOP functions for Lua.
--[[
MIT License
Copyright (c) 2019 Unlimiter
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.
--]]
-- Return true if `t` is a class.
function isclass(t)
if type(t) == 'table' then
if type(t.__bases) == 'table'
and type(t.__instances) == 'table'
then
return true
end
end
return false
end
-- Return true if `v` is an instance of the class `c`.
function isinstanceof(v, c)
if isclass(c) then
if type(v) == 'table' then
for i, instance in ipairs(c.__instances) do
if c.__instances[i] == v then
return true
end
end
return false
else
return false
end
else
error('bad argument #' .. 2 .. " to 'isinstanceof' (class expected, got "
.. type(c) .. ')')
end
end
-- Return true if `c` is the class of all items in `...`.
function isclassof(c, ...)
if isclass(c) then
if #{...} > 0 then
for i, v in ipairs({...}) do
if not isinstanceof(v, c) then
return false
end
end
return true
end
return false
else
error('bad argument #' .. 1 .. " to 'isclassof' (class expected, got "
.. type(c) .. ')')
end
end
-- Return true if `key` is a class-special key.
function isspecial(key)
if key == '__bases'
or key == '__instances'
or key == '__init'
then
return true
end
return false
end
-- Copy all items in `source` into `destination` or a new table, recursively.
-- Return `destination`.
function copyrecursively(source, destination)
if type(source) ~= 'table' then
error('bad argument #' .. 1 .. " to 'copyrecursively' (table expected, got "
.. type(source) .. ')')
end
destination = destination or {}
for k, v in pairs(source) do
if type(v) == 'table' then
copyrecursively(v, destination[k])
else
destination[k] = v
end
end
return destination
end
-- Make the class `c` inherit from all classes in `...`.
function inherit(c, ...)
if isclass(c) then
if select('#', ...) > 0 then
local bases = {...}
local hasbase
for i, newbase in ipairs(bases) do
if not isclass(newbase) then
error("bad vararg argument to 'inherit' (class expected, got "
.. type(newbase) .. ')')
end
for j, base in ipairs(c.__bases) do
if base == newbase then
hasbase = true
end
end
if hasbase then
table.remove(bases, i)
else
table.insert(c.__bases, newbase)
end
end
for i, base in ipairs(bases) do
for k, v in pairs(base) do
if k ~= '__bases' and k ~= '__instances' then
if type(v) == 'table' then
c[k] = copyrecursively(v)
else
c[k] = v
end
end
end
end
return c
end
else
error('bad argument #' .. 1 .. " to 'inherit' (class expected, got "
.. type(c) .. ')')
end
end
-- Return the most basic table defined as a class.
function basicclass()
return {
__bases = {},
__instances = {},
}
end
-- Return a new class that inherits from all items in `...` if given.
function class(...)
local c = basicclass()
local function assign(fields)
if type(fields) == 'table' then
for k, v in pairs(fields) do
c[k] = v
end
setmetatable(
c,
{
__call = function(c, ...)
local instance = setmetatable({}, c)
for k, v in pairs(c) do
if not isspecial(k) then
instance[k] = v
end
end
if c.__init then
c.__init(instance, ...)
end
table.insert(c.__instances, instance)
instance.__class = c
return instance
end
}
)
else
local address = tostring(c):sub(8)
error('bad argument #' .. 1 .. ' to class (' .. address
.. ') constructor (table expected, got ' .. type(fields) .. ')')
end
return c
end
if isclass(...) then
inherit(c, ...)
return assign
else
return assign(...)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment