09-08-2012, 03:51 AM
Hello, all! I'm new here. I decided it would benefit the community if I shared programming/scripting knowledge, and thus, this is now open source.
This is a simple OOP library I made, called weaverOOP. It's unfinished, but the basic code is here.
[lua]wO={}
wO.global={}
wO.global.clone=function(self) local cloneObj={} for i,v in pairs(self) do cloneObj[i]=v end return cloneObj end
wO.global.isA=function(self, arg)
if self.className==arg then
return true
elseif self.className~="base" then
if wO.classes[self.extends]:isA(arg) then
return true
end
else
return false
end
end
wO.global.className="base"
wO.classes={base=wO.global}
function wO.newClass(a, ...)
arg={...}
b=(arg[1] or {})
b.className=a
b.name=a.."Obj"
if #arg==2 then
b.mt={__index=wO.classes[arg[2]]}
b.extends=arg[2]
else
b.mt={__index=wO.classes["base"]}
b.extends="base"
end
b.mt.__tostring=function(a) return "Class "..a end
b.mt.__call=function(self) return wO.newObj(self.className) end
setmetatable(b, b.mt)
b.mt=nil
wO.classes[a]=b
end
function wO.newObj(a, ...)
arg={...}
local o=(arg[1] or {})
local mt={__index=wO.classes[a]}
mt.__tostring=function(a) return "Object "..o.name..", a "..o.className end
setmetatable(o, mt)
return o
end
setmetatable(_G,{__index=function(_,a) return (a=="newClass" and wO.newClass or (a=="newObj" and wO.newObj or (wO.classes[a] or nil))) end}) --Ugly piece of code.
--Example begins here.
newClass("class",{x=0,y=0})
local t=class()
print(class.x,class.y)
newClass("class2",{z=15},"class")
local t=class2()
print(t.x,t.y,t.z)
[/lua]
API for those to lazy to read my 46 line code:
wO.newClass(string className,table class,[string extends])
creates a class called "classNameHere" where it's simply that table. It'll inherit all the things from the class called extends. (With no extends argument, it uses the global class)
wO.newObj(string className,[table objectValues])
Creates an object of className, with the values of objectValues. (Defaults to {} if not supplied) (deprecated as of 0.1.2)
className([table objectValues])
Equivalent to wO.newObj("className",[table objectValues]). Make sure the metatable of _G has not been changed by another script, this will error it.
Global methods and properties:
obj:isA(string className)
Checks if object obj is or extends an object of className.
obj:clone()
Returns an exact copy of object obj.
obj.name
Specific name of an object, not of much use as of now.
obj.className
The class name of an object, used in global functions and various other items.
This is a simple OOP library I made, called weaverOOP. It's unfinished, but the basic code is here.
[lua]wO={}
wO.global={}
wO.global.clone=function(self) local cloneObj={} for i,v in pairs(self) do cloneObj[i]=v end return cloneObj end
wO.global.isA=function(self, arg)
if self.className==arg then
return true
elseif self.className~="base" then
if wO.classes[self.extends]:isA(arg) then
return true
end
else
return false
end
end
wO.global.className="base"
wO.classes={base=wO.global}
function wO.newClass(a, ...)
arg={...}
b=(arg[1] or {})
b.className=a
b.name=a.."Obj"
if #arg==2 then
b.mt={__index=wO.classes[arg[2]]}
b.extends=arg[2]
else
b.mt={__index=wO.classes["base"]}
b.extends="base"
end
b.mt.__tostring=function(a) return "Class "..a end
b.mt.__call=function(self) return wO.newObj(self.className) end
setmetatable(b, b.mt)
b.mt=nil
wO.classes[a]=b
end
function wO.newObj(a, ...)
arg={...}
local o=(arg[1] or {})
local mt={__index=wO.classes[a]}
mt.__tostring=function(a) return "Object "..o.name..", a "..o.className end
setmetatable(o, mt)
return o
end
setmetatable(_G,{__index=function(_,a) return (a=="newClass" and wO.newClass or (a=="newObj" and wO.newObj or (wO.classes[a] or nil))) end}) --Ugly piece of code.
--Example begins here.
newClass("class",{x=0,y=0})
local t=class()
print(class.x,class.y)
newClass("class2",{z=15},"class")
local t=class2()
print(t.x,t.y,t.z)
[/lua]
API for those to lazy to read my 46 line code:
wO.newClass(string className,table class,[string extends])
creates a class called "classNameHere" where it's simply that table. It'll inherit all the things from the class called extends. (With no extends argument, it uses the global class)
wO.newObj(string className,[table objectValues])
Creates an object of className, with the values of objectValues. (Defaults to {} if not supplied) (deprecated as of 0.1.2)
className([table objectValues])
Equivalent to wO.newObj("className",[table objectValues]). Make sure the metatable of _G has not been changed by another script, this will error it.
Global methods and properties:
obj:isA(string className)
Checks if object obj is or extends an object of className.
obj:clone()
Returns an exact copy of object obj.
obj.name
Specific name of an object, not of much use as of now.
obj.className
The class name of an object, used in global functions and various other items.