Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Tiny class lib, made in python style, wroted by Tommy (aka Snusmumriken)
- -- JAIL License (C)
- -- If you see it, you will go to jail.
- local Class
- local smt, gmt = setmetatable, getmetatable
- local function C(v, t) return type(v) == t and v or nil end -- checking shortcut
- -- set of utily functions
- local cls = {}
- function cls:Copy(...)
- local mt = gmt(self)
- local copy = {}
- for k, v in pairs(self) do
- local type = type(v)
- if type == 'table' then
- copy[k] = cls.Copy(v) -- recursive
- elseif type == 'cdata' then
- local c_clone = v.clone or v.copy
- if c_clone then
- copy[k] = c_clone(v)
- end
- else
- copy[k] = v
- end
- end
- return mt and smt(copy, mt) or copy
- end
- -- raw copy methods and fields
- function cls:Include(b)
- for k, v in pairs(b) do
- self[k] = v
- end
- end
- function cls:Child(name)
- return Class(self, name)
- end
- function cls:Type(v)
- -- return name of current class
- if not v then
- return self.Class.__name
- end
- -- check object is part of class
- if C(v, 'table') then
- return self.Class == v
- end
- -- check all class and superclass names
- if C(v, 'string') then
- local c = self.Class
- while c do
- if c.__name == v then
- return true
- end
- c = c.Super -- goto superclass
- end
- end
- return false
- end
- function cls:IsObjectOf(self, class)
- return rawequal(class, gmt(self))
- end
- function cls:__tostring()
- return self.__name
- end
- function cls:__concat(other)
- return tostring(self) .. tostring(other)
- end
- function Class(a, b)
- local mt = {}
- -- parent class, if exists
- mt.__index = C(a, 'table') or C(b, 'table')
- function mt:__tostring()
- return ('<Class: %s>'):format(self.__name)
- end
- function mt:__concat(other)
- return tostring(self) .. tostring(other)
- end
- local cl = {}
- cl.__name = C(a, 'string') or C(b, 'string') or tostring(cl):match('0x%x+')
- cl.__index = cl -- looping, but metamethod access
- cl.Class = cl
- cl.Super = mt.__index -- access to parent class. Super.Super.Super
- function mt:__call(...)
- local obj = {} -- new obj
- obj.__name = ('<Object: %s>'):format(cl.__name .. ' ' .. tostring(obj):match('0x%x+'))
- obj = smt(obj, cl)
- local init = self.init or self.new
- if not init then return obj end -- without init func - fine too
- return obj, init(obj, ...) -- return obj and any returns in init func
- end
- cls.Include(cl, cls)
- return smt(cl, mt)
- end
- if ... then
- return Class
- end
- local foo = Class('foo')
- foo.foo = 'Hey'
- local bar = Class('bar', foo)
- a = bar()
- local var = bar:Child('fobar')
- -- init and new is the same
- function var:new(x, y)
- self.x, self.y = x, y
- end
- local obj = var(10, 20)
- print(var.foo) --> hey
- print(obj.foo) --> hey
- print(obj:Type('foo')) --> true
- print(obj:Type('bar')) --> true
- print(obj:Type('fobar')) --> true
- print(obj:Type('Fobar')) --> false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement