[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" |
---|
[1747] | 39 | #include "ConfigValueContainer.h" |
---|
[3196] | 40 | #include "Factory.h" |
---|
[790] | 41 | |
---|
| 42 | namespace orxonox |
---|
| 43 | { |
---|
| 44 | // ############################### |
---|
| 45 | // ### Identifier ### |
---|
| 46 | // ############################### |
---|
[3325] | 47 | int Identifier::hierarchyCreatingCounter_s = 0; |
---|
| 48 | unsigned int Identifier::classIDCounter_s = 0; |
---|
[790] | 49 | |
---|
| 50 | /** |
---|
| 51 | @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. |
---|
| 52 | */ |
---|
| 53 | Identifier::Identifier() |
---|
[3325] | 54 | : classID_(classIDCounter_s++) |
---|
[790] | 55 | { |
---|
[1747] | 56 | this->objects_ = new ObjectListBase(this); |
---|
| 57 | |
---|
[790] | 58 | this->bCreatedOneObject_ = false; |
---|
[1747] | 59 | this->bSetName_ = false; |
---|
[1505] | 60 | this->factory_ = 0; |
---|
| 61 | |
---|
| 62 | this->bHasConfigValues_ = false; |
---|
[790] | 63 | |
---|
[1505] | 64 | this->children_ = new std::set<const Identifier*>(); |
---|
| 65 | this->directChildren_ = new std::set<const Identifier*>(); |
---|
[790] | 66 | } |
---|
| 67 | |
---|
| 68 | /** |
---|
[871] | 69 | @brief Destructor: Deletes the list containing the children. |
---|
[790] | 70 | */ |
---|
| 71 | Identifier::~Identifier() |
---|
| 72 | { |
---|
[1505] | 73 | delete this->children_; |
---|
| 74 | delete this->directChildren_; |
---|
[1747] | 75 | delete this->objects_; |
---|
| 76 | |
---|
| 77 | if (this->factory_) |
---|
| 78 | delete this->factory_; |
---|
| 79 | |
---|
| 80 | for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it) |
---|
| 81 | delete (it->second); |
---|
[790] | 82 | } |
---|
| 83 | |
---|
| 84 | /** |
---|
[2662] | 85 | @brief Returns the identifier map with the names as received by typeid(). This is only used internally. |
---|
| 86 | */ |
---|
| 87 | std::map<std::string, Identifier*>& Identifier::getTypeIDIdentifierMap() |
---|
| 88 | { |
---|
| 89 | static std::map<std::string, Identifier*> identifiers; //!< The map to store all Identifiers. |
---|
| 90 | return identifiers; |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
[1543] | 94 | @brief Returns an identifier by name and adds it if not available |
---|
| 95 | @param name The name of the identifier as typeid().name() suggests |
---|
| 96 | @param proposal A pointer to a newly created identifier for the case of non existance in the map |
---|
| 97 | @return The identifier (unique instance) |
---|
| 98 | */ |
---|
[1747] | 99 | Identifier* Identifier::getIdentifierSingleton(const std::string& name, Identifier* proposal) |
---|
[1543] | 100 | { |
---|
[2662] | 101 | std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name); |
---|
[1747] | 102 | |
---|
[2662] | 103 | if (it != getTypeIDIdentifierMap().end()) |
---|
[1543] | 104 | { |
---|
[1747] | 105 | // There is already an entry: return it and delete the proposal |
---|
| 106 | delete proposal; |
---|
| 107 | return (*it).second; |
---|
[1543] | 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
[1747] | 111 | // There is no entry: put the proposal into the map and return it |
---|
[2662] | 112 | getTypeIDIdentifierMap()[name] = proposal; |
---|
[1747] | 113 | return proposal; |
---|
[1543] | 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
[1856] | 118 | @brief Registers a class, which means that the name and the parents get stored. |
---|
| 119 | @param parents A list, containing the Identifiers of all parents of the class |
---|
| 120 | @param bRootClass True if the class is either an Interface or the BaseObject itself |
---|
| 121 | */ |
---|
| 122 | void Identifier::initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass) |
---|
| 123 | { |
---|
| 124 | // Check if at least one object of the given type was created |
---|
| 125 | if (!this->bCreatedOneObject_ && Identifier::isCreatingHierarchy()) |
---|
| 126 | { |
---|
[5695] | 127 | // If no: We have to store the information and initialize the Identifier |
---|
[1856] | 128 | COUT(4) << "*** ClassIdentifier: Register Class in " << this->getName() << "-Singleton -> Initialize Singleton." << std::endl; |
---|
| 129 | if (bRootClass) |
---|
| 130 | this->initialize(0); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case. |
---|
| 131 | else |
---|
| 132 | this->initialize(parents); |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
[871] | 137 | @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to. |
---|
| 138 | @param parents A list containing all parents |
---|
[790] | 139 | */ |
---|
[1052] | 140 | void Identifier::initialize(std::set<const Identifier*>* parents) |
---|
[790] | 141 | { |
---|
[871] | 142 | COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl; |
---|
[790] | 143 | this->bCreatedOneObject_ = true; |
---|
| 144 | |
---|
| 145 | if (parents) |
---|
[1505] | 146 | { |
---|
| 147 | this->parents_ = (*parents); |
---|
| 148 | this->directParents_ = (*parents); |
---|
| 149 | |
---|
[871] | 150 | // Iterate through all parents |
---|
[1052] | 151 | for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it) |
---|
[1505] | 152 | { |
---|
| 153 | // Tell the parent we're one of it's children |
---|
| 154 | (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this); |
---|
| 155 | |
---|
| 156 | // Erase all parents of our parent from our direct-parent-list |
---|
| 157 | for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1) |
---|
| 158 | { |
---|
| 159 | // Search for the parent's parent in our direct-parent-list |
---|
| 160 | for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2) |
---|
| 161 | { |
---|
| 162 | if ((*it1) == (*it2)) |
---|
| 163 | { |
---|
| 164 | // We've found a non-direct parent in our list: Erase it |
---|
| 165 | this->directParents_.erase(it2); |
---|
| 166 | break; |
---|
| 167 | } |
---|
| 168 | } |
---|
| 169 | } |
---|
[790] | 170 | } |
---|
[1505] | 171 | |
---|
| 172 | // Now iterate through all direct parents |
---|
| 173 | for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) |
---|
| 174 | { |
---|
| 175 | // Tell the parent we're one of it's direct children |
---|
| 176 | (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this); |
---|
| 177 | } |
---|
[790] | 178 | } |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | /** |
---|
[1747] | 182 | @brief Destroys all Identifiers. Called when exiting the program. |
---|
| 183 | */ |
---|
| 184 | void Identifier::destroyAllIdentifiers() |
---|
| 185 | { |
---|
[2662] | 186 | for (std::map<std::string, Identifier*>::iterator it = Identifier::getTypeIDIdentifierMap().begin(); it != Identifier::getTypeIDIdentifierMap().end(); ++it) |
---|
[1747] | 187 | delete (it->second); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | @brief Sets the name of the class. |
---|
| 192 | @param name The name |
---|
| 193 | */ |
---|
| 194 | void Identifier::setName(const std::string& name) |
---|
| 195 | { |
---|
| 196 | if (!this->bSetName_) |
---|
| 197 | { |
---|
| 198 | this->name_ = name; |
---|
| 199 | this->bSetName_ = true; |
---|
| 200 | Identifier::getIdentifierMapIntern()[name] = this; |
---|
| 201 | Identifier::getLowercaseIdentifierMapIntern()[getLowercase(name)] = this; |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
[790] | 206 | @brief Creates an object of the type the Identifier belongs to. |
---|
| 207 | @return The new object |
---|
| 208 | */ |
---|
[2087] | 209 | BaseObject* Identifier::fabricate(BaseObject* creator) |
---|
[790] | 210 | { |
---|
| 211 | if (this->factory_) |
---|
| 212 | { |
---|
[2087] | 213 | return this->factory_->fabricate(creator); // We have to return a BaseObject, because we don't know the exact type. |
---|
[790] | 214 | } |
---|
| 215 | else |
---|
| 216 | { |
---|
[1505] | 217 | COUT(1) << "An error occurred in Identifier.cc:" << std::endl; |
---|
[871] | 218 | COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl; |
---|
[790] | 219 | COUT(1) << "Aborting..." << std::endl; |
---|
| 220 | abort(); |
---|
| 221 | return NULL; |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
[871] | 226 | @brief Returns true, if the Identifier is at least of the given type. |
---|
[790] | 227 | @param identifier The identifier to compare with |
---|
| 228 | */ |
---|
| 229 | bool Identifier::isA(const Identifier* identifier) const |
---|
| 230 | { |
---|
[1059] | 231 | return (identifier == this || (this->parents_.find(identifier) != this->parents_.end())); |
---|
[790] | 232 | } |
---|
| 233 | |
---|
| 234 | /** |
---|
[871] | 235 | @brief Returns true, if the Identifier is exactly of the given type. |
---|
[790] | 236 | @param identifier The identifier to compare with |
---|
| 237 | */ |
---|
[871] | 238 | bool Identifier::isExactlyA(const Identifier* identifier) const |
---|
[790] | 239 | { |
---|
| 240 | return (identifier == this); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | /** |
---|
[871] | 244 | @brief Returns true, if the assigned identifier is a child of the given identifier. |
---|
[790] | 245 | @param identifier The identifier to compare with |
---|
| 246 | */ |
---|
| 247 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
| 248 | { |
---|
[1060] | 249 | return (this->parents_.find(identifier) != this->parents_.end()); |
---|
[1505] | 250 | } |
---|
[790] | 251 | |
---|
| 252 | /** |
---|
[1505] | 253 | @brief Returns true, if the assigned identifier is a direct child of the given identifier. |
---|
| 254 | @param identifier The identifier to compare with |
---|
| 255 | */ |
---|
| 256 | bool Identifier::isDirectChildOf(const Identifier* identifier) const |
---|
| 257 | { |
---|
| 258 | return (this->directParents_.find(identifier) != this->directParents_.end()); |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
[871] | 262 | @brief Returns true, if the assigned identifier is a parent of the given identifier. |
---|
[790] | 263 | @param identifier The identifier to compare with |
---|
| 264 | */ |
---|
| 265 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
| 266 | { |
---|
[1052] | 267 | return (this->children_->find(identifier) != this->children_->end()); |
---|
[1505] | 268 | } |
---|
| 269 | |
---|
| 270 | /** |
---|
| 271 | @brief Returns true, if the assigned identifier is a direct parent of the given identifier. |
---|
| 272 | @param identifier The identifier to compare with |
---|
| 273 | */ |
---|
| 274 | bool Identifier::isDirectParentOf(const Identifier* identifier) const |
---|
| 275 | { |
---|
| 276 | return (this->directChildren_->find(identifier) != this->directChildren_->end()); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | /** |
---|
| 280 | @brief Returns the map that stores all Identifiers. |
---|
| 281 | @return The map |
---|
| 282 | */ |
---|
| 283 | std::map<std::string, Identifier*>& Identifier::getIdentifierMapIntern() |
---|
| 284 | { |
---|
| 285 | static std::map<std::string, Identifier*> identifierMap; |
---|
| 286 | return identifierMap; |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | /** |
---|
| 290 | @brief Returns the map that stores all Identifiers. |
---|
| 291 | @return The map |
---|
| 292 | */ |
---|
| 293 | std::map<std::string, Identifier*>& Identifier::getLowercaseIdentifierMapIntern() |
---|
| 294 | { |
---|
| 295 | static std::map<std::string, Identifier*> lowercaseIdentifierMap; |
---|
| 296 | return lowercaseIdentifierMap; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | @brief Adds the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 301 | @param varname The name of the variablee |
---|
| 302 | @param container The container |
---|
| 303 | */ |
---|
| 304 | void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container) |
---|
| 305 | { |
---|
| 306 | std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname); |
---|
| 307 | if (it != this->configValues_.end()) |
---|
| 308 | { |
---|
| 309 | COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << "." << std::endl; |
---|
[1747] | 310 | delete (it->second); |
---|
[1505] | 311 | } |
---|
| 312 | |
---|
| 313 | this->bHasConfigValues_ = true; |
---|
| 314 | this->configValues_[varname] = container; |
---|
| 315 | this->configValues_LC_[getLowercase(varname)] = container; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /** |
---|
| 319 | @brief Returns the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 320 | @param varname The name of the variable |
---|
| 321 | @return The ConfigValueContainer |
---|
| 322 | */ |
---|
| 323 | ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname) |
---|
| 324 | { |
---|
| 325 | std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); |
---|
| 326 | if (it != configValues_.end()) |
---|
| 327 | return ((*it).second); |
---|
| 328 | else |
---|
| 329 | return 0; |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | /** |
---|
| 333 | @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase. |
---|
| 334 | @param varname The name of the variable in lowercase |
---|
| 335 | @return The ConfigValueContainer |
---|
| 336 | */ |
---|
| 337 | ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname) |
---|
| 338 | { |
---|
| 339 | std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname); |
---|
| 340 | if (it != configValues_LC_.end()) |
---|
| 341 | return ((*it).second); |
---|
| 342 | else |
---|
| 343 | return 0; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | /** |
---|
| 347 | @brief Lists the names of all Identifiers in a std::set<const Identifier*>. |
---|
| 348 | @param out The outstream |
---|
| 349 | @param list The list (or set) of Identifiers |
---|
| 350 | @return The outstream |
---|
| 351 | */ |
---|
| 352 | std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list) |
---|
| 353 | { |
---|
| 354 | for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
| 355 | out << (*it)->getName() << " "; |
---|
| 356 | |
---|
| 357 | return out; |
---|
| 358 | } |
---|
[790] | 359 | } |
---|