[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 | |
---|
[9564] | 34 | #include "IdentifierManager.h" |
---|
[1505] | 35 | |
---|
| 36 | #include <ostream> |
---|
| 37 | |
---|
[3280] | 38 | #include "util/StringUtils.h" |
---|
[9563] | 39 | #include "core/config/ConfigValueContainer.h" |
---|
| 40 | #include "core/XMLPort.h" |
---|
| 41 | #include "core/object/ClassFactory.h" |
---|
[790] | 42 | |
---|
| 43 | namespace orxonox |
---|
| 44 | { |
---|
[9640] | 45 | /* static */ IdentifierManager& IdentifierManager::getInstance() |
---|
| 46 | { |
---|
| 47 | static IdentifierManager instance; |
---|
| 48 | return instance; |
---|
| 49 | } |
---|
[790] | 50 | |
---|
[9640] | 51 | IdentifierManager::IdentifierManager() |
---|
[2662] | 52 | { |
---|
[9640] | 53 | this->hierarchyCreatingCounter_s = 0; |
---|
| 54 | this->classIDCounter_s = 0; |
---|
[2662] | 55 | } |
---|
| 56 | |
---|
| 57 | /** |
---|
[1543] | 58 | @brief Returns an identifier by name and adds it if not available |
---|
| 59 | @param name The name of the identifier as typeid().name() suggests |
---|
[8267] | 60 | @param proposal A pointer to a newly created identifier for the case of non existence in the map |
---|
[1543] | 61 | @return The identifier (unique instance) |
---|
| 62 | */ |
---|
[9564] | 63 | Identifier* IdentifierManager::getIdentifierSingleton(const std::string& name, Identifier* proposal) |
---|
[1543] | 64 | { |
---|
[9640] | 65 | std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeId_.find(name); |
---|
[1747] | 66 | |
---|
[9640] | 67 | if (it != this->identifierByTypeId_.end()) |
---|
[1543] | 68 | { |
---|
[1747] | 69 | // There is already an entry: return it and delete the proposal |
---|
| 70 | delete proposal; |
---|
[5929] | 71 | return it->second; |
---|
[1543] | 72 | } |
---|
| 73 | else |
---|
| 74 | { |
---|
[1747] | 75 | // There is no entry: put the proposal into the map and return it |
---|
[9640] | 76 | this->identifierByTypeId_[name] = proposal; |
---|
[1747] | 77 | return proposal; |
---|
[1543] | 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
[9641] | 82 | * Registers the identifier in all maps of the IdentifierManager. |
---|
| 83 | */ |
---|
| 84 | void IdentifierManager::registerIdentifier(Identifier* identifier) |
---|
| 85 | { |
---|
| 86 | IdentifierManager::getInstance().identifierByString_[identifier->getName()] = identifier; |
---|
| 87 | IdentifierManager::getInstance().identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier; |
---|
| 88 | IdentifierManager::getInstance().identifierByNetworkId_[identifier->getNetworkID()] = identifier; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
[5929] | 92 | @brief Creates the class-hierarchy by creating and destroying one object of each type. |
---|
| 93 | */ |
---|
[9564] | 94 | void IdentifierManager::createClassHierarchy() |
---|
[5929] | 95 | { |
---|
[8858] | 96 | orxout(internal_status) << "Create class-hierarchy" << endl; |
---|
[9640] | 97 | this->startCreatingHierarchy(); |
---|
| 98 | for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeId_.begin(); it != this->identifierByTypeId_.end(); ++it) |
---|
[5929] | 99 | { |
---|
| 100 | // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. |
---|
| 101 | if (it->second->hasFactory()) |
---|
| 102 | { |
---|
[9632] | 103 | Identifiable* temp = it->second->fabricate(0); |
---|
| 104 | delete temp; |
---|
[5929] | 105 | } |
---|
| 106 | } |
---|
[9640] | 107 | this->stopCreatingHierarchy(); |
---|
[8858] | 108 | orxout(internal_status) << "Finished class-hierarchy creation" << endl; |
---|
[5929] | 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
[1747] | 112 | @brief Destroys all Identifiers. Called when exiting the program. |
---|
| 113 | */ |
---|
[9564] | 114 | void IdentifierManager::destroyAllIdentifiers() |
---|
[1747] | 115 | { |
---|
[9640] | 116 | for (std::map<std::string, Identifier*>::iterator it = this->identifierByTypeId_.begin(); it != this->identifierByTypeId_.end(); ++it) |
---|
[1747] | 117 | delete (it->second); |
---|
[9634] | 118 | |
---|
[9640] | 119 | this->identifierByTypeId_.clear(); |
---|
| 120 | this->identifierByString_.clear(); |
---|
| 121 | this->identifierByLowercaseString_.clear(); |
---|
| 122 | this->identifierByNetworkId_.clear(); |
---|
[1747] | 123 | } |
---|
| 124 | |
---|
| 125 | /** |
---|
[5929] | 126 | @brief Returns the Identifier with a given name. |
---|
| 127 | @param name The name of the wanted Identifier |
---|
| 128 | @return The Identifier |
---|
| 129 | */ |
---|
[9564] | 130 | Identifier* IdentifierManager::getIdentifierByString(const std::string& name) |
---|
[5929] | 131 | { |
---|
[9640] | 132 | std::map<std::string, Identifier*>::const_iterator it = this->identifierByString_.find(name); |
---|
| 133 | if (it != this->identifierByString_.end()) |
---|
[5929] | 134 | return it->second; |
---|
| 135 | else |
---|
| 136 | return 0; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | @brief Returns the Identifier with a given name in lowercase. |
---|
| 141 | @param name The name of the wanted Identifier |
---|
| 142 | @return The Identifier |
---|
| 143 | */ |
---|
[9564] | 144 | Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name) |
---|
[5929] | 145 | { |
---|
[9640] | 146 | std::map<std::string, Identifier*>::const_iterator it = this->identifierByLowercaseString_.find(name); |
---|
| 147 | if (it != this->identifierByLowercaseString_.end()) |
---|
[5929] | 148 | return it->second; |
---|
| 149 | else |
---|
| 150 | return 0; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | @brief Returns the Identifier with a given network ID. |
---|
| 155 | @param id The network ID of the wanted Identifier |
---|
| 156 | @return The Identifier |
---|
| 157 | */ |
---|
[9564] | 158 | Identifier* IdentifierManager::getIdentifierByID(const uint32_t id) |
---|
[5929] | 159 | { |
---|
[9640] | 160 | std::map<uint32_t, Identifier*>::const_iterator it = this->identifierByNetworkId_.find(id); |
---|
| 161 | if (it != this->identifierByNetworkId_.end()) |
---|
[5929] | 162 | return it->second; |
---|
| 163 | else |
---|
| 164 | return 0; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | @brief Cleans the NetworkID map (needed on clients for correct initialization) |
---|
| 169 | */ |
---|
[9564] | 170 | void IdentifierManager::clearNetworkIDs() |
---|
[5929] | 171 | { |
---|
[9640] | 172 | this->identifierByNetworkId_.clear(); |
---|
[5929] | 173 | } |
---|
[790] | 174 | } |
---|