[513] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * ... |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[384] | 28 | /*! |
---|
| 29 | @file Factory.cc |
---|
| 30 | @brief Implementation of the Factory class. |
---|
| 31 | */ |
---|
| 32 | |
---|
[513] | 33 | #include "Factory.h" |
---|
[496] | 34 | #include "Identifier.h" |
---|
| 35 | #include "Debug.h" |
---|
[513] | 36 | #include "../objects/BaseObject.h" |
---|
[384] | 37 | |
---|
[513] | 38 | namespace orxonox |
---|
| 39 | { |
---|
[551] | 40 | Factory* Factory::pointer1_s; |
---|
| 41 | Factory* Factory::pointer2_s; |
---|
| 42 | Factory* Factory::pointer3_s; |
---|
| 43 | Factory* Factory::pointer4_s; |
---|
| 44 | Factory* Factory::pointer5_s = NULL; // Set the static member variable pointer5_s to zero |
---|
[513] | 45 | |
---|
[384] | 46 | /** |
---|
[551] | 47 | @brief Checks if the pointer to the only Factory-object exists and creates it, if not. |
---|
| 48 | */ |
---|
| 49 | void Factory::checkPointer() |
---|
| 50 | { |
---|
| 51 | if ((!pointer5_s) || (pointer1_s != pointer2_s) || (pointer2_s != pointer3_s) || (pointer3_s != pointer4_s) || (pointer4_s != pointer5_s)) |
---|
| 52 | { |
---|
| 53 | std::cout << pointer1_s << std::endl; |
---|
| 54 | std::cout << pointer2_s << std::endl; |
---|
| 55 | std::cout << pointer3_s << std::endl; |
---|
| 56 | std::cout << pointer4_s << std::endl; |
---|
| 57 | std::cout << pointer5_s << std::endl; |
---|
| 58 | pointer1_s = pointer2_s = pointer3_s = pointer4_s = pointer5_s = new Factory; |
---|
| 59 | std::cout << pointer1_s << std::endl; |
---|
| 60 | std::cout << pointer2_s << std::endl; |
---|
| 61 | std::cout << pointer3_s << std::endl; |
---|
| 62 | std::cout << pointer4_s << std::endl; |
---|
| 63 | std::cout << pointer5_s << std::endl; |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
[384] | 68 | @returns the Identifier with a given name. |
---|
| 69 | @param name The name of the wanted Identifier |
---|
[513] | 70 | */ |
---|
| 71 | Identifier* Factory::getIdentifier(const std::string& name) |
---|
| 72 | { |
---|
[551] | 73 | Factory::checkPointer(); |
---|
[363] | 74 | |
---|
[551] | 75 | return pointer1_s->identifierStringMap_[name]; |
---|
[363] | 76 | } |
---|
| 77 | |
---|
[384] | 78 | /** |
---|
[496] | 79 | @returns the Identifier with a given network ID. |
---|
| 80 | @param id The network ID of the wanted Identifier |
---|
[384] | 81 | */ |
---|
[363] | 82 | Identifier* Factory::getIdentifier(const unsigned int id) |
---|
| 83 | { |
---|
[551] | 84 | Factory::checkPointer(); |
---|
[363] | 85 | |
---|
[551] | 86 | return pointer1_s->identifierNetworkIDMap_[id]; |
---|
[513] | 87 | } |
---|
[384] | 88 | |
---|
| 89 | /** |
---|
| 90 | @brief Adds a new Identifier to both maps. |
---|
| 91 | @param name The name of the identifier |
---|
| 92 | @param identifier The identifier to add |
---|
[513] | 93 | */ |
---|
| 94 | void Factory::add(const std::string& name, Identifier* identifier) |
---|
| 95 | { |
---|
[551] | 96 | Factory::checkPointer(); |
---|
[513] | 97 | |
---|
[551] | 98 | pointer1_s->identifierStringMap_[name] = identifier; |
---|
| 99 | pointer1_s->identifierNetworkIDMap_[identifier->getNetworkID()] = identifier; |
---|
[363] | 100 | } |
---|
| 101 | |
---|
[384] | 102 | /** |
---|
[496] | 103 | @brief Removes the entry with the old network ID and adds a new one. |
---|
[384] | 104 | @param identifier The identifier to change |
---|
| 105 | @param oldID The old networkID |
---|
| 106 | @param newID The new networkID |
---|
| 107 | */ |
---|
[363] | 108 | void Factory::changeNetworkID(Identifier* identifier, const unsigned int oldID, const unsigned int newID) |
---|
| 109 | { |
---|
[551] | 110 | Factory::checkPointer(); |
---|
| 111 | |
---|
| 112 | pointer1_s->identifierNetworkIDMap_.erase(oldID); |
---|
| 113 | pointer1_s->identifierNetworkIDMap_[newID] = identifier; |
---|
[496] | 114 | } |
---|
| 115 | |
---|
| 116 | /** |
---|
| 117 | @brief Creates the class-hierarchy by creating and destroying one object of each type. |
---|
| 118 | */ |
---|
| 119 | void Factory::createClassHierarchy() |
---|
| 120 | { |
---|
[551] | 121 | Factory::checkPointer(); |
---|
[496] | 122 | |
---|
| 123 | COUT(4) << "*** Factory -> Create class-hierarchy\n"; |
---|
| 124 | std::map<std::string, Identifier*>::iterator it; |
---|
[551] | 125 | it = pointer1_s->identifierStringMap_.begin(); |
---|
| 126 | (*pointer1_s->identifierStringMap_.begin()).second->startCreatingHierarchy(); |
---|
| 127 | for (it = pointer1_s->identifierStringMap_.begin(); it != pointer1_s->identifierStringMap_.end(); ++it) |
---|
[496] | 128 | { |
---|
| 129 | // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards. |
---|
| 130 | BaseObject* temp = (*it).second->fabricate(); |
---|
| 131 | delete temp; |
---|
| 132 | } |
---|
[551] | 133 | (*pointer1_s->identifierStringMap_.begin()).second->stopCreatingHierarchy(); |
---|
[496] | 134 | COUT(4) << "*** Factory -> Finished class-hierarchy creation\n"; |
---|
| 135 | } |
---|
[513] | 136 | } |
---|