init.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. local middleclass = {
  2. _VERSION = 'middleclass v4.1.1',
  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, {
  68. __index = function(_,k)
  69. local result = rawget(dict,k)
  70. if result == nil then
  71. return super.static[k]
  72. end
  73. return result
  74. end
  75. })
  76. else
  77. setmetatable(aClass.static, { __index = function(_,k) return rawget(dict,k) end })
  78. end
  79. setmetatable(aClass, { __index = aClass.static, __tostring = _tostring,
  80. __call = _call, __newindex = _declareInstanceMethod })
  81. return aClass
  82. end
  83. local function _includeMixin(aClass, mixin)
  84. assert(type(mixin) == 'table', "mixin must be a table")
  85. for name,method in pairs(mixin) do
  86. if name ~= "included" and name ~= "static" then aClass[name] = method end
  87. end
  88. for name,method in pairs(mixin.static or {}) do
  89. aClass.static[name] = method
  90. end
  91. if type(mixin.included)=="function" then mixin:included(aClass) end
  92. return aClass
  93. end
  94. local DefaultMixin = {
  95. __tostring = function(self) return "instance of " .. tostring(self.class) end,
  96. initialize = function(self, ...) end,
  97. isInstanceOf = function(self, aClass)
  98. return type(aClass) == 'table'
  99. and type(self) == 'table'
  100. and (self.class == aClass
  101. or type(self.class) == 'table'
  102. and type(self.class.isSubclassOf) == 'function'
  103. and self.class:isSubclassOf(aClass))
  104. end,
  105. static = {
  106. allocate = function(self)
  107. assert(type(self) == 'table', "Make sure that you are using 'Class:allocate' instead of 'Class.allocate'")
  108. return setmetatable({ class = self }, self.__instanceDict)
  109. end,
  110. new = function(self, ...)
  111. assert(type(self) == 'table', "Make sure that you are using 'Class:new' instead of 'Class.new'")
  112. local instance = self:allocate()
  113. instance:initialize(...)
  114. return instance
  115. end,
  116. subclass = function(self, name)
  117. assert(type(self) == 'table', "Make sure that you are using 'Class:subclass' instead of 'Class.subclass'")
  118. assert(type(name) == "string", "You must provide a name(string) for your class")
  119. local subclass = _createClass(name, self)
  120. for methodName, f in pairs(self.__instanceDict) do
  121. _propagateInstanceMethod(subclass, methodName, f)
  122. end
  123. subclass.initialize = function(instance, ...) return self.initialize(instance, ...) end
  124. self.subclasses[subclass] = true
  125. self:subclassed(subclass)
  126. return subclass
  127. end,
  128. subclassed = function(self, other) end,
  129. isSubclassOf = function(self, other)
  130. return type(other) == 'table' and
  131. type(self.super) == 'table' and
  132. ( self.super == other or self.super:isSubclassOf(other) )
  133. end,
  134. include = function(self, ...)
  135. assert(type(self) == 'table', "Make sure you that you are using 'Class:include' instead of 'Class.include'")
  136. for _,mixin in ipairs({...}) do _includeMixin(self, mixin) end
  137. return self
  138. end
  139. }
  140. }
  141. function middleclass.class(name, super)
  142. assert(type(name) == 'string', "A name (string) is needed for the new class")
  143. return super and super:subclass(name) or _includeMixin(_createClass(name), DefaultMixin)
  144. end
  145. setmetatable(middleclass, { __call = function(_, ...) return middleclass.class(...) end })
  146. return middleclass