Advertisement
wackeyhd5cool

Lua OOP Class Module

Mar 25th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | None | 0 0
  1. local Module_classExtension = {}
  2.  
  3. function Module_classExtension.addParentClass(Class_targetClass,Class_newClass)
  4.  
  5.     table.insert(getmetatable(Class_targetClass).__index._proxy,Class_newClass)
  6.    
  7.     --[[
  8.     insert Class_newClass into the table with _proxy in it. _proxy contains the parent classes
  9.     ]]
  10.    
  11. end
  12.  
  13. function Module_classExtension.removeParentClass(Class_targetClass,Class_classToRemove)
  14.  
  15.     local Tab_proxyTable = getmetatable(Class_targetClass).__index._proxy
  16.     for Int_giPosition,Class_parentClass in pairs(Tab_proxyTable) do
  17.         if Class_parentClass == Class_classToRemove then
  18.             table.remove(Tab_proxyTable,Int_giPosition)
  19.         end
  20.     end
  21.    
  22.     --[[
  23.     get the _proxy table
  24.     iterate through the proxy table, and
  25.     if the class in the proxy table is the class we need to remove then
  26.     remove it
  27.     ]]
  28.    
  29. end
  30.  
  31. function Module_classExtension.findMethodFromParentClasses(Tab_proxyTable,Func_method)
  32.    
  33.     for Int_giIndex,Class_gvClass in pairs(Tab_proxyTable._proxy) do
  34.         if Class_gvClass[Func_method] then
  35.             return Class_gvClass[Func_method]
  36.         end
  37.     end
  38.    
  39.     --[[
  40.     iterate through _proxy in Tab_proxyTable,
  41.     if one of the classes has the method then
  42.     return the method
  43.     ]]
  44.    
  45. end
  46.  
  47. function Module_classExtension.getParentClasses(Class_targetClass)
  48.        
  49.     return getmetatable(Class_targetClass).__index._proxy
  50.    
  51.     --[[
  52.     return the the classes in _proxy
  53.     ]]
  54.        
  55. end
  56.  
  57. function Module_classExtension.createMultipleInheritance(Class_baseClass)
  58.  
  59.     setmetatable(Class_baseClass,{__index = setmetatable({_proxy = {}},{__index = Module_classExtension.findMethodFromParentClasses})})
  60.    
  61.     --[[
  62.     this allows multiple inheritance
  63.     the structure is:
  64.     Class ==_index==> {_proxy = {parent1,parent2,...}} ==_index==> function findMethodFromParentClasses
  65.     ]]
  66.    
  67. end
  68.  
  69.  
  70.  
  71. --================================================================================================================
  72. --================================================================================================================
  73.  
  74. local Class_drawing = {}
  75.  
  76. function Class_drawing:newShape(String_name)
  77.     return {["String_shape"] = String_name}
  78. end
  79.  
  80. function Class_drawing:drawShape()
  81.     print(self["String_shape"])
  82. end
  83.  
  84. --================================================================================================================
  85. --================================================================================================================
  86.  
  87. local Class_quadrilateral = {}
  88. Module_classExtension.createMultipleInheritance(Class_quadrilateral)
  89. Module_classExtension.addParentClass(Class_quadrilateral,Class_drawing)
  90.  
  91. function Class_quadrilateral:someMethod()
  92.     print("HI")
  93. end
  94.  
  95.  
  96. --================================================================================================================
  97. --================================================================================================================
  98.  
  99. local Class_square = {}
  100. Module_classExtension.createMultipleInheritance(Class_square)
  101. Module_classExtension.addParentClass(Class_square,Class_quadrilateral)
  102.  
  103. function Class_square:newSquare()
  104.     return setmetatable(self:newShape("Square"),{__index = self})
  105. end
  106.  
  107.  
  108. --================================================================================================================
  109. --================================================================================================================
  110.  
  111. local Square_newSquare = Class_square:newSquare()
  112. Square_newSquare:drawShape()
  113. Square_newSquare:someMethod()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement