[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 | { |
---|
[9564] | 45 | int IdentifierManager::hierarchyCreatingCounter_s = 0; |
---|
| 46 | unsigned int IdentifierManager::classIDCounter_s = 0; |
---|
[790] | 47 | |
---|
| 48 | /** |
---|
[2662] | 49 | @brief Returns the identifier map with the names as received by typeid(). This is only used internally. |
---|
| 50 | */ |
---|
[9564] | 51 | std::map<std::string, Identifier*>& IdentifierManager::getTypeIDIdentifierMap() |
---|
[2662] | 52 | { |
---|
| 53 | static std::map<std::string, Identifier*> identifiers; //!< The map to store all Identifiers. |
---|
| 54 | return identifiers; |
---|
| 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 | { |
---|
[2662] | 65 | std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name); |
---|
[1747] | 66 | |
---|
[2662] | 67 | if (it != getTypeIDIdentifierMap().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 |
---|
[2662] | 76 | getTypeIDIdentifierMap()[name] = proposal; |
---|
[1747] | 77 | return proposal; |
---|
[1543] | 78 | } |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /** |
---|
[5929] | 82 | @brief Creates the class-hierarchy by creating and destroying one object of each type. |
---|
| 83 | */ |
---|
[9564] | 84 | void IdentifierManager::createClassHierarchy() |
---|
[5929] | 85 | { |
---|
[8858] | 86 | orxout(internal_status) << "Create class-hierarchy" << endl; |
---|
[9564] | 87 | IdentifierManager::startCreatingHierarchy(); |
---|
| 88 | for (std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getStringIdentifierMap().begin(); it != IdentifierManager::getStringIdentifierMap().end(); ++it) |
---|
[5929] | 89 | { |
---|
| 90 | // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. |
---|
| 91 | if (it->second->hasFactory()) |
---|
| 92 | { |
---|
[9556] | 93 | OrxonoxClass* temp = it->second->fabricate(0); |
---|
[5929] | 94 | temp->destroy(); |
---|
| 95 | } |
---|
| 96 | } |
---|
[9564] | 97 | IdentifierManager::stopCreatingHierarchy(); |
---|
[8858] | 98 | orxout(internal_status) << "Finished class-hierarchy creation" << endl; |
---|
[5929] | 99 | } |
---|
| 100 | |
---|
| 101 | /** |
---|
[1747] | 102 | @brief Destroys all Identifiers. Called when exiting the program. |
---|
| 103 | */ |
---|
[9564] | 104 | void IdentifierManager::destroyAllIdentifiers() |
---|
[1747] | 105 | { |
---|
[9564] | 106 | for (std::map<std::string, Identifier*>::iterator it = IdentifierManager::getTypeIDIdentifierMap().begin(); it != IdentifierManager::getTypeIDIdentifierMap().end(); ++it) |
---|
[1747] | 107 | delete (it->second); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /** |
---|
[5929] | 111 | @brief Returns the map that stores all Identifiers with their names. |
---|
[1505] | 112 | @return The map |
---|
| 113 | */ |
---|
[9564] | 114 | std::map<std::string, Identifier*>& IdentifierManager::getStringIdentifierMapIntern() |
---|
[1505] | 115 | { |
---|
| 116 | static std::map<std::string, Identifier*> identifierMap; |
---|
| 117 | return identifierMap; |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
[5929] | 121 | @brief Returns the map that stores all Identifiers with their names in lowercase. |
---|
[1505] | 122 | @return The map |
---|
| 123 | */ |
---|
[9564] | 124 | std::map<std::string, Identifier*>& IdentifierManager::getLowercaseStringIdentifierMapIntern() |
---|
[1505] | 125 | { |
---|
| 126 | static std::map<std::string, Identifier*> lowercaseIdentifierMap; |
---|
| 127 | return lowercaseIdentifierMap; |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
[5929] | 131 | @brief Returns the map that stores all Identifiers with their network IDs. |
---|
| 132 | @return The map |
---|
| 133 | */ |
---|
[9564] | 134 | std::map<uint32_t, Identifier*>& IdentifierManager::getIDIdentifierMapIntern() |
---|
[5929] | 135 | { |
---|
| 136 | static std::map<uint32_t, Identifier*> identifierMap; |
---|
| 137 | return identifierMap; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | @brief Returns the Identifier with a given name. |
---|
| 142 | @param name The name of the wanted Identifier |
---|
| 143 | @return The Identifier |
---|
| 144 | */ |
---|
[9564] | 145 | Identifier* IdentifierManager::getIdentifierByString(const std::string& name) |
---|
[5929] | 146 | { |
---|
[9564] | 147 | std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getStringIdentifierMapIntern().find(name); |
---|
| 148 | if (it != IdentifierManager::getStringIdentifierMapIntern().end()) |
---|
[5929] | 149 | return it->second; |
---|
| 150 | else |
---|
| 151 | return 0; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | @brief Returns the Identifier with a given name in lowercase. |
---|
| 156 | @param name The name of the wanted Identifier |
---|
| 157 | @return The Identifier |
---|
| 158 | */ |
---|
[9564] | 159 | Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name) |
---|
[5929] | 160 | { |
---|
[9564] | 161 | std::map<std::string, Identifier*>::const_iterator it = IdentifierManager::getLowercaseStringIdentifierMapIntern().find(name); |
---|
| 162 | if (it != IdentifierManager::getLowercaseStringIdentifierMapIntern().end()) |
---|
[5929] | 163 | return it->second; |
---|
| 164 | else |
---|
| 165 | return 0; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | @brief Returns the Identifier with a given network ID. |
---|
| 170 | @param id The network ID of the wanted Identifier |
---|
| 171 | @return The Identifier |
---|
| 172 | */ |
---|
[9564] | 173 | Identifier* IdentifierManager::getIdentifierByID(const uint32_t id) |
---|
[5929] | 174 | { |
---|
[9564] | 175 | std::map<uint32_t, Identifier*>::const_iterator it = IdentifierManager::getIDIdentifierMapIntern().find(id); |
---|
| 176 | if (it != IdentifierManager::getIDIdentifierMapIntern().end()) |
---|
[5929] | 177 | return it->second; |
---|
| 178 | else |
---|
| 179 | return 0; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | @brief Cleans the NetworkID map (needed on clients for correct initialization) |
---|
| 184 | */ |
---|
[9564] | 185 | void IdentifierManager::clearNetworkIDs() |
---|
[5929] | 186 | { |
---|
[9564] | 187 | IdentifierManager::getIDIdentifierMapIntern().clear(); |
---|
[5929] | 188 | } |
---|
[790] | 189 | } |
---|