diff engine/core/model/model.cpp @ 35:bf7f838e6684

Added methods for removing Objects from the Model.
author jwt@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 12 Jul 2008 02:50:09 +0000
parents 112fc4af772d
children 90005975cdbb
line wrap: on
line diff
--- a/engine/core/model/model.cpp	Sat Jul 12 00:20:26 2008 +0000
+++ b/engine/core/model/model.cpp	Sat Jul 12 02:50:09 2008 +0000
@@ -32,6 +32,8 @@
 #include "model/metamodel/object.h"
 #include "model/metamodel/grids/cellgrid.h"
 #include "structures/map.h"
+#include "structures/layer.h"
+#include "structures/instance.h"
 #include "util/base/exception.h"
 
 #include "model.h"
@@ -159,6 +161,62 @@
 		return object;
 	}
 
+	bool Model::deleteObject(Object* object) {
+		std::list<Layer*>::const_iterator jt;
+		std::vector<Instance*>::const_iterator kt;
+		for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
+			for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
+				for(kt = (*jt)->getInstances().begin(); kt != (*jt)->getInstances().end(); ++kt) {
+					Object* o = (*kt)->getObject();
+					while(o != 0) {
+						if(o == object)
+							return false;
+					}
+				}
+			}
+		}
+
+		std::string name_space = object->getNamespace();
+		std::list<namespace_t>::iterator nspace = m_namespaces.begin();
+		for(; nspace != m_namespaces.end(); ++nspace) {
+			if(nspace->first == name_space) break;
+		}
+
+		if(nspace == m_namespaces.end())
+			return true;
+
+		std::list<Object*>::iterator it = nspace->second.begin();
+		for(; it != nspace->second.end(); ++it) {
+			if(*it == object) {
+				delete *it;
+				nspace->second.erase(it);
+				return true;
+			}
+		}
+
+		return true;
+	}
+
+	bool Model::deleteObjects() {
+		std::list<Layer*>::const_iterator jt;
+		for(std::list<Map*>::iterator it = m_maps.begin(); it != m_maps.end(); ++it) {
+			for(jt = (*it)->getLayers().begin(); jt != (*it)->getLayers().end(); ++jt) {
+				if((*jt)->hasInstances())
+					return false;
+			}
+		}
+
+		std::list<namespace_t>::iterator nspace = m_namespaces.begin();
+		while(nspace != m_namespaces.end()) {
+			std::list<Object*>::iterator it = nspace->second.begin();
+			for(; it != nspace->second.end(); ++it) {
+				delete *it;
+			}
+			nspace = m_namespaces.erase(nspace);
+		}
+		return true;
+	}
+
 	Object* Model::getObject(const std::string& id, const std::string& name_space) {
 		std::list<namespace_t>::iterator nspace = m_namespaces.begin();
 		for(; nspace != m_namespaces.end(); ++nspace) {