Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local LuaTypes = {
- ["function"] = true;
- ["string"] = true;
- ["table"] = true;
- ["number"] = true;
- ["thread"] = true;
- ["boolean"] = true;
- }
- local function resolveType(Type,Init)
- if Init then
- return Type
- end
- if type(Type) == "table" then
- if Type.__type then
- return Type.__type
- end
- end
- if LuaTypes[type(Type)] then
- return type(Type)
- end
- end
- local function checkValues(ValTable,LType)
- for k,v in pairs(ValTable) do
- if resolveType(v) ~= LType then
- return false
- end
- end
- return true
- end
- local CList = {}
- function CList:add(Value,Pos)
- if Pos then
- self.entries[Pos] = Value
- else
- table.insert(self.entries,Value)
- end
- end
- function CList:getType()
- return self.__ListType
- end
- function CList:remove(Key)
- table.remove(self.entries,Key)
- end
- function CList:length()
- return self.__ListLength
- end
- function CList:sort(CompFunc)
- local rawentries = self.__ListEntries
- table.sort(rawentries,CompFunc or nil)
- end
- function CList:concat(Sep,RStart,RStop)
- local rawentries = self.__ListEntries
- return table.concat(rawentries,Sep,RStart,RStop)
- end
- local function createList(GenType)
- local obj = {
- initialized = false;
- type = resolveType(GenType,true);
- entries = {};
- }
- setmetatable(obj,{__index = CList})
- local objProxy = {
- __type = "List"
- }
- setmetatable(objProxy,{
- __index = function(_,k)
- if k == "__ListType" then
- return obj.type
- end
- if k == "__ListLength" then
- return #obj.entries
- end
- if k == "__ListEntries" then
- return obj.entries
- end
- if not obj.initialized then
- obj.initialized = true
- end
- return obj.entries[k] or CList[k]
- end;
- __newindex = function(_,Key,Value)
- if resolveType(Value) == obj.type then
- obj:add(Value,Key)
- else
- error("Type of new value("..type(Value)..") does not match with list type("..obj.type..")")
- end
- end;
- __call = function(_,_,InitValues)
- if obj.initialized then
- error("List has already been initialized",2)
- end
- if checkValues(InitValues,obj.type) then
- obj.entries = InitValues
- else
- error("Inital values do not match List type",2)
- end
- return objProxy
- end;
- __add = function(Left,Right)
- if Left.__type == "List" then
- if resolveType(Right) == obj.type then
- obj:add(Right,nil)
- else
- error("Type of new value("..type(Right)..") does not match with list type("..obj.type..")")
- end
- else
- if resolveType(Left) == obj.type then
- obj:add(Left,nil)
- else
- error("Type of new value("..type(Left)..") does not match with list type("..obj.type..")")
- end
- end
- return objProxy
- end;
- })
- return objProxy
- end
- ListParser = setmetatable({},{
- __index = function(_,GenType)
- return createList(GenType)
- end;
- })
- List = ListParser
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement