[790] | 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 | |
---|
[871] | 28 | /** |
---|
[790] | 29 | @file Identifier.cc |
---|
| 30 | @brief Implementation of the Identifier class. |
---|
| 31 | */ |
---|
[871] | 32 | |
---|
| 33 | #include <ostream> |
---|
[790] | 34 | |
---|
[197] | 35 | #include "Identifier.h" |
---|
[790] | 36 | #include "Factory.h" |
---|
[947] | 37 | #include "Executor.h" |
---|
| 38 | #include "CommandExecutor.h" |
---|
[790] | 39 | |
---|
| 40 | namespace orxonox |
---|
| 41 | { |
---|
| 42 | // ############################### |
---|
| 43 | // ### Identifier ### |
---|
| 44 | // ############################### |
---|
[947] | 45 | 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) |
---|
[790] | 46 | |
---|
| 47 | /** |
---|
| 48 | @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. |
---|
| 49 | */ |
---|
| 50 | Identifier::Identifier() |
---|
| 51 | { |
---|
| 52 | this->bCreatedOneObject_ = false; |
---|
[947] | 53 | this->factory_ = 0; |
---|
| 54 | |
---|
| 55 | this->bHasConfigValues_ = false; |
---|
| 56 | this->bHasConsoleCommands_ = false; |
---|
[790] | 57 | |
---|
[876] | 58 | this->children_ = new std::set<const Identifier*>(); |
---|
| 59 | this->directChildren_ = new std::set<const Identifier*>(); |
---|
[197] | 60 | |
---|
[790] | 61 | // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables |
---|
| 62 | static unsigned int classIDcounter_s = 0; |
---|
[871] | 63 | this->classID_ = classIDcounter_s++; |
---|
[790] | 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
[871] | 67 | @brief Destructor: Deletes the list containing the children. |
---|
[790] | 68 | */ |
---|
| 69 | Identifier::~Identifier() |
---|
| 70 | { |
---|
[871] | 71 | delete this->children_; |
---|
| 72 | delete this->directChildren_; |
---|
[790] | 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
[871] | 76 | @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to. |
---|
| 77 | @param parents A list containing all parents |
---|
[790] | 78 | */ |
---|
[876] | 79 | void Identifier::initialize(std::set<const Identifier*>* parents) |
---|
[790] | 80 | { |
---|
[871] | 81 | COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl; |
---|
[790] | 82 | this->bCreatedOneObject_ = true; |
---|
| 83 | |
---|
| 84 | if (parents) |
---|
[871] | 85 | { |
---|
| 86 | this->parents_ = (*parents); |
---|
| 87 | this->directParents_ = (*parents); |
---|
| 88 | |
---|
| 89 | // Iterate through all parents |
---|
[876] | 90 | for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it) |
---|
[871] | 91 | { |
---|
| 92 | // Tell the parent we're one of it's children |
---|
| 93 | (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this); |
---|
| 94 | |
---|
| 95 | // Erase all parents of our parent from our direct-parent-list |
---|
[876] | 96 | for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1) |
---|
[871] | 97 | { |
---|
| 98 | // Search for the parent's parent in our direct-parent-list |
---|
[876] | 99 | for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2) |
---|
[871] | 100 | { |
---|
| 101 | if ((*it1) == (*it2)) |
---|
| 102 | { |
---|
| 103 | // We've found a non-direct parent in our list: Erase it |
---|
| 104 | this->directParents_.erase(it2); |
---|
| 105 | break; |
---|
| 106 | } |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | // Now iterate through all direct parents |
---|
[876] | 112 | for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) |
---|
[871] | 113 | { |
---|
| 114 | // Tell the parent we're one of it's direct children |
---|
| 115 | (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this); |
---|
[790] | 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | @brief Creates an object of the type the Identifier belongs to. |
---|
| 122 | @return The new object |
---|
| 123 | */ |
---|
| 124 | BaseObject* Identifier::fabricate() |
---|
| 125 | { |
---|
| 126 | if (this->factory_) |
---|
| 127 | { |
---|
| 128 | return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type. |
---|
| 129 | } |
---|
| 130 | else |
---|
| 131 | { |
---|
[871] | 132 | COUT(1) << "An error occurred in Identifier.cc:" << std::endl; |
---|
| 133 | COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl; |
---|
[790] | 134 | COUT(1) << "Aborting..." << std::endl; |
---|
| 135 | abort(); |
---|
| 136 | return NULL; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /** |
---|
| 141 | @brief Sets the network ID to a new value and changes the entry in the Factory. |
---|
| 142 | @param id The new network ID |
---|
| 143 | */ |
---|
| 144 | void Identifier::setNetworkID(unsigned int id) |
---|
| 145 | { |
---|
| 146 | Factory::changeNetworkID(this, this->classID_, id); |
---|
| 147 | this->classID_ = id; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
[871] | 151 | @brief Returns true, if the Identifier is at least of the given type. |
---|
[790] | 152 | @param identifier The identifier to compare with |
---|
| 153 | */ |
---|
| 154 | bool Identifier::isA(const Identifier* identifier) const |
---|
| 155 | { |
---|
[876] | 156 | return (identifier == this || (this->parents_.find(identifier) != this->children_->end())); |
---|
[790] | 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
[871] | 160 | @brief Returns true, if the Identifier is exactly of the given type. |
---|
[790] | 161 | @param identifier The identifier to compare with |
---|
| 162 | */ |
---|
[871] | 163 | bool Identifier::isExactlyA(const Identifier* identifier) const |
---|
[790] | 164 | { |
---|
| 165 | return (identifier == this); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
[871] | 169 | @brief Returns true, if the assigned identifier is a child of the given identifier. |
---|
[790] | 170 | @param identifier The identifier to compare with |
---|
| 171 | */ |
---|
| 172 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
| 173 | { |
---|
[876] | 174 | return (this->parents_.find(identifier) != this->children_->end()); |
---|
[871] | 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | @brief Returns true, if the assigned identifier is a direct child of the given identifier. |
---|
| 179 | @param identifier The identifier to compare with |
---|
| 180 | */ |
---|
| 181 | bool Identifier::isDirectChildOf(const Identifier* identifier) const |
---|
| 182 | { |
---|
[876] | 183 | return (this->directParents_.find(identifier) != this->children_->end()); |
---|
[871] | 184 | } |
---|
[790] | 185 | |
---|
| 186 | /** |
---|
[871] | 187 | @brief Returns true, if the assigned identifier is a parent of the given identifier. |
---|
[790] | 188 | @param identifier The identifier to compare with |
---|
| 189 | */ |
---|
| 190 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
| 191 | { |
---|
[876] | 192 | return (this->children_->find(identifier) != this->children_->end()); |
---|
[871] | 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | @brief Returns true, if the assigned identifier is a direct parent of the given identifier. |
---|
| 197 | @param identifier The identifier to compare with |
---|
| 198 | */ |
---|
| 199 | bool Identifier::isDirectParentOf(const Identifier* identifier) const |
---|
| 200 | { |
---|
[876] | 201 | return (this->directChildren_->find(identifier) != this->children_->end()); |
---|
[871] | 202 | } |
---|
| 203 | |
---|
| 204 | /** |
---|
[947] | 205 | @brief Returns the map that stores all Identifiers. |
---|
| 206 | @return The map |
---|
| 207 | */ |
---|
| 208 | std::map<std::string, Identifier*>& Identifier::getIdentifierMapIntern() |
---|
| 209 | { |
---|
| 210 | static std::map<std::string, Identifier*> identifierMap; |
---|
| 211 | return identifierMap; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | /** |
---|
| 215 | @brief Returns the map that stores all Identifiers. |
---|
| 216 | @return The map |
---|
| 217 | */ |
---|
| 218 | std::map<std::string, Identifier*>& Identifier::getLowercaseIdentifierMapIntern() |
---|
| 219 | { |
---|
| 220 | static std::map<std::string, Identifier*> lowercaseIdentifierMap; |
---|
| 221 | return lowercaseIdentifierMap; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
| 225 | @brief Adds the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 226 | @param varname The name of the variablee |
---|
| 227 | @param container The container |
---|
| 228 | */ |
---|
| 229 | void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container) |
---|
| 230 | { |
---|
| 231 | this->bHasConfigValues_ = true; |
---|
| 232 | this->configValues_[varname] = container; |
---|
| 233 | this->configValues_LC_[getLowercase(varname)] = container; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | /** |
---|
[871] | 237 | @brief Returns the ConfigValueContainer of a variable, given by the string of its name. |
---|
| 238 | @param varname The name of the variable |
---|
| 239 | @return The ConfigValueContainer |
---|
| 240 | */ |
---|
| 241 | ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname) |
---|
| 242 | { |
---|
| 243 | std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); |
---|
| 244 | if (it != configValues_.end()) |
---|
| 245 | return ((*it).second); |
---|
| 246 | else |
---|
| 247 | return 0; |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | /** |
---|
[947] | 251 | @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase. |
---|
| 252 | @param varname The name of the variable in lowercase |
---|
| 253 | @return The ConfigValueContainer |
---|
[871] | 254 | */ |
---|
[947] | 255 | ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname) |
---|
[871] | 256 | { |
---|
[947] | 257 | std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname); |
---|
| 258 | if (it != configValues_LC_.end()) |
---|
| 259 | return ((*it).second); |
---|
| 260 | else |
---|
| 261 | return 0; |
---|
[871] | 262 | } |
---|
| 263 | |
---|
[947] | 264 | /** |
---|
| 265 | @brief Adds a new console command of this class. |
---|
| 266 | @param executor The executor of the command |
---|
| 267 | @param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command |
---|
| 268 | @return The executor of the command |
---|
| 269 | */ |
---|
| 270 | ExecutorStatic& Identifier::addConsoleCommand(ExecutorStatic* executor, bool bCreateShortcut) |
---|
| 271 | { |
---|
| 272 | this->bHasConsoleCommands_ = true; |
---|
| 273 | this->consoleCommands_[executor->getName()] = executor; |
---|
| 274 | this->consoleCommands_LC_[getLowercase(executor->getName())] = executor; |
---|
| 275 | |
---|
| 276 | if (bCreateShortcut) |
---|
| 277 | CommandExecutor::addConsoleCommandShortcut(executor); |
---|
| 278 | |
---|
| 279 | return (*executor); |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | @brief Returns the executor of a console command with given name. |
---|
| 284 | @brief name The name of the requested console command |
---|
| 285 | @return The executor of the requested console command |
---|
| 286 | */ |
---|
| 287 | ExecutorStatic* Identifier::getConsoleCommand(const std::string& name) const |
---|
| 288 | { |
---|
| 289 | std::map<std::string, ExecutorStatic*>::const_iterator it = this->consoleCommands_.find(name); |
---|
| 290 | if (it != this->consoleCommands_.end()) |
---|
| 291 | return (*it).second; |
---|
| 292 | else |
---|
| 293 | return 0; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | /** |
---|
| 297 | @brief Returns the executor of a console command with given name in lowercase. |
---|
| 298 | @brief name The name of the requested console command in lowercae |
---|
| 299 | @return The executor of the requested console command |
---|
| 300 | */ |
---|
| 301 | ExecutorStatic* Identifier::getLowercaseConsoleCommand(const std::string& name) const |
---|
| 302 | { |
---|
| 303 | std::map<std::string, ExecutorStatic*>::const_iterator it = this->consoleCommands_LC_.find(name); |
---|
| 304 | if (it != this->consoleCommands_LC_.end()) |
---|
| 305 | return (*it).second; |
---|
| 306 | else |
---|
| 307 | return 0; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | /** |
---|
| 311 | @brief Lists the names of all Identifiers in a std::set<const Identifier*>. |
---|
| 312 | @param out The outstream |
---|
| 313 | @param list The list (or set) of Identifiers |
---|
| 314 | @return The outstream |
---|
| 315 | */ |
---|
[876] | 316 | std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list) |
---|
[871] | 317 | { |
---|
[876] | 318 | for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) |
---|
[871] | 319 | out << (*it)->getName() << " "; |
---|
| 320 | |
---|
| 321 | return out; |
---|
| 322 | } |
---|
[790] | 323 | } |
---|