Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

98 rindas
3.0 KiB

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