-- -- $Header: /Users/dap/lua/RCS/OO.lua,v 1.4 2007/02/11 09:53:01 dap Exp $ -- -- Author (a) 2007, Damon Anton Permezel. All bugs revered. -- -- Object Oriented Package for Lua -- module(..., package.seeall) function class(o) local cls = o or {} function cls:subclass(o, ...) local super = self local new_class = super:new(o, ...) function new_class:class() return new_class end function new_class:super() return super:class() end function new_class:init(...) -- -- important to use the current definition of super -- (ok: we have it here, but this is for doco) -- new_class:super().init(self, ...) end return new_class end -- return the class object of the instance function cls.class() return cls end -- return the super class object of the instance function cls.super() return nil end local function new(self, o, ...) self.__index = self self = setmetatable(o or {}, self) self:init(...) return self end cls.new = cls.new or new local function init(self, ...) end cls.init = cls.init or init local function copy(self, ...) local new = self:new(unpack(arg)) for n,v in pairs(self) do new[n] = v end return new end cls.copy = cls.copy or copy -- return true if the caller is an instance of class function cls:isa(class) local cur_class = cls while cur_class do if cur_class == class then return true else cur_class = cur_class:super() end end return false end return cls end