源战役客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 rivejä
3.6 KiB

4 viikkoa sitten
  1. --每创建一个新类 就递增1 保存每个类创建的顺序id
  2. _in_ctype_count = _in_ctype_count or 0
  3. --通过类的创建顺序id来保存每一个类
  4. _in_ctype_map = _in_ctype_map or {}
  5. --每创建一个新的对象 就递增1 在New中调用 保存每个对象创建的顺序id
  6. _in_obj_ins_id = _in_obj_ins_id or 0
  7. --保存每个类对应实例化的n个对象
  8. _in_obj_ins_map = _in_obj_ins_map or {}
  9. --保存每个类实例化对象的总个数
  10. _in_obj_count_map = _in_obj_count_map or {}
  11. --保存每个类作为不一样类的父类的个数
  12. _be_super_count_map = _be_super_count_map or {}
  13. local _in_ctype_count = _in_ctype_count
  14. local _in_ctype_map = _in_ctype_map
  15. local _in_obj_ins_map = _in_obj_ins_map
  16. local _in_obj_count_map = _in_obj_count_map
  17. local _be_super_count_map = _be_super_count_map
  18. local setmetatable = setmetatable
  19. local debug_getinfo = debug.getinfo
  20. --先调用基类的init函数,依次往上层次调用派生类
  21. local function createFunc(class, obj, ...)
  22. if class.super then
  23. createFunc(class.super, obj, ...)
  24. end
  25. if class.__init then
  26. class.__init(obj, ...)
  27. end
  28. end
  29. --先调用本身的deleteme函数,再依次往下调用基类的
  30. local function deleteMeFunc(self)
  31. if self._use_delete_method then
  32. return
  33. end
  34. self._use_delete_method = true
  35. local now_super = self._class_type
  36. while now_super ~= nil do
  37. if now_super.__delete then
  38. now_super.__delete(self)
  39. end
  40. now_super = now_super.super
  41. end
  42. --清理该类所有的资源引用计数
  43. lua_resM:clearReference(self)
  44. end
  45. function BaseClass(super, use_class_type)
  46. local class_type =
  47. {
  48. __init = false,
  49. __delete = false,
  50. New = false,
  51. _source = false,
  52. __index = false,
  53. super = false,
  54. }
  55. _in_ctype_count = _in_ctype_count + 1
  56. _in_ctype_map[_in_ctype_count] = class_type
  57. local cls_obj_ins_map = {}
  58. _in_obj_ins_map[class_type] = cls_obj_ins_map
  59. setmetatable(cls_obj_ins_map, {__mode = "v"})
  60. _in_obj_count_map[class_type] = 0
  61. local info = debug_getinfo(2, "Sl")
  62. class_type._source = info.source
  63. class_type.super = super
  64. if _in_ctype_count == 1 then --设置为弱引用 只需设置一次
  65. setmetatable(_in_ctype_map, {__mode = "v"})
  66. setmetatable(_in_obj_ins_map, {__mode = "k"})
  67. setmetatable(_in_obj_count_map, {__mode = "k"})
  68. setmetatable(_be_super_count_map, {__mode = "k"})
  69. end
  70. if super then --如果有引用父类 则该对象递增1
  71. if _be_super_count_map[super] == nil then
  72. _be_super_count_map[super] = 0
  73. end
  74. _be_super_count_map[super] = _be_super_count_map[super] + 1
  75. end
  76. class_type.New = function(...)
  77. local obj = nil
  78. if not use_class_type then
  79. _in_obj_ins_id = _in_obj_ins_id + 1
  80. if class_type.__defineVar then --一次性生成该对象所要的属性 减少消耗
  81. obj = class_type:__defineVar()
  82. else
  83. obj =
  84. {
  85. _class_type = class_type,
  86. _iid = _in_obj_ins_id,
  87. DeleteMe = nil,
  88. _use_delete_method = false
  89. }
  90. end
  91. local function newFunc(t, k)
  92. local ret = class_type[k]
  93. obj[k] = ret
  94. return ret
  95. end
  96. setmetatable(obj, {__index = newFunc})
  97. else
  98. obj = class_type
  99. obj._class_type = class_type
  100. end
  101. cls_obj_ins_map[_in_obj_ins_id] = obj --save here for mem debug
  102. _in_obj_count_map[class_type] = _in_obj_count_map[class_type] + 1
  103. createFunc(class_type, obj, ...)
  104. obj.DeleteMe = deleteMeFunc
  105. return obj
  106. end
  107. --如果该类中没有的方法 则通过元表来调用父类的该方法
  108. if super then
  109. local function superFunc(t, k)
  110. local ret = super[k]
  111. class_type[k] = ret
  112. return ret
  113. end
  114. setmetatable(class_type, {__index = superFunc })
  115. end
  116. return class_type
  117. end