Advertisement
theTANCO

Class.lua

Jul 28th, 2021 (edited)
897
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.33 KB | None | 0 0
  1. -- This is a script that attempts emulate the creation of class based objects
  2. --   used within OOP languages.
  3. -- C++ is the inspiration for the design of this script.
  4. -- You can get this program from https://pastebin.com/t2TvSiSU
  5. -- If you are running this in ComputerCraft, use the following command to
  6. --   download it: pastebin get t2TvSiSU "/API/Class.lua"
  7. -- In ComputerCraft, this file should be in "/API/" in the root directory to
  8. --   ensure all programs that use this script function properly.
  9. -- An in depth guide to this script can be found here:
  10. --   https://pastebin.com/upj3fgyZ
  11.  
  12. -- Sets a protected metatable.
  13. readOnly = function(t, meta, ro)
  14.   meta = meta or {}
  15.   meta.__pairs = meta.__pairs or function()
  16.     local keys = {}
  17.     for _, o in pairs({t, ro}) do
  18.       for k, v in next, o do
  19.         keys[k] = v
  20.       end
  21.     end
  22.     return next, keys
  23.   end
  24.   meta.__index = ro or meta.__index
  25.   meta.__metatable = meta.__metatable or false
  26.   return setmetatable(t, meta)
  27. end
  28.  
  29. -- Checks if values are or are NOT of specified types.
  30. typeCheck = function(member, memberName, types, level, invert)
  31.   local err = true
  32.   level = level or 3
  33.   local errorMessage = "expected '"..memberName.."' to be type "
  34.  
  35.   for a, b in ipairs(types) do
  36.     if a > 1 then errorMessage = errorMessage.." or " end
  37.     errorMessage = errorMessage..b
  38.     if (not invert and type(member) == b) or (invert and not type(member) == b) then
  39.       err = false
  40.       break
  41.     end
  42.   end
  43.   errorMessage = errorMessage..", got "..type(member)
  44.   if err then error(errorMessage, level) end
  45. end
  46.  
  47. -- Object Constructor.
  48. Object = function(class, ...)
  49.   typeCheck(class, "class", {"table"})
  50.   typeCheck(class.ctor, "ctor", {"function", "nil"})
  51.   typeCheck(class.protected, "protected", {"boolean", "nil"})
  52.   typeCheck(class.public, "public", {"table", "nil"})
  53.   typeCheck(class.readOnly, "readOnly", {"table", "nil"})
  54.   typeCheck(class.meta, "meta", {"table", "nil"})
  55.  
  56.   class.public = class.public or {}
  57.   class.meta = class.meta or {}
  58.  
  59.   class.meta.__len = class.meta.__len or function()
  60.     local size = 0
  61.     for k, v in ipairs(class.public) do
  62.       size = size + 1
  63.     end
  64.     return size
  65.   end
  66.   if class.ctor == nil then
  67.     class.meta.__call = class.meta.__call or function() end
  68.   else
  69.     class.meta.__call = function(self, ...)
  70.       return class.ctor(...)
  71.     end
  72.   end
  73.   if class.protected then
  74.     class.meta.__newindex = function() error("attempt to update a protected object", 2) end
  75.   end
  76.  
  77.   class.meta.__call(nil, ...)
  78.   local obj = readOnly(class.public, class.meta, class.readOnly)
  79.   return obj
  80. end
  81.  
  82.   -- Class Constructor.
  83. Class = function(header)
  84.   typeCheck(header, "header", {"function"})
  85.   local h = header()
  86.   typeCheck(h, "head", {"table"})
  87.   typeCheck(h.ctor, "ctor", {"function", "nil"})
  88.   typeCheck(h.protected, "protected", {"boolean", "nil"})
  89.   typeCheck(h.public, "public", {"table", "nil"})
  90.   typeCheck(h.readOnly, "readOnly", {"table", "nil"})
  91.   typeCheck(h.meta, "meta", {"table", "nil"})
  92.  
  93.   return readOnly({}, {
  94.       __index = {h = header},
  95.       __call = function(self, ...) return Object(header(...), ...) end,
  96.       __newindex = function() error("attempt to update a class, access denied", 3) end
  97.   })
  98. end
  99.  
  100. --[[ Changelog:
  101. The changelog has been moved to https://pastebin.com/PTWQcPsP
  102. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement