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