class.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. table.unpack = table.unpack or unpack
  2. function clone(object, setMetaTable)
  3. local lookup_table = {}
  4. local function _copy(object)
  5. if type(object) ~= "table" then
  6. return object
  7. elseif lookup_table[object] then
  8. return lookup_table[object]
  9. end
  10. local new_table = {}
  11. lookup_table[object] = new_table
  12. for key, value in pairs(object) do
  13. new_table[_copy(key)] = _copy(value)
  14. end
  15. if not setMetaTable then
  16. return new_table
  17. end
  18. return setmetatable(new_table, getmetatable(object))
  19. end
  20. return _copy(object)
  21. end
  22. local objectCount = 0
  23. local function getObjectCount()
  24. objectCount = objectCount + 1
  25. return objectCount
  26. end
  27. local create
  28. create = function(c, obj, ...)
  29. if c.supers then
  30. for k,v in pairs(c.supers) do
  31. create(v, obj, ...)
  32. end
  33. elseif c.super then
  34. create(c.super, obj, ...)
  35. end
  36. if c.ctor then
  37. c.ctor(obj, ...)
  38. end
  39. end
  40. function class(classname, ...)
  41. local supers = {...}
  42. local super = supers[1]
  43. local superType = type(super)
  44. local cls = {}
  45. cls.__cname = classname
  46. if superType ~= "function" and superType ~= "table" then
  47. superType = nil
  48. super = nil
  49. end
  50. if superType == "function" or (super and super.__ctype == 1) then
  51. -- inherited from native C++ Object
  52. cls.__ctype = 1
  53. if superType == "table" then
  54. -- copy fields from super
  55. -- setmetatable(cls, super)
  56. for k,v in pairs(super) do cls[k] = v end
  57. cls.__create = super.__create
  58. cls.super = super
  59. else
  60. cls.__create = super
  61. end
  62. function cls:new(o, ...)
  63. local instance = cls.__create(o, ...)
  64. tolua.setpeer(instance, {})
  65. -- instance.instUUId = getObjectCount()
  66. -- copy fields from class to native object
  67. for k,v in pairs(cls) do instance[k] = v end
  68. if tag then
  69. create(cls, instance, ...)
  70. else
  71. create(cls, instance, o, ...)
  72. end
  73. return instance
  74. end
  75. else
  76. -- inherited from Lua Object
  77. cls.super = super
  78. cls.supers = supers
  79. cls.__index = cls
  80. cls.__ctype = 2 -- lua
  81. if #supers == 1 then
  82. setmetatable(cls, super)
  83. else
  84. LuaExtend(cls, table.unpack(supers))
  85. end
  86. function cls:new(o, ...)
  87. local instance = setmetatable({}, cls)
  88. create(cls, instance, o, ...)
  89. -- -- instance.instUUId = getObjectCount()
  90. -- if iskindof(instance, "LuaMono") then
  91. -- instance:InitGameObject(o)
  92. -- create(cls, instance, ...)
  93. -- else
  94. -- create(cls, instance, o, ...)
  95. -- end
  96. return instance
  97. end
  98. end
  99. function cls:getClassName()
  100. return self.__cname
  101. end
  102. return cls
  103. end
  104. function instanceOf(cls)
  105. if cls and type(cls) == 'table' and cls.__cname then
  106. return cls.__cname
  107. end
  108. return NONE_CLASS
  109. end
  110. function iskindof(obj, className)
  111. local t = type(obj)
  112. if t == "table" then
  113. -- local mt = getmetatable(obj)
  114. -- while mt and mt.__index do
  115. local mt = obj
  116. if mt.__cname == className then
  117. return true
  118. elseif mt.supers then
  119. for k,v in pairs(mt.supers) do
  120. if iskindof(v, className) then
  121. return true
  122. end
  123. end
  124. else
  125. return iskindof(obj.super, className)
  126. end
  127. return false
  128. elseif t == "userdata" then
  129. return false
  130. else
  131. return false
  132. end
  133. end
  134. function LuaExtend(src, ...)
  135. local meta = getmetatable(src)
  136. local parents = {...}
  137. setmetatable(src, {
  138. __index = function (tb, key)
  139. local v = rawget(tb, key)
  140. if not v and meta then
  141. local find = meta[key]
  142. if find then
  143. return find
  144. end
  145. end
  146. for k,v in pairs(parents) do
  147. local find = v[key]
  148. if find then
  149. return find
  150. end
  151. end
  152. end
  153. })
  154. end
  155. function addmetatable(src, super)
  156. local meta = getmetatable(src)
  157. setmetatable(src, {
  158. __index = function (tb, key)
  159. local v = rawget(tb, key)
  160. if not v and meta then
  161. local find = meta[key]
  162. if find then
  163. return find
  164. end
  165. end
  166. find = super[key]
  167. if find then
  168. return find
  169. end
  170. end
  171. })
  172. end