class.lua 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. --[[
  2. Copyright (c) 2010-2011 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 __NULL__() end
  23. -- class "inheritance" by copying functions
  24. local function inherit(class, interface, ...)
  25. if not interface then return end
  26. assert(type(interface) == "table", "Can only inherit from other classes.")
  27. -- __index and construct are not overwritten as for them class[name] is defined
  28. for name, func in pairs(interface) do
  29. if not class[name] then
  30. class[name] = func
  31. end
  32. end
  33. for super in pairs(interface.__is_a or {}) do
  34. class.__is_a[super] = true
  35. end
  36. return inherit(class, ...)
  37. end
  38. -- class builder
  39. local function new(args)
  40. local super = {}
  41. local name = '<unnamed class>'
  42. local constructor = args or __NULL__
  43. if type(args) == "table" then
  44. -- nasty hack to check if args.inherits is a table of classes or a class or nil
  45. super = (args.inherits or {}).__is_a and {args.inherits} or args.inherits or {}
  46. name = args.name or name
  47. constructor = args[1] or __NULL__
  48. end
  49. assert(type(constructor) == "function", 'constructor has to be nil or a function')
  50. -- build class
  51. local class = {}
  52. class.__index = class
  53. class.__tostring = function() return ("<instance of %s>"):format(tostring(class)) end
  54. class.construct = constructor or __NULL__
  55. class.inherit = inherit
  56. class.__is_a = {[class] = true}
  57. class.is_a = function(self, other) return not not self.__is_a[other] end
  58. -- inherit superclasses (see above)
  59. inherit(class, unpack(super))
  60. -- syntactic sugar
  61. local meta = {
  62. __call = function(self, ...)
  63. local obj = {}
  64. setmetatable(obj, self)
  65. self.construct(obj, ...)
  66. return obj
  67. end,
  68. __tostring = function() return name end
  69. }
  70. return setmetatable(class, meta)
  71. end
  72. -- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
  73. if common_class ~= false and not common then
  74. common = {}
  75. function common.class(name, prototype, parent)
  76. local init = prototype.init or (parent or {}).init
  77. return new{name = name, inherits = {prototype, parent}, init}
  78. end
  79. function common.instance(class, ...)
  80. return class(...)
  81. end
  82. end
  83. -- the module
  84. return setmetatable({new = new, inherit = inherit},
  85. {__call = function(_,...) return new(...) end})