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 Identifier.cc |
---|
30 | @brief Implementation of the Identifier class. |
---|
31 | */ |
---|
32 | |
---|
33 | #include "Identifier.h" |
---|
34 | #include "Factory.h" |
---|
35 | |
---|
36 | namespace orxonox |
---|
37 | { |
---|
38 | // ############################### |
---|
39 | // ### Identifier ### |
---|
40 | // ############################### |
---|
41 | int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero (this static member variable is ok: it's used in main(), not before) |
---|
42 | |
---|
43 | /** |
---|
44 | @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. |
---|
45 | */ |
---|
46 | Identifier::Identifier() |
---|
47 | { |
---|
48 | this->bCreatedOneObject_ = false; |
---|
49 | this->factory_ = 0; |
---|
50 | |
---|
51 | this->children_ = new std::list<const Identifier*>(); |
---|
52 | |
---|
53 | // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables |
---|
54 | static unsigned int classIDcounter_s = 0; |
---|
55 | this->classID_ = classIDcounter_s++; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | @brief Destructor: Deletes the list containing the children. |
---|
60 | */ |
---|
61 | Identifier::~Identifier() |
---|
62 | { |
---|
63 | delete this->children_; |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to. |
---|
68 | @param parents A list containing all parents |
---|
69 | */ |
---|
70 | void Identifier::initialize(std::list<const Identifier*>* parents) |
---|
71 | { |
---|
72 | COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl; |
---|
73 | this->bCreatedOneObject_ = true; |
---|
74 | |
---|
75 | if (parents) |
---|
76 | { |
---|
77 | std::list<const Identifier*>::iterator temp1 = parents->begin(); |
---|
78 | while (temp1 != parents->end()) |
---|
79 | { |
---|
80 | this->parents_.insert(this->parents_.end(), *temp1); |
---|
81 | (*temp1)->getChildren().insert((*temp1)->getChildren().end(), this); // We're a child of our parents |
---|
82 | |
---|
83 | ++temp1; |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | @brief Creates an object of the type the Identifier belongs to. |
---|
90 | @return The new object |
---|
91 | */ |
---|
92 | BaseObject* Identifier::fabricate() |
---|
93 | { |
---|
94 | if (this->factory_) |
---|
95 | { |
---|
96 | return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type. |
---|
97 | } |
---|
98 | else |
---|
99 | { |
---|
100 | // Abstract classes don't have a factory and therefore can't create new objects |
---|
101 | COUT(1) << "An error occurred in Identifier:" << std::endl; |
---|
102 | COUT(1) << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract." << std::endl; |
---|
103 | COUT(1) << "Aborting..." << std::endl; |
---|
104 | abort(); |
---|
105 | return NULL; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | @brief Sets the network ID to a new value and changes the entry in the Factory. |
---|
111 | @param id The new network ID |
---|
112 | */ |
---|
113 | void Identifier::setNetworkID(unsigned int id) |
---|
114 | { |
---|
115 | Factory::changeNetworkID(this, this->classID_, id); |
---|
116 | this->classID_ = id; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | @brief Returns true, if the Identifier is at least of the given type. |
---|
121 | @param identifier The identifier to compare with |
---|
122 | */ |
---|
123 | bool Identifier::isA(const Identifier* identifier) const |
---|
124 | { |
---|
125 | return (identifier == this || this->identifierIsInList(identifier, this->parents_)); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | @brief Returns true, if the Identifier is exactly of the given type. |
---|
130 | @param identifier The identifier to compare with |
---|
131 | */ |
---|
132 | bool Identifier::isDirectlyA(const Identifier* identifier) const |
---|
133 | { |
---|
134 | return (identifier == this); |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | @brief Returns true, if the assigned identifier is a child of the given identifier. |
---|
139 | @param identifier The identifier to compare with |
---|
140 | */ |
---|
141 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
142 | { |
---|
143 | return this->identifierIsInList(identifier, this->parents_); |
---|
144 | } |
---|
145 | |
---|
146 | /** |
---|
147 | @brief Returns true, if the assigned identifier is a parent of the given identifier. |
---|
148 | @param identifier The identifier to compare with |
---|
149 | */ |
---|
150 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
151 | { |
---|
152 | return this->identifierIsInList(identifier, *this->children_); |
---|
153 | } |
---|
154 | |
---|
155 | /** |
---|
156 | @brief Searches for a given identifier in a list and returns whether the identifier is in the list or not. |
---|
157 | @param identifier The identifier to look for |
---|
158 | @param list The list |
---|
159 | @return True = the identifier is in the list |
---|
160 | */ |
---|
161 | bool Identifier::identifierIsInList(const Identifier* identifier, const std::list<const Identifier*>& list) |
---|
162 | { |
---|
163 | for (std::list<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
164 | if (identifier == (*it)) |
---|
165 | return true; |
---|
166 | |
---|
167 | return false; |
---|
168 | } |
---|
169 | } |
---|