comparison engine/core/model/model.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children 112fc4af772d
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23
24 // 3rd party library includes
25
26 // FIFE includes
27 // These includes are split up in two parts, separated by one empty line
28 // First block: files included from the FIFE root src directory
29 // Second block: files included from the same folder
30 #include "util/structures/purge.h"
31 #include "model/metamodel/abstractpather.h"
32 #include "model/metamodel/object.h"
33 #include "structures/map.h"
34 #include "util/base/exception.h"
35
36 #include "model.h"
37
38 namespace FIFE {
39
40 Model::Model():
41 FifeClass(),
42 m_timeprovider(NULL) {
43 }
44
45 Model::~Model() {
46 purge(m_maps);
47 for(std::list<namespace_t>::iterator nspace = m_namespaces.begin(); nspace != m_namespaces.end(); ++nspace)
48 purge(nspace->second);
49 purge(m_pathers);
50 }
51
52 Map* Model::createMap(const std::string& identifier) {
53 std::list<Map*>::const_iterator it = m_maps.begin();
54 for(; it != m_maps.end(); ++it) {
55 if(identifier == (*it)->getId())
56 throw NameClash(identifier);
57 }
58
59 Map* map = new Map(identifier, &m_timeprovider);
60 m_maps.push_back(map);
61 return map;
62 }
63
64 void Model::adoptPather(AbstractPather* pather) {
65 m_pathers.push_back(pather);
66 }
67
68 AbstractPather* Model::getPather(const std::string& pathername) {
69 std::vector<AbstractPather*>::const_iterator it = m_pathers.begin();
70 for(; it != m_pathers.end(); ++it) {
71 if ((*it)->getName() == pathername) {
72 return *it;
73 }
74 }
75 return NULL;
76 }
77
78 Map* Model::getMap(const std::string& identifier) const {
79 std::list<Map*>::const_iterator it = m_maps.begin();
80 for(; it != m_maps.end(); ++it) {
81 if((*it)->getId() == identifier)
82 return *it;
83 }
84
85 throw NotFound(std::string("Tried to get non-existant map: ") + identifier + ".");
86 }
87
88 void Model::deleteMap(Map* map) {
89 std::list<Map*>::iterator it = m_maps.begin();
90 for(; it != m_maps.end(); ++it) {
91 if(*it == map) {
92 delete *it;
93 m_maps.erase(it);
94 return ;
95 }
96 }
97 }
98
99 size_t Model::getNumMaps() const {
100 return m_maps.size();
101 }
102
103 void Model::deleteMaps() {
104 purge(m_maps);
105 m_maps.clear();
106 }
107
108 std::list<std::string> Model::getNamespaces() const {
109 std::list<std::string> lst;
110 std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
111 for(; nspace != m_namespaces.end(); ++nspace) {
112 lst.push_back(nspace->first);
113 }
114
115 return lst;
116 }
117
118 Object* Model::createObject(const std::string& identifier, const std::string& name_space, Object* parent) {
119
120 std::list<namespace_t>::iterator nspace = m_namespaces.begin();
121 for(; nspace != m_namespaces.end(); ++nspace) {
122 if(nspace->first == name_space) break;
123 }
124
125 if(nspace == m_namespaces.end()) {
126 m_namespaces.push_back(namespace_t(name_space,std::list<Object*>()));
127 nspace = m_namespaces.end();
128 --nspace;
129 }
130
131 std::list<Object*>::const_iterator it = nspace->second.begin();
132 for(; it != nspace->second.end(); ++it) {
133 if(identifier == (*it)->getId())
134 throw NameClash(identifier);
135 }
136
137 Object* object = new Object(identifier, name_space, parent);
138 nspace->second.push_back(object);
139 return object;
140 }
141
142 Object* Model::getObject(const std::string& id, const std::string& name_space) {
143 std::list<namespace_t>::iterator nspace = m_namespaces.begin();
144 for(; nspace != m_namespaces.end(); ++nspace) {
145 if(nspace->first == name_space) {
146 std::list<Object*>::iterator obj = nspace->second.begin();
147 for(; obj != nspace->second.end(); ++obj)
148 if((*obj)->getId() == id) return *obj;
149 }
150 }
151 for(; nspace != m_namespaces.end(); ++nspace) {
152 std::list<Object*>::iterator obj = nspace->second.begin();
153 for(; obj != nspace->second.end(); ++obj)
154 if((*obj)->getId() == id) return *obj;
155 }
156
157 return 0;
158 }
159
160 const std::list<Object*>& Model::getObjects(const std::string& name_space) const {
161 std::list<namespace_t>::const_iterator nspace = m_namespaces.begin();
162 for(; nspace != m_namespaces.end(); ++nspace) {
163 if(nspace->first == name_space) return nspace->second;
164 }
165
166 throw NotFound(name_space);
167 }
168
169 void Model::update() {
170 std::list<Map*>::iterator it = m_maps.begin();
171 for(; it != m_maps.end(); ++it) {
172 (*it)->update();
173 }
174 std::vector<AbstractPather*>::iterator jt = m_pathers.begin();
175 for(; jt != m_pathers.end(); ++jt) {
176 (*jt)->update();
177 }
178 }
179
180 } //FIFE
181