Module:Color/utils/class

From Space Station 14 Wiki
Revision as of 07:56, 24 May 2025 by Aliser (talk | contribs) (Created page with "-- Code based on: -- http://lua-users.org/wiki/SimpleLuaClasses --- Helper function to create classes -- -- @usage local Color = class(function () --constructor end) -- @usage local Color2 = class( -- Color, -- function () --constructor end, -- { prop_a = "some value" } -- ) local function class(base, init, defaults) local c = defaults or {} -- a new class instance if not init and type(base) == 'function' then init = base base = nil el...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Color/utils/class/doc

-- Code based on:
-- http://lua-users.org/wiki/SimpleLuaClasses

--- Helper function to create classes
--
-- @usage local Color = class(function () --[[ constructor ]] end)
-- @usage local Color2 = class(
--   Color,
--   function () --[[ constructor ]] end,
--   { prop_a = "some value" }
-- )
local function class(base, init, defaults)
  local c = defaults or {}    -- a new class instance
  if not init and type(base) == 'function' then
    init = base
    base = nil
  elseif type(base) == 'table' then
   -- our new class is a shallow copy of the base class!
    for i,v in pairs(base) do
      c[i] = v
    end
    c._base = base
  end
  -- the class will be the metatable for all its objects,
  -- and they will look up their methods in it.
  c.__index = c

  -- expose a constructor which can be called by <classname>(<args>)
  local mt = {}
  mt.__call = function(class_tbl, ...)
    local obj = {}
    setmetatable(obj,c)
    if init then
      init(obj,...)
    else 
      -- make sure that any stuff from the base class is initialized!
      if base and base.init then
      base.init(obj, ...)
      end
    end
    return obj
  end
  c.init = init
  c.is_a = function(self, klass)
    local m = getmetatable(self)
    while m do 
      if m == klass then return true end
      m = m._base
    end
    return false
  end
  setmetatable(c, mt)
  return c
end

return class