[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; |
---|
[10374] | 58 | this->bIsVirtualBase_ = false; |
---|
[1505] | 59 | |
---|
| 60 | this->bHasConfigValues_ = false; |
---|
[790] | 61 | |
---|
[5781] | 62 | // Default network ID is the class ID |
---|
| 63 | this->networkID_ = this->classID_; |
---|
[790] | 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
[871] | 67 | @brief Destructor: Deletes the list containing the children. |
---|
[790] | 68 | */ |
---|
| 69 | Identifier::~Identifier() |
---|
| 70 | { |
---|
[1747] | 71 | if (this->factory_) |
---|
| 72 | delete this->factory_; |
---|
| 73 | |
---|
| 74 | for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it) |
---|
| 75 | delete (it->second); |
---|
[5781] | 76 | for (std::map<std::string, XMLPortParamContainer*>::iterator it = this->xmlportParamContainers_.begin(); it != this->xmlportParamContainers_.end(); ++it) |
---|
| 77 | delete (it->second); |
---|
| 78 | for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it) |
---|
| 79 | delete (it->second); |
---|
[790] | 80 | } |
---|
| 81 | |
---|
| 82 | /** |
---|
[9667] | 83 | @brief Sets the name of the class. |
---|
[1856] | 84 | */ |
---|
[9667] | 85 | void Identifier::setName(const std::string& name) |
---|
[1856] | 86 | { |
---|
[9667] | 87 | if (name != this->name_) |
---|
[1856] | 88 | { |
---|
[9667] | 89 | this->name_ = name; |
---|
| 90 | IdentifierManager::getInstance().addIdentifierToLookupMaps(this); |
---|
[1856] | 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
[9667] | 94 | void Identifier::setFactory(Factory* factory) |
---|
[790] | 95 | { |
---|
[9667] | 96 | if (this->factory_) |
---|
| 97 | delete this->factory_; |
---|
[790] | 98 | |
---|
[9667] | 99 | this->factory_ = factory; |
---|
[790] | 100 | } |
---|
| 101 | |
---|
[1747] | 102 | |
---|
| 103 | /** |
---|
[790] | 104 | @brief Creates an object of the type the Identifier belongs to. |
---|
| 105 | @return The new object |
---|
| 106 | */ |
---|
[9667] | 107 | Identifiable* Identifier::fabricate(Context* context) |
---|
[790] | 108 | { |
---|
| 109 | if (this->factory_) |
---|
| 110 | { |
---|
[9667] | 111 | return this->factory_->fabricate(context); |
---|
[790] | 112 | } |
---|
| 113 | else |
---|
| 114 | { |
---|
[8858] | 115 | orxout(user_error) << "An error occurred in Identifier.cc:" << endl; |
---|
| 116 | orxout(user_error) << "Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << endl; |
---|
| 117 | orxout(user_error) << "Aborting..." << endl; |
---|
[790] | 118 | abort(); |
---|
[5929] | 119 | return 0; |
---|
[790] | 120 | } |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
[5929] | 124 | @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map. |
---|
[5781] | 125 | */ |
---|
| 126 | void Identifier::setNetworkID(uint32_t id) |
---|
| 127 | { |
---|
| 128 | this->networkID_ = id; |
---|
[9667] | 129 | IdentifierManager::getInstance().addIdentifierToLookupMaps(this); |
---|
[5781] | 130 | } |
---|
| 131 | |
---|
| 132 | /** |
---|
[9667] | 133 | * @brief Used to define the direct parents of an Identifier of an abstract class. |
---|
| 134 | */ |
---|
| 135 | Identifier& Identifier::inheritsFrom(Identifier* directParent) |
---|
| 136 | { |
---|
| 137 | if (this->parents_.empty()) |
---|
[10372] | 138 | this->directParents_.push_back(directParent); |
---|
[9667] | 139 | else |
---|
| 140 | orxout(internal_error) << "Trying to add " << directParent->getName() << " as a direct parent of " << this->getName() << " after the latter was already initialized" << endl; |
---|
| 141 | |
---|
| 142 | return *this; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | /** |
---|
| 146 | * @brief Initializes the parents of this Identifier while creating the class hierarchy. |
---|
[10366] | 147 | * @param initializationTrace All identifiers that were recorded while creating an instance of this class (including nested classes and this identifier itself) |
---|
[9667] | 148 | */ |
---|
[10372] | 149 | void Identifier::initializeParents(const std::list<const Identifier*>& initializationTrace) |
---|
[9667] | 150 | { |
---|
| 151 | if (!IdentifierManager::getInstance().isCreatingHierarchy()) |
---|
| 152 | { |
---|
| 153 | orxout(internal_warning) << "Identifier::initializeParents() created outside of class hierarchy creation" << endl; |
---|
| 154 | return; |
---|
| 155 | } |
---|
| 156 | |
---|
[10371] | 157 | if (this->directParents_.empty()) |
---|
[9667] | 158 | { |
---|
[10372] | 159 | for (std::list<const Identifier*>::const_iterator it = initializationTrace.begin(); it != initializationTrace.end(); ++it) |
---|
[10371] | 160 | if (*it != this) |
---|
[10372] | 161 | this->parents_.push_back(*it); |
---|
[9667] | 162 | } |
---|
[10371] | 163 | else |
---|
| 164 | orxout(internal_error) << "Trying to add parents to " << this->getName() << " after it was already initialized with manual calls to inheritsFrom<Class>()." << endl; |
---|
[9667] | 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly. |
---|
| 169 | */ |
---|
| 170 | void Identifier::finishInitialization() |
---|
| 171 | { |
---|
| 172 | if (!IdentifierManager::getInstance().isCreatingHierarchy()) |
---|
| 173 | { |
---|
| 174 | orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl; |
---|
| 175 | return; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | if (this->isInitialized()) |
---|
| 179 | return; |
---|
| 180 | |
---|
[10371] | 181 | if (!this->parents_.empty()) |
---|
| 182 | { |
---|
| 183 | // parents defined -> this class was initialized by creating a sample instance and recording the trace of identifiers |
---|
| 184 | |
---|
| 185 | // initialize all parents |
---|
[10372] | 186 | for (std::list<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it) |
---|
[10371] | 187 | const_cast<Identifier*>(*it)->finishInitialization(); // initialize parent |
---|
| 188 | |
---|
| 189 | // parents of parents are no direct parents of this identifier |
---|
[9667] | 190 | this->directParents_ = this->parents_; |
---|
[10372] | 191 | for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent) |
---|
| 192 | for (std::list<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) |
---|
| 193 | this->directParents_.remove(*it_parent_parent); |
---|
[10371] | 194 | } |
---|
| 195 | else if (!this->directParents_.empty()) |
---|
| 196 | { |
---|
| 197 | // no parents defined -> this class was manually initialized by calling inheritsFrom<Class>() |
---|
[9667] | 198 | |
---|
[10371] | 199 | // initialize all direct parents |
---|
[10372] | 200 | for (std::list<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) |
---|
[10371] | 201 | const_cast<Identifier*>(*it)->finishInitialization(); // initialize parent |
---|
| 202 | |
---|
| 203 | // direct parents and their parents are also parents of this identifier (but only add them once) |
---|
[10372] | 204 | for (std::list<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent) |
---|
[10375] | 205 | { |
---|
[10372] | 206 | for (std::list<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) |
---|
[10371] | 207 | if (std::find(this->parents_.begin(), this->parents_.end(), *it_parent_parent) == this->parents_.end()) |
---|
[10372] | 208 | this->parents_.push_back(*it_parent_parent); |
---|
[10375] | 209 | if (std::find(this->parents_.begin(), this->parents_.end(), *it_parent) == this->parents_.end()) |
---|
| 210 | this->parents_.push_back(*it_parent); |
---|
| 211 | } |
---|
[10371] | 212 | } |
---|
| 213 | else if (!this->isExactlyA(Class(Identifiable))) |
---|
[9667] | 214 | { |
---|
[10371] | 215 | // only Identifiable is allowed to have no parents (even tough it's currently not abstract) |
---|
| 216 | orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl; |
---|
| 217 | orxout(internal_error) << " If this class is not abstract, use RegisterClass(ThisClass);" << endl; |
---|
| 218 | orxout(internal_error) << " If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl; |
---|
[9667] | 219 | } |
---|
| 220 | |
---|
| 221 | // tell all parents that this identifier is a child |
---|
[10372] | 222 | for (std::list<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it) |
---|
[9667] | 223 | const_cast<Identifier*>(*it)->children_.insert(this); |
---|
| 224 | |
---|
| 225 | // tell all direct parents that this identifier is a direct child |
---|
[10372] | 226 | for (std::list<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) |
---|
[9667] | 227 | { |
---|
| 228 | const_cast<Identifier*>(*it)->directChildren_.insert(this); |
---|
| 229 | |
---|
| 230 | // Create the super-function dependencies |
---|
| 231 | (*it)->createSuperFunctionCaller(); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | this->bInitialized_ = true; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | /** |
---|
[871] | 238 | @brief Returns true, if the Identifier is at least of the given type. |
---|
[790] | 239 | @param identifier The identifier to compare with |
---|
| 240 | */ |
---|
| 241 | bool Identifier::isA(const Identifier* identifier) const |
---|
| 242 | { |
---|
[10371] | 243 | return (identifier == this || (this->isChildOf(identifier))); |
---|
[790] | 244 | } |
---|
| 245 | |
---|
| 246 | /** |
---|
[871] | 247 | @brief Returns true, if the Identifier is exactly of the given type. |
---|
[790] | 248 | @param identifier The identifier to compare with |
---|
| 249 | */ |
---|
[871] | 250 | bool Identifier::isExactlyA(const Identifier* identifier) const |
---|
[790] | 251 | { |
---|
| 252 | return (identifier == this); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | /** |
---|
[871] | 256 | @brief Returns true, if the assigned identifier is a child of the given identifier. |
---|
[790] | 257 | @param identifier The identifier to compare with |
---|
| 258 | */ |
---|
| 259 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
| 260 | { |
---|
[10372] | 261 | return (std::find(this->parents_.begin(), this->parents_.end(), identifier) != this->parents_.end()); |
---|
[1505] | 262 | } |
---|
[790] | 263 | |
---|
| 264 | /** |
---|
[1505] | 265 | @brief Returns true, if the assigned identifier is a direct child of the given identifier. |
---|
| 266 | @param identifier The identifier to compare with |
---|
| 267 | */ |
---|
| 268 | bool Identifier::isDirectChildOf(const Identifier* identifier) const |
---|
| 269 | { |
---|
[10372] | 270 | return (std::find(this->directParents_.begin(), this->directParents_.end(), identifier) != this->directParents_.end()); |
---|
[1505] | 271 | } |
---|
| 272 | |
---|
| 273 | /** |
---|
[871] | 274 | @brief Returns true, if the assigned identifier is a parent of the given identifier. |
---|
[790] | 275 | @param identifier The identifier to compare with |
---|
| 276 | */ |
---|
| 277 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
| 278 | { |
---|
[5929] | 279 | return (this->children_.find(identifier) != this->children_.end()); |
---|
[1505] | 280 | } |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | @brief Returns true, if the assigned identifier is a direct parent of the given identifier. |
---|
| 284 | @param identifier The identifier to compare with |
---|
| 285 | */ |
---|
| 286 | bool Identifier::isDirectParentOf(const Identifier* identifier) const |
---|
| 287 | { |
---|
[5929] | 288 | return (this->directChildren_.find(identifier) != this->directChildren_.end()); |
---|
[1505] | 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | @brief Adds the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 293 | @param varname The name of the variablee |
---|
| 294 | @param container The container |
---|
| 295 | */ |
---|
| 296 | void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container) |
---|
| 297 | { |
---|
| 298 | std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname); |
---|
| 299 | if (it != this->configValues_.end()) |
---|
| 300 | { |
---|
[8858] | 301 | orxout(internal_warning) << "Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << endl; |
---|
[1747] | 302 | delete (it->second); |
---|
[1505] | 303 | } |
---|
| 304 | |
---|
| 305 | this->bHasConfigValues_ = true; |
---|
| 306 | this->configValues_[varname] = container; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | @brief Returns the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 311 | @param varname The name of the variable |
---|
| 312 | @return The ConfigValueContainer |
---|
| 313 | */ |
---|
| 314 | ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname) |
---|
| 315 | { |
---|
| 316 | std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); |
---|
| 317 | if (it != configValues_.end()) |
---|
[5929] | 318 | return it->second; |
---|
[1505] | 319 | else |
---|
| 320 | return 0; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /** |
---|
[5781] | 324 | @brief Returns a XMLPortParamContainer that loads a parameter of this class. |
---|
| 325 | @param paramname The name of the parameter |
---|
| 326 | @return The container |
---|
| 327 | */ |
---|
| 328 | XMLPortParamContainer* Identifier::getXMLPortParamContainer(const std::string& paramname) |
---|
| 329 | { |
---|
| 330 | std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname); |
---|
| 331 | if (it != this->xmlportParamContainers_.end()) |
---|
[5929] | 332 | return it->second; |
---|
[5781] | 333 | else |
---|
| 334 | return 0; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | /** |
---|
| 338 | @brief Adds a new XMLPortParamContainer that loads a parameter of this class. |
---|
| 339 | @param paramname The name of the parameter |
---|
| 340 | @param container The container |
---|
| 341 | */ |
---|
| 342 | void Identifier::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container) |
---|
| 343 | { |
---|
| 344 | std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname); |
---|
| 345 | if (it != this->xmlportParamContainers_.end()) |
---|
| 346 | { |
---|
[8858] | 347 | orxout(internal_warning) << "Overwriting XMLPortParamContainer in class " << this->getName() << '.' << endl; |
---|
[5781] | 348 | delete (it->second); |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | this->xmlportParamContainers_[paramname] = container; |
---|
| 352 | } |
---|
| 353 | |
---|
| 354 | /** |
---|
| 355 | @brief Returns a XMLPortObjectContainer that attaches an object to this class. |
---|
| 356 | @param sectionname The name of the section that contains the attachable objects |
---|
| 357 | @return The container |
---|
| 358 | */ |
---|
| 359 | XMLPortObjectContainer* Identifier::getXMLPortObjectContainer(const std::string& sectionname) |
---|
| 360 | { |
---|
| 361 | std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname); |
---|
| 362 | if (it != this->xmlportObjectContainers_.end()) |
---|
[5929] | 363 | return it->second; |
---|
[5781] | 364 | else |
---|
| 365 | return 0; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | @brief Adds a new XMLPortObjectContainer that attaches an object to this class. |
---|
| 370 | @param sectionname The name of the section that contains the attachable objects |
---|
| 371 | @param container The container |
---|
| 372 | */ |
---|
| 373 | void Identifier::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container) |
---|
| 374 | { |
---|
| 375 | std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname); |
---|
| 376 | if (it != this->xmlportObjectContainers_.end()) |
---|
| 377 | { |
---|
[8858] | 378 | orxout(internal_warning) << "Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << endl; |
---|
[5781] | 379 | delete (it->second); |
---|
| 380 | } |
---|
| 381 | |
---|
| 382 | this->xmlportObjectContainers_[sectionname] = container; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | /** |
---|
[1505] | 386 | @brief Lists the names of all Identifiers in a std::set<const Identifier*>. |
---|
| 387 | @param out The outstream |
---|
| 388 | @param list The list (or set) of Identifiers |
---|
| 389 | @return The outstream |
---|
| 390 | */ |
---|
| 391 | std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list) |
---|
| 392 | { |
---|
| 393 | for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
[5929] | 394 | { |
---|
| 395 | if (it != list.begin()) |
---|
[6417] | 396 | out << ' '; |
---|
[5929] | 397 | out << (*it)->getName(); |
---|
| 398 | } |
---|
[1505] | 399 | |
---|
| 400 | return out; |
---|
| 401 | } |
---|
[790] | 402 | } |
---|