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