containertables.inc.lua

Sat, 27 Mar 2010 17:43:08 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Sat, 27 Mar 2010 17:43:08 +0000
changeset 0
cc66ad6b0d75
permissions
-rwxr-xr-x

Initial commit (importing from old SVN repo which got lost)


--[[ I created this class for an easy way of managing the objects inside a container in the MOO. ]]

containertable = { };

function containertable:add(object)
	local subt = self[(object and object._properties.name) or ""];
	if not subt then subt = { };
		self[(object and object._properties.name) or ""] = subt; 
		-- Also, all object's aliases need to point to subt
		if object._tags then
			for i, tag in ipairs(object._tags) do
				self[tag] = subt;
			end
		end
	end
	table.insert(subt, object);
	return object;
end

function containertable:remove(object)
	local subt = self[(object._properties and object._properties.name) or ""];
	if not subt then return false; end
	for n, stored_object in pairs(subt) do
		if object == stored_object then table.remove(subt, n); return object; end
	end
	return false;
end

function containertable:addalias(name, tag)
	self[tag] = self[name];
end

function containertable:findobject(name)
	local subt = self[name or ""];
	if not subt then subt = self[""]; if not subt then return; end end
	return subt[1];
end

function containertable:getallobjects()
	all = {};
	for tag, list in pairs(self) do
		for n, object in ipairs(list) do
			all[object] = true;
		end
	end
	return all; -- We return a table, where the objects are the keys, and the values are true
end

function createcontainertable(t)
	return setmetatable(t or {}, { __index = containertable });
end

mercurial