mirror of
https://github.com/Reuh/classtoi.git
synced 2025-10-27 12:19:31 +00:00
Add classtoi-light
This commit is contained in:
parent
4db788df32
commit
3bb90f9034
6 changed files with 128 additions and 1 deletions
62
classtoi-light.lua
Normal file
62
classtoi-light.lua
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
--- Reuh's light class library version 0.1.0. Lua 5.1-5.3 and LuaJIT compatible.
|
||||
-- Lighter and faster version of classtoi, keeping the same syntax but with uncommon features removed.
|
||||
-- Features:
|
||||
-- * class creation and inheritance with class(table...) or subclass(table...) (left-to-right priority). Parent classes are shallowly copied on creation in the new class.
|
||||
-- * instance creation with class:new, which calls the :init constructor (which can returns another object instead of the automaticaly created one)
|
||||
-- * every methamethod supported in instances except __index
|
||||
-- * a default :is method to check if an object is an instance of a class (ignore parents): class:is(obj) or obj:is(class) returns true if obj is of class
|
||||
-- * a default __tostring method and __name attribute for pretty printing of objects and classes
|
||||
-- Main differences from classtoi:
|
||||
-- * methamethods are only applied to instances and not classes
|
||||
-- * can't redefine __init
|
||||
-- * instance constructor is renamed to :init
|
||||
-- * inherited fields and methods are copied on creation; adding a new field to a parent class after creation will not affect child classes
|
||||
-- * is can only be called in its class:is(object) or object:is(class) form (returns true if object is of class), and ignore parents
|
||||
-- * no class commons support
|
||||
-- Please note that if you redefine :new or :is, they will be used instead of the class default.
|
||||
|
||||
local newClass, class_mt
|
||||
newClass = function(...)
|
||||
local class = {}
|
||||
for _, mixin in ipairs({...}) do
|
||||
for k, v in pairs(mixin) do
|
||||
class[k] = v
|
||||
end
|
||||
end
|
||||
class.__index = class
|
||||
return setmetatable(class, class_mt)
|
||||
end
|
||||
class_mt = {
|
||||
new = function(self, ...)
|
||||
local obj = setmetatable({}, self)
|
||||
return obj.init and obj:init(...) or obj
|
||||
end,
|
||||
is = function(self, other)
|
||||
if getmetatable(self) == class_mt then -- class:is(obj)
|
||||
return getmetatable(other) == self
|
||||
else -- obj:is(class)
|
||||
return getmetatable(self) == other
|
||||
end
|
||||
end,
|
||||
__call = newClass,
|
||||
__tostring = function(self)
|
||||
local mt = getmetatable(self)
|
||||
setmetatable(self, nil)
|
||||
local str = tostring(self)
|
||||
setmetatable(self, mt)
|
||||
return str:gsub("^table", "class")
|
||||
end
|
||||
}
|
||||
class_mt.__index = class_mt
|
||||
|
||||
-- base class
|
||||
return newClass {
|
||||
__name = "object",
|
||||
__tostring = function(self)
|
||||
local mt = getmetatable(self)
|
||||
setmetatable(self, nil)
|
||||
local str = tostring(self)
|
||||
setmetatable(self, mt)
|
||||
return str:gsub("^table", self.__name)
|
||||
end
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue