# HG changeset patch # User jwt@33b003aa-7bff-0310-803a-e67f0ece8222 # Date 1215831009 0 # Node ID bf7f838e66843e42618cad127be5b51838078eda # Parent d66538926e781b83882cf7f3b890a59510187a5c Added methods for removing Objects from the Model. diff -r d66538926e78 -r bf7f838e6684 doc/dependencies/filedeps.dot --- a/doc/dependencies/filedeps.dot Sat Jul 12 00:20:26 2008 +0000 +++ b/doc/dependencies/filedeps.dot Sat Jul 12 02:50:09 2008 +0000 @@ -235,6 +235,8 @@ "engine/core/model/model.cpp" -> "model/metamodel/abstractpather.h" "engine/core/model/model.cpp" -> "model/metamodel/grids/cellgrid.h" "engine/core/model/model.cpp" -> "model/metamodel/object.h" + "engine/core/model/model.cpp" -> "structures/instance.h" + "engine/core/model/model.cpp" -> "structures/layer.h" "engine/core/model/model.cpp" -> "structures/map.h" "engine/core/model/model.cpp" -> "util/base/exception.h" "engine/core/model/model.cpp" -> "util/structures/purge.h" diff -r d66538926e78 -r bf7f838e6684 engine/core/model/model.cpp --- 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::const_iterator jt; + std::vector::const_iterator kt; + for(std::list::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::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::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::const_iterator jt; + for(std::list::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::iterator nspace = m_namespaces.begin(); + while(nspace != m_namespaces.end()) { + std::list::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::iterator nspace = m_namespaces.begin(); for(; nspace != m_namespaces.end(); ++nspace) { diff -r d66538926e78 -r bf7f838e6684 engine/core/model/model.h --- a/engine/core/model/model.h Sat Jul 12 00:20:26 2008 +0000 +++ b/engine/core/model/model.h Sat Jul 12 02:50:09 2008 +0000 @@ -97,6 +97,16 @@ */ Object* createObject(const std::string& identifier, const std::string& name_space, Object* parent=0); + /** Attempt to remove an object from the model + * Fails and returns false if the object is referenced by an instance. + */ + bool deleteObject(Object*); + + /** Attempt to remove all objects from the model + * Fails and returns false if any maps with instances are present. + */ + bool deleteObjects(); + /** Get an object by its id. If no namespace is specified, the namespaces are searched in order * and the first matching object is returned. Returns 0 if object is not found. */ diff -r d66538926e78 -r bf7f838e6684 engine/core/model/model.i --- a/engine/core/model/model.i Sat Jul 12 00:20:26 2008 +0000 +++ b/engine/core/model/model.i Sat Jul 12 02:50:09 2008 +0000 @@ -33,6 +33,8 @@ std::list getNamespaces() const; Object* createObject(const std::string& identifier, const std::string& name_space, Object* parent=0); + bool deleteObject(Object*); + bool deleteObjects(); Object* getObject(const std::string& id, const std::string& name_space); const std::list& getObjects(const std::string& name_space) const; diff -r d66538926e78 -r bf7f838e6684 tests/swig_tests/model_tests.py --- a/tests/swig_tests/model_tests.py Sat Jul 12 00:20:26 2008 +0000 +++ b/tests/swig_tests/model_tests.py Sat Jul 12 02:50:09 2008 +0000 @@ -89,7 +89,26 @@ #p2 = inst.getLocation().getLayerCoordinates() #print p2.x, p2.y #self.assertEqual(inst.getLocation().getLayerCoordinates(), fife.ModelCoordinate(4,4)) - + + def testObjects(self): + obj1 = self.model.createObject("object003","test_nspace") + obj2 = self.model.createObject("object004","test_nspace") + self.model.createObject("object005","test_nspace") + self.model.createObject("object006","test_nspace") + + self.assertEqual(self.model.deleteObject(obj2),True) + + map = self.model.createMap("map007") + grid = fife.SquareGrid() + layer = map.createLayer("layer004",grid) + + inst = layer.createInstance(obj1, fife.ModelCoordinate(4,4)) + self.assertEqual(self.model.deleteObject(obj1),False) + self.assertEqual(self.model.deleteObjects(),False) + layer.deleteInstance(inst) + self.assertEqual(self.model.deleteObject(obj1),True) + self.assertEqual(self.model.deleteObjects(),True) + class TestActionAngles(unittest.TestCase): def setUp(self): self.runaction = fife.Action("action001")