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