schwiti100

Untitled

Jul 25th, 2021
868
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or
  5. (at your option) any later version.
  6.  
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10. GNU General Public License for more details.
  11.  
  12. You should have received a copy of the GNU General Public License
  13. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. ]]
  15.  
  16. -- Class implementation stolen from http://lua-users.org/wiki/SimpleLuaClasses
  17. -- and https://github.com/Courseplay/courseplay
  18.  
  19. function Class(base, init)
  20.     local c = {}    -- a new class instance
  21.     if not init and type(base) == 'function' then
  22.         init = base
  23.         base = nil
  24.     elseif type(base) == 'table' then
  25.         -- our new class is a shallow copy of the base class!
  26.         for i,v in pairs(base) do
  27.             c[i] = v
  28.         end
  29.         c._base = base
  30.     end
  31.     -- the class will be the metatable for all its objects,
  32.     -- and they will look up their methods in it.
  33.     c.__index = c
  34.  
  35.     -- expose a constructor which can be called by <classname>(<args>)
  36.     local mt = {}
  37.     mt.__call = function(class_tbl, ...)
  38.         local obj = {}
  39.         setmetatable(obj,c)
  40.         if class_tbl.init then
  41.             class_tbl.init(obj,...)
  42.         else
  43.             -- make sure that any stuff from the base class is initialized!
  44.             if base and base.init then
  45.                 base.init(obj, ...)
  46.             end
  47.         end
  48.         return obj
  49.     end
  50.     c.init = init
  51.     c.is_a = function(self, klass)
  52.         local m = getmetatable(self)
  53.         while m do
  54.             if m == klass then return true end
  55.             m = m._base
  56.         end
  57.         return false
  58.     end
  59.     setmetatable(c, mt)
  60.     return c
  61. end
  62.  
RAW Paste Data