Advertisement
joseleeph

Untitled

Jan 5th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. --[[
  2. Copyright (c) 2010-2013 Matthias Richter
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13.  
  14. Except as contained in this notice, the name(s) of the above copyright holders
  15. shall not be used in advertising or otherwise to promote the sale, use or
  16. other dealings in this Software without prior written authorization.
  17.  
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. ]]--
  26.  
  27. local function include_helper(to, from, seen)
  28. if from == nil then
  29. return to
  30. elseif type(from) ~= 'table' then
  31. return from
  32. elseif seen[from] then
  33. return seen[from]
  34. end
  35.  
  36. seen[from] = to
  37. for k,v in pairs(from) do
  38. k = include_helper({}, k, seen) -- keys might also be tables
  39. if to[k] == nil then
  40. to[k] = include_helper({}, v, seen)
  41. end
  42. end
  43. return to
  44. end
  45.  
  46. -- deeply copies `other' into `class'. keys in `other' that are already
  47. -- defined in `class' are omitted
  48. local function include(class, other)
  49. return include_helper(class, other, {})
  50. end
  51.  
  52. -- returns a deep copy of `other'
  53. local function clone(other)
  54. return setmetatable(include({}, other), getmetatable(other))
  55. end
  56.  
  57. local function new(class)
  58. -- mixins
  59. class = class or {} -- class can be nil
  60. local inc = class.__includes or {}
  61. if getmetatable(inc) then inc = {inc} end
  62.  
  63. for _, other in ipairs(inc) do
  64. if type(other) == "string" then
  65. other = _G[other]
  66. end
  67. include(class, other)
  68. end
  69.  
  70. -- class implementation
  71. class.__index = class
  72. class.init = class.init or class[1] or function() end
  73. class.include = class.include or include
  74. class.clone = class.clone or clone
  75.  
  76. -- constructor call
  77. return setmetatable(class, {__call = function(c, ...)
  78. local o = setmetatable({}, c)
  79. o:init(...)
  80. return o
  81. end})
  82. end
  83.  
  84. -- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
  85. if class_commons ~= false and not common then
  86. common = {}
  87. function common.class(name, prototype, parent)
  88. return new{__includes = {prototype, parent}}
  89. end
  90. function common.instance(class, ...)
  91. return class(...)
  92. end
  93. end
  94.  
  95.  
  96. -- the module
  97. return setmetatable({new = new, include = include, clone = clone},
  98. {__call = function(_,...) return new(...) end})
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement