class.lua 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. local middleclass = {
  2. _VERSION = 'middleclass v4.1.0',
  3. _DESCRIPTION = 'Object Orientation for Lua',
  4. _URL = 'https://github.com/kikito/middleclass',
  5. _LICENSE = [[
  6. MIT LICENSE
  7. Copyright (c) 2011 Enrique García Cota
  8. Permission is hereby granted, free of charge, to any person obtaining a
  9. copy of this software and associated documentation files (the
  10. "Software"), to deal in the Software without restriction, including
  11. without limitation the rights to use, copy, modify, merge, publish,
  12. distribute, sublicense, and/or sell copies of the Software, and to
  13. permit persons to whom the Software is furnished to do so, subject to
  14. the following conditions:
  15. The above copyright notice and this permission notice shall be included
  16. in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. ]]
  25. }
  26. local function _createIndexWrapper(aClass, f)
  27. if f == nil then
  28. return aClass.__instanceDict
  29. else
  30. return function(self, name)
  31. local value = aClass.__instanceDict[name]
  32. if value ~= nil then
  33. return value
  34. elseif type(f) == "function" then
  35. return (f(self, name))
  36. else
  37. return f[name]
  38. end
  39. end
  40. end
  41. end
  42. local function _propagateInstanceMethod(aClass, name, f)
  43. f = name == "__index" and _createIndexWrapper(aClass, f) or f
  44. aClass.__instanceDict[name] = f
  45. for subclass in pairs(aClass.subclasses) do
  46. if rawget(subclass.__declaredMethods, name) == nil then
  47. _propagateInstanceMethod(subclass, name, f)
  48. end
  49. end
  50. end
  51. local function _declareInstanceMethod(aClass, name, f)
  52. aClass.__declaredMethods[name] = f
  53. if f == nil and aClass.super then
  54. f = aClass.super.__instanceDict[name]
  55. end
  56. _propagateInstanceMethod(aClass, name, f)
  57. end
  58. local function _tostring(self) return "class " .. self.name end
  59. local function _call(self, ...) return self:new(...) end
  60. local function _createClass(name, super)
  61. local dict = {}
  62. dict.__index = dict
  63. local aClass = { name = name, super = super, static = {},
  64. __instanceDict = dict, __declaredMethods = {},
  65. subclasses = setmetatable({}, {__mode='k'}) }
  66. if super then
  67. setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) or super.static[k] end })
  68. else
  69. setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) end })
  70. end
  71. setmetatable(aClass, { __index = aClass.static, __tostring = _tostring,
  72. __call = _call, __newindex = _declareInstanceMethod })
  73. return aClass
  74. end
  75. local function _includeMixin(aClass, mixin)
  76. assert(type(mixin) == 'table', "mixin must be a table")
  77. for name,method in pairs(mixin) do
  78. if name ~= "included" and name ~= "static" then aClass[name] = method end
  79. end
  80. for name,method in pairs(mixin.static or {}) do
  81. aClass.static[name] = method
  82. end
  83. if type(mixin.included)=="function" then mixin:included(aClass) end
  84. return aClass
  85. end
  86. local DefaultMixin = {
  87. __tostring = function(self) return "instance of " .. tostring(self.class) end,
  88. initialize = function(self, ...) end,
  89. isInstanceOf = function(self, aClass)
  90. return type(aClass) == 'table' and (aClass == self.class or self.class:isSubclassOf(aClass))
  91. end,
  92. static = {
  93. allocate = function(self)
  94. assert(type(self) == 'table', "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
  95. return setmetatable({ class = self }, self.__instanceDict)
  96. end,
  97. new = function(self, ...)
  98. assert(type(self) == 'table', "Make sure that you are using 'Class:new' instead of 'Class.new'")
  99. local instance = self:allocate()
  100. instance:initialize(...)
  101. return instance
  102. end,
  103. subclass = function(self, name)
  104. assert(type(self) == 'table', "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
  105. assert(type(name) == "string", "You must provide a name(string) for your class")
  106. local subclass = _createClass(name, self)
  107. for methodName, f in pairs(self.__instanceDict) do
  108. _propagateInstanceMethod(subclass, methodName, f)
  109. end
  110. subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end
  111. self.subclasses[subclass] = true
  112. self:subclassed(subclass)
  113. return subclass
  114. end,
  115. subclassed = function(self, other) end,
  116. isSubclassOf = function(self, other)
  117. return type(other) == 'table' and
  118. type(self.super) == 'table' and
  119. ( self.super == other or self.super:isSubclassOf(other) )
  120. end,
  121. include = function(self, ...)
  122. assert(type(self) == 'table', "Make sure you that you are using 'Class:include' instead of 'Class.include'")
  123. for _,mixin in ipairs({...}) do _includeMixin(self, mixin) end
  124. return self
  125. end
  126. }
  127. }
  128. function middleclass.class(name, super)
  129. assert(type(name) == 'string', "A name (string) is needed for the new class")
  130. return super and super:subclass(name) or _includeMixin(_createClass(name), DefaultMixin)
  131. end
  132. setmetatable(middleclass, { __call = function(_, ...) return middleclass.class(...) end })
  133. return middleclass