[790] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
[1505] | 3 | * > www.orxonox.net < |
---|
[790] | 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Fabian 'x3n' Landau |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[871] | 29 | /** |
---|
[2171] | 30 | @file |
---|
[790] | 31 | @brief Implementation of the Identifier class. |
---|
| 32 | */ |
---|
| 33 | |
---|
[1505] | 34 | #include "Identifier.h" |
---|
| 35 | |
---|
| 36 | #include <ostream> |
---|
| 37 | |
---|
[3280] | 38 | #include "util/StringUtils.h" |
---|
[9667] | 39 | #include "core/CoreIncludes.h" |
---|
[9563] | 40 | #include "core/config/ConfigValueContainer.h" |
---|
| 41 | #include "core/XMLPort.h" |
---|
| 42 | #include "core/object/ClassFactory.h" |
---|
[790] | 43 | |
---|
| 44 | namespace orxonox |
---|
| 45 | { |
---|
| 46 | // ############################### |
---|
| 47 | // ### Identifier ### |
---|
| 48 | // ############################### |
---|
| 49 | /** |
---|
| 50 | @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. |
---|
| 51 | */ |
---|
| 52 | Identifier::Identifier() |
---|
[9667] | 53 | : classID_(IdentifierManager::getInstance().getUniqueClassId()) |
---|
[790] | 54 | { |
---|
[1505] | 55 | this->factory_ = 0; |
---|
[9667] | 56 | this->bInitialized_ = false; |
---|
[5929] | 57 | this->bLoadable_ = false; |
---|
[1505] | 58 | |
---|
| 59 | this->bHasConfigValues_ = false; |
---|
[790] | 60 | |
---|
[5781] | 61 | // Default network ID is the class ID |
---|
| 62 | this->networkID_ = this->classID_; |
---|
[790] | 63 | } |
---|
| 64 | |
---|
| 65 | /** |
---|
[871] | 66 | @brief Destructor: Deletes the list containing the children. |
---|
[790] | 67 | */ |
---|
| 68 | Identifier::~Identifier() |
---|
| 69 | { |
---|
[1747] | 70 | if (this->factory_) |
---|
| 71 | delete this->factory_; |
---|
| 72 | |
---|
| 73 | for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it) |
---|
| 74 | delete (it->second); |
---|
[5781] | 75 | for (std::map<std::string, XMLPortParamContainer*>::iterator it = this->xmlportParamContainers_.begin(); it != this->xmlportParamContainers_.end(); ++it) |
---|
| 76 | delete (it->second); |
---|
| 77 | for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it) |
---|
| 78 | delete (it->second); |
---|
[790] | 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
[9667] | 82 | @brief Sets the name of the class. |
---|
[1856] | 83 | */ |
---|
[9667] | 84 | void Identifier::setName(const std::string& name) |
---|
[1856] | 85 | { |
---|
[9667] | 86 | if (name != this->name_) |
---|
[1856] | 87 | { |
---|
[9667] | 88 | this->name_ = name; |
---|
| 89 | IdentifierManager::getInstance().addIdentifierToLookupMaps(this); |
---|
[1856] | 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
[9667] | 93 | void Identifier::setFactory(Factory* factory) |
---|
[790] | 94 | { |
---|
[9667] | 95 | if (this->factory_) |
---|
| 96 | delete this->factory_; |
---|
[790] | 97 | |
---|
[9667] | 98 | this->factory_ = factory; |
---|
[790] | 99 | } |
---|
| 100 | |
---|
[1747] | 101 | |
---|
| 102 | /** |
---|
[790] | 103 | @brief Creates an object of the type the Identifier belongs to. |
---|
| 104 | @return The new object |
---|
| 105 | */ |
---|
[9667] | 106 | Identifiable* Identifier::fabricate(Context* context) |
---|
[790] | 107 | { |
---|
| 108 | if (this->factory_) |
---|
| 109 | { |
---|
[9667] | 110 | return this->factory_->fabricate(context); |
---|
[790] | 111 | } |
---|
| 112 | else |
---|
| 113 | { |
---|
[8858] | 114 | orxout(user_error) << "An error occurred in Identifier.cc:" << endl; |
---|
| 115 | orxout(user_error) << "Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << endl; |
---|
| 116 | orxout(user_error) << "Aborting..." << endl; |
---|
[790] | 117 | abort(); |
---|
[5929] | 118 | return 0; |
---|
[790] | 119 | } |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | /** |
---|
[5929] | 123 | @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map. |
---|
[5781] | 124 | */ |
---|
| 125 | void Identifier::setNetworkID(uint32_t id) |
---|
| 126 | { |
---|
| 127 | this->networkID_ = id; |
---|
[9667] | 128 | IdentifierManager::getInstance().addIdentifierToLookupMaps(this); |
---|
[5781] | 129 | } |
---|
| 130 | |
---|
| 131 | /** |
---|
[9667] | 132 | * @brief Used to define the direct parents of an Identifier of an abstract class. |
---|
| 133 | */ |
---|
| 134 | Identifier& Identifier::inheritsFrom(Identifier* directParent) |
---|
| 135 | { |
---|
| 136 | if (this->parents_.empty()) |
---|
| 137 | this->directParents_.insert(directParent); |
---|
| 138 | else |
---|
| 139 | orxout(internal_error) << "Trying to add " << directParent->getName() << " as a direct parent of " << this->getName() << " after the latter was already initialized" << endl; |
---|
| 140 | |
---|
| 141 | return *this; |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | /** |
---|
| 145 | * @brief Initializes the parents of this Identifier while creating the class hierarchy. |
---|
| 146 | * @param identifiers All identifiers that were used to create an instance of this class (including this identifier itself) |
---|
| 147 | */ |
---|
| 148 | void Identifier::initializeParents(const std::set<const Identifier*>& identifiers) |
---|
| 149 | { |
---|
| 150 | if (!IdentifierManager::getInstance().isCreatingHierarchy()) |
---|
| 151 | { |
---|
| 152 | orxout(internal_warning) << "Identifier::initializeParents() created outside of class hierarchy creation" << endl; |
---|
| 153 | return; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | for (std::set<const Identifier*>::const_iterator it = identifiers.begin(); it != identifiers.end(); ++it) |
---|
| 157 | if (*it != this) |
---|
| 158 | this->parents_.insert(*it); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * @brief Initializes the direct parents of this Identifier while creating the class hierarchy. This is only intended for abstract classes. |
---|
| 163 | */ |
---|
| 164 | void Identifier::initializeDirectParentsOfAbstractClass() |
---|
| 165 | { |
---|
| 166 | if (!IdentifierManager::getInstance().isCreatingHierarchy()) |
---|
| 167 | { |
---|
| 168 | orxout(internal_warning) << "Identifier::initializeDirectParentsOfAbstractClass() created outside of class hierarchy creation" << endl; |
---|
| 169 | return; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | // only Identifiable is allowed to have no parents (even tough it's currently not abstract) |
---|
| 173 | if (this->directParents_.empty() && !this->isExactlyA(Class(Identifiable))) |
---|
| 174 | { |
---|
| 175 | orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl; |
---|
| 176 | orxout(internal_error) << " If this class is not abstract, use RegisterClass(ThisClass);" << endl; |
---|
| 177 | orxout(internal_error) << " If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl; |
---|
| 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly. |
---|
| 183 | */ |
---|
| 184 | void Identifier::finishInitialization() |
---|
| 185 | { |
---|
| 186 | if (!IdentifierManager::getInstance().isCreatingHierarchy()) |
---|
| 187 | { |
---|
| 188 | orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl; |
---|
| 189 | return; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if (this->isInitialized()) |
---|
| 193 | return; |
---|
| 194 | |
---|
| 195 | // if no direct parents were defined, initialize them with the set of all parents |
---|
| 196 | if (this->directParents_.empty()) |
---|
| 197 | this->directParents_ = this->parents_; |
---|
| 198 | |
---|
| 199 | // initialize all parents before continuing to initialize this identifier |
---|
| 200 | for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) |
---|
| 201 | { |
---|
| 202 | Identifier* directParent = const_cast<Identifier*>(*it); |
---|
| 203 | directParent->finishInitialization(); // initialize parent |
---|
| 204 | this->parents_.insert(directParent); // direct parent is also a parent |
---|
| 205 | this->parents_.insert(directParent->parents_.begin(), directParent->parents_.end()); // parents of direct parent are also parents |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // parents of parents are no direct parents of this identifier |
---|
| 209 | for (std::set<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent) |
---|
| 210 | for (std::set<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent) |
---|
| 211 | this->directParents_.erase(*it_parent_parent); |
---|
| 212 | |
---|
| 213 | // tell all parents that this identifier is a child |
---|
| 214 | for (std::set<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it) |
---|
| 215 | const_cast<Identifier*>(*it)->children_.insert(this); |
---|
| 216 | |
---|
| 217 | // tell all direct parents that this identifier is a direct child |
---|
| 218 | for (std::set<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) |
---|
| 219 | { |
---|
| 220 | const_cast<Identifier*>(*it)->directChildren_.insert(this); |
---|
| 221 | |
---|
| 222 | // Create the super-function dependencies |
---|
| 223 | (*it)->createSuperFunctionCaller(); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | this->bInitialized_ = true; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
[871] | 230 | @brief Returns true, if the Identifier is at least of the given type. |
---|
[790] | 231 | @param identifier The identifier to compare with |
---|
| 232 | */ |
---|
| 233 | bool Identifier::isA(const Identifier* identifier) const |
---|
| 234 | { |
---|
[1059] | 235 | return (identifier == this || (this->parents_.find(identifier) != this->parents_.end())); |
---|
[790] | 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
[871] | 239 | @brief Returns true, if the Identifier is exactly of the given type. |
---|
[790] | 240 | @param identifier The identifier to compare with |
---|
| 241 | */ |
---|
[871] | 242 | bool Identifier::isExactlyA(const Identifier* identifier) const |
---|
[790] | 243 | { |
---|
| 244 | return (identifier == this); |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
[871] | 248 | @brief Returns true, if the assigned identifier is a child of the given identifier. |
---|
[790] | 249 | @param identifier The identifier to compare with |
---|
| 250 | */ |
---|
| 251 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
| 252 | { |
---|
[1060] | 253 | return (this->parents_.find(identifier) != this->parents_.end()); |
---|
[1505] | 254 | } |
---|
[790] | 255 | |
---|
| 256 | /** |
---|
[1505] | 257 | @brief Returns true, if the assigned identifier is a direct child of the given identifier. |
---|
| 258 | @param identifier The identifier to compare with |
---|
| 259 | */ |
---|
| 260 | bool Identifier::isDirectChildOf(const Identifier* identifier) const |
---|
| 261 | { |
---|
| 262 | return (this->directParents_.find(identifier) != this->directParents_.end()); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | /** |
---|
[871] | 266 | @brief Returns true, if the assigned identifier is a parent of the given identifier. |
---|
[790] | 267 | @param identifier The identifier to compare with |
---|
| 268 | */ |
---|
| 269 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
| 270 | { |
---|
[5929] | 271 | return (this->children_.find(identifier) != this->children_.end()); |
---|
[1505] | 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | @brief Returns true, if the assigned identifier is a direct parent of the given identifier. |
---|
| 276 | @param identifier The identifier to compare with |
---|
| 277 | */ |
---|
| 278 | bool Identifier::isDirectParentOf(const Identifier* identifier) const |
---|
| 279 | { |
---|
[5929] | 280 | return (this->directChildren_.find(identifier) != this->directChildren_.end()); |
---|
[1505] | 281 | } |
---|
| 282 | |
---|
| 283 | /** |
---|
| 284 | @brief Adds the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 285 | @param varname The name of the variablee |
---|
| 286 | @param container The container |
---|
| 287 | */ |
---|
| 288 | void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container) |
---|
| 289 | { |
---|
| 290 | std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname); |
---|
| 291 | if (it != this->configValues_.end()) |
---|
| 292 | { |
---|
[8858] | 293 | orxout(internal_warning) << "Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << endl; |
---|
[1747] | 294 | delete (it->second); |
---|
[1505] | 295 | } |
---|
| 296 | |
---|
| 297 | this->bHasConfigValues_ = true; |
---|
| 298 | this->configValues_[varname] = container; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | /** |
---|
| 302 | @brief Returns the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 303 | @param varname The name of the variable |
---|
| 304 | @return The ConfigValueContainer |
---|
| 305 | */ |
---|
| 306 | ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname) |
---|
| 307 | { |
---|
| 308 | std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); |
---|
| 309 | if (it != configValues_.end()) |
---|
[5929] | 310 | return it->second; |
---|
[1505] | 311 | else |
---|
| 312 | return 0; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | /** |
---|
[5781] | 316 | @brief Returns a XMLPortParamContainer that loads a parameter of this class. |
---|
| 317 | @param paramname The name of the parameter |
---|
| 318 | @return The container |
---|
| 319 | */ |
---|
| 320 | XMLPortParamContainer* Identifier::getXMLPortParamContainer(const std::string& paramname) |
---|
| 321 | { |
---|
| 322 | std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname); |
---|
| 323 | if (it != this->xmlportParamContainers_.end()) |
---|
[5929] | 324 | return it->second; |
---|
[5781] | 325 | else |
---|
| 326 | return 0; |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
| 330 | @brief Adds a new XMLPortParamContainer that loads a parameter of this class. |
---|
| 331 | @param paramname The name of the parameter |
---|
| 332 | @param container The container |
---|
| 333 | */ |
---|
| 334 | void Identifier::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) |
---|
| 335 | { |
---|
| 336 | std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname); |
---|
| 337 | if (it != this->xmlportParamContainers_.end()) |
---|
| 338 | { |
---|
[8858] | 339 | orxout(internal_warning) << "Overwriting XMLPortParamContainer in class " << this->getName() << '.' << endl; |
---|
[5781] | 340 | delete (it->second); |
---|
| 341 | } |
---|
| 342 | |
---|
| 343 | this->xmlportParamContainers_[paramname] = container; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | /** |
---|
| 347 | @brief Returns a XMLPortObjectContainer that attaches an object to this class. |
---|
| 348 | @param sectionname The name of the section that contains the attachable objects |
---|
| 349 | @return The container |
---|
| 350 | */ |
---|
| 351 | XMLPortObjectContainer* Identifier::getXMLPortObjectContainer(const std::string& sectionname) |
---|
| 352 | { |
---|
| 353 | std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname); |
---|
| 354 | if (it != this->xmlportObjectContainers_.end()) |
---|
[5929] | 355 | return it->second; |
---|
[5781] | 356 | else |
---|
| 357 | return 0; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | /** |
---|
| 361 | @brief Adds a new XMLPortObjectContainer that attaches an object to this class. |
---|
| 362 | @param sectionname The name of the section that contains the attachable objects |
---|
| 363 | @param container The container |
---|
| 364 | */ |
---|
| 365 | void Identifier::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) |
---|
| 366 | { |
---|
| 367 | std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname); |
---|
| 368 | if (it != this->xmlportObjectContainers_.end()) |
---|
| 369 | { |
---|
[8858] | 370 | orxout(internal_warning) << "Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << endl; |
---|
[5781] | 371 | delete (it->second); |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | this->xmlportObjectContainers_[sectionname] = container; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | /** |
---|
[1505] | 378 | @brief Lists the names of all Identifiers in a std::set<const Identifier*>. |
---|
| 379 | @param out The outstream |
---|
| 380 | @param list The list (or set) of Identifiers |
---|
| 381 | @return The outstream |
---|
| 382 | */ |
---|
| 383 | std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list) |
---|
| 384 | { |
---|
| 385 | for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
[5929] | 386 | { |
---|
| 387 | if (it != list.begin()) |
---|
[6417] | 388 | out << ' '; |
---|
[5929] | 389 | out << (*it)->getName(); |
---|
| 390 | } |
---|
[1505] | 391 | |
---|
| 392 | return out; |
---|
| 393 | } |
---|
[790] | 394 | } |
---|