Posted 27 June 2015 - 11:12 PM
TypeTable
Well I was bored so I tried to make tables that only accept values of a certain type and have a limited size .
This is basically what I was able to produce :
The relevant functions are
typeTable() simply returns a table that only allows values of the type given as the parameter ( the type can be a string or a table of strings ) and the specified size.
You can add new types through addType() and delete the whole table with deleteTable()
This is basically what I was able to produce :
Spoiler
local tTables={}
local validTypes={"nil","boolean", "number", "string", "function", "thread","table"}
local function UID()
--print(#tTables)
return #tTables
end
local function contains(table, element)
if type(table)~="table" then error("Expected table,element,got "..type(table)..","..type(element),0)
else
for _, value in pairs(table) do
if value == element then
return true
end
end
return false
end
end
local function isType(tp)
if contains(validTypes,tp) then
return true
else
return false
end
end
local function getLength(tbl)
local count=0
for k,v in pairs(tbl) do
if k~="__id" and k~="__types" then
count=count+1
end
end
return count
end
function typedTable( sType, nSize )
if type(sType)=="string" then
if(sType and isType(sType) and (nSize and type(nSize)=="number")) then
tTables[#tTables]={}
tTables[#tTables].__id=UID()
tTables[#tTables].__types={sType}
end
elseif type(sType)=="table" then
tTables[#tTables]={}
tTables[#tTables].__id=UID()
tTables[#tTables].__types={}
for _,tp in pairs(sType) do
if(tp and isType(tp) and (nSize and type(nSize)=="number")) then
table.insert(tTables[#tTables].__types,tp)
else
tTables[#tTables]=nil
error("wrong type paramter")
end
end
end
local proxy=setmetatable({},{__index=tTables[#tTables],__newindex=function(t,k,v) if (contains(tTables[#tTables].__types,type(v)) and getLength(tTables[t.__id])+1<=nSize) then tTables[t.__id][k]=v else error("Attempt to access typed table with wrong type ("..type(v)..") or table size limit reached ("..getLength(tTables[t.__id])..")" )end end })
return proxy
end
function deleteTable(pTbl)
tTables[pTbl.__id]=nil
end
function addType(tbl,sType)
if isType(sType) then
getmetatable(tbl).__index.__types[#getmetatable(tbl).__index.__types+1]=sType
end
end
--local t=typedTable({"number","function"},5)
--
-- -- t.test=1
-- -- t.w=2
-- -- t.z=3
-- -- t.u=4
-- t.pp=45
-- addType(t,"string")
-- t.pp=function() print "test" end
-- t.pp="hfdsjiak"
-- t.pp="he"
The relevant functions are
typedTable( sType, nSize)
addType(tbl,sType)
deleteTable(pTbl)
.typeTable() simply returns a table that only allows values of the type given as the parameter ( the type can be a string or a table of strings ) and the specified size.
You can add new types through addType() and delete the whole table with deleteTable()
Pastebin:
It´s no masterpiece and it didn´t take too long to code I was simply bored :D/>
Maybe it´s useful for someone :)/>
Kouksi44
http://pastebin.com/42zgHJHh
It´s no masterpiece and it didn´t take too long to code I was simply bored :D/>
Maybe it´s useful for someone :)/>
Kouksi44