[1505] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 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 | |
---|
| 29 | /** |
---|
[2171] | 30 | @file |
---|
[1505] | 31 | @brief Implementation of the Factory class. |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include "Factory.h" |
---|
[3196] | 35 | |
---|
| 36 | #include "util/Debug.h" |
---|
[1505] | 37 | #include "Identifier.h" |
---|
| 38 | #include "BaseObject.h" |
---|
| 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | /** |
---|
| 43 | @brief Returns the Identifier with a given name. |
---|
| 44 | @param name The name of the wanted Identifier |
---|
| 45 | @return The Identifier |
---|
| 46 | */ |
---|
| 47 | Identifier* Factory::getIdentifier(const std::string& name) |
---|
| 48 | { |
---|
[2087] | 49 | std::map<std::string, Identifier*>::const_iterator it = getFactoryPointer()->identifierStringMap_.find(name); |
---|
| 50 | if (it != getFactoryPointer()->identifierStringMap_.end()) |
---|
| 51 | return it->second; |
---|
| 52 | else |
---|
| 53 | return 0; |
---|
[1505] | 54 | } |
---|
| 55 | |
---|
| 56 | /** |
---|
| 57 | @brief Returns the Identifier with a given network ID. |
---|
| 58 | @param id The network ID of the wanted Identifier |
---|
| 59 | @return The Identifier |
---|
| 60 | */ |
---|
[2662] | 61 | Identifier* Factory::getIdentifier(const uint32_t id) |
---|
[1505] | 62 | { |
---|
[2662] | 63 | std::map<uint32_t, Identifier*>::const_iterator it = getFactoryPointer()->identifierNetworkIDMap_.find(id); |
---|
[2087] | 64 | if (it != getFactoryPointer()->identifierNetworkIDMap_.end()) |
---|
| 65 | return it->second; |
---|
| 66 | else |
---|
| 67 | return 0; |
---|
[1505] | 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | @brief Adds a new Identifier to both maps. |
---|
| 72 | @param name The name of the identifier |
---|
| 73 | @param identifier The identifier to add |
---|
| 74 | */ |
---|
[5610] | 75 | void Factory::add(const std::string& name, BaseFactory* factory) |
---|
[1505] | 76 | { |
---|
[5610] | 77 | getFactoryPointer()->factoryMap_[name] = factory; |
---|
[1505] | 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
| 81 | @brief Removes the entry with the old network ID and adds a new one. |
---|
| 82 | @param identifier The identifier to change |
---|
| 83 | @param oldID The old networkID |
---|
| 84 | @param newID The new networkID |
---|
| 85 | */ |
---|
[2662] | 86 | void Factory::changeNetworkID(Identifier* identifier, const uint32_t oldID, const uint32_t newID) |
---|
[1505] | 87 | { |
---|
[2759] | 88 | // getFactoryPointer()->identifierNetworkIDMap_.erase(oldID); |
---|
[1505] | 89 | getFactoryPointer()->identifierNetworkIDMap_[newID] = identifier; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
[2759] | 93 | @brief Cleans the NetworkID map (needed on clients for correct initialization) |
---|
| 94 | */ |
---|
| 95 | void Factory::cleanNetworkIDs() |
---|
| 96 | { |
---|
| 97 | getFactoryPointer()->identifierNetworkIDMap_.clear(); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
[1505] | 101 | @brief Creates the class-hierarchy by creating and destroying one object of each type. |
---|
| 102 | */ |
---|
| 103 | void Factory::createClassHierarchy() |
---|
| 104 | { |
---|
| 105 | COUT(3) << "*** Factory: Create class-hierarchy" << std::endl; |
---|
[5610] | 106 | std::map<std::string, BaseFactory*>::iterator it; |
---|
| 107 | it = getFactoryPointer()->factoryMap_.begin(); |
---|
| 108 | Identifier::startCreatingHierarchy(); |
---|
| 109 | for (it = getFactoryPointer()->factoryMap_.begin(); it != getFactoryPointer()->factoryMap_.end(); ++it) |
---|
[1505] | 110 | { |
---|
[5610] | 111 | // Create the corresponding identifier first |
---|
| 112 | Identifier* identifier = it->second->createIdentifier(it->first); |
---|
| 113 | getFactoryPointer()->identifierStringMap_[it->first] = identifier; |
---|
| 114 | getFactoryPointer()->identifierNetworkIDMap_[identifier->getNetworkID()] = identifier; |
---|
[1505] | 115 | // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. |
---|
[5610] | 116 | BaseObject* temp = identifier->fabricate(0); |
---|
[1505] | 117 | delete temp; |
---|
| 118 | } |
---|
[5610] | 119 | Identifier::stopCreatingHierarchy(); |
---|
[1505] | 120 | COUT(3) << "*** Factory: Finished class-hierarchy creation" << std::endl; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | @brief Ensures the Factory gets created in the right moment. |
---|
| 125 | @return The Factory. |
---|
| 126 | */ |
---|
| 127 | Factory* Factory::getFactoryPointer() |
---|
| 128 | { |
---|
| 129 | static Factory theOneAndOnlyFactoryInstance = Factory(); |
---|
| 130 | return &theOneAndOnlyFactoryInstance; |
---|
| 131 | } |
---|
| 132 | } |
---|