Changeset 1494 for code/branches/network/src/core/Identifier.cc
- Timestamp:
- May 31, 2008, 11:24:44 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/core/Identifier.cc
- Property svn:eol-style set to native
r1446 r1494 1 1 /* 2 2 * ORXONOX - the hottest 3D action shooter ever to exist 3 * > www.orxonox.net < 4 * 3 * > www.orxonox.net < * 5 4 * 6 5 * License notice: … … 31 30 @brief Implementation of the Identifier class. 32 31 */ 33 34 #include "Identifier.h" 35 36 #include <ostream> 37 32 #include "Identifier.h"#include <ostream> 38 33 #include "Factory.h" 39 #include "ConsoleCommand.h" 40 #include "CommandExecutor.h" 41 34 #include "ConsoleCommand.h"#include "CommandExecutor.h" 42 35 namespace orxonox 43 36 { … … 46 39 // ############################### 47 40 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) 48 49 41 /** 50 42 @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. … … 53 45 { 54 46 this->bCreatedOneObject_ = false; 55 this->factory_ = 0; 47 this->factory_ = 0; this->bHasConfigValues_ = false; this->bHasConsoleCommands_ = false; 56 48 57 this->bHasConfigValues_ = false; 58 this->bHasConsoleCommands_ = false; 59 60 this->children_ = new std::set<const Identifier*>(); 61 this->directChildren_ = new std::set<const Identifier*>(); 62 63 // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables 64 static unsigned int classIDcounter_s = 0; 65 this->classID_ = classIDcounter_s++; 66 } 49 this->children_ = new std::set<const Identifier*>(); this->directChildren_ = new std::set<const Identifier*>(); // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables static unsigned int classIDcounter_s = 0; 50 this->classID_ = classIDcounter_s++; } 67 51 68 52 /** … … 71 55 Identifier::~Identifier() 72 56 { 73 delete this->children_; 74 delete this->directChildren_; 75 } 57 delete this->children_; delete this->directChildren_; } 76 58 77 59 /** … … 85 67 86 68 if (parents) 87 { 88 this->parents_ = (*parents); 89 this->directParents_ = (*parents); 90 91 // Iterate through all parents 69 { this->parents_ = (*parents); this->directParents_ = (*parents); // Iterate through all parents 92 70 for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it) 93 { 94 // Tell the parent we're one of it's children 95 (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this); 96 97 // Erase all parents of our parent from our direct-parent-list 98 for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1) 99 { 100 // Search for the parent's parent in our direct-parent-list 101 for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2) 102 { 103 if ((*it1) == (*it2)) 104 { 105 // We've found a non-direct parent in our list: Erase it 106 this->directParents_.erase(it2); 107 break; 108 } 109 } 110 } 111 } 112 113 // Now iterate through all direct parents 114 for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) 115 { 116 // Tell the parent we're one of it's direct children 117 (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this); 118 } 71 { // Tell the parent we're one of it's children (*it)->getChildrenIntern().insert((*it)->getChildrenIntern().end(), this); // Erase all parents of our parent from our direct-parent-list for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1) { // Search for the parent's parent in our direct-parent-list for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2) { if ((*it1) == (*it2)) { // We've found a non-direct parent in our list: Erase it this->directParents_.erase(it2); break; } } } } // Now iterate through all direct parents for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it) { // Tell the parent we're one of it's direct children (*it)->getDirectChildrenIntern().insert((*it)->getDirectChildrenIntern().end(), this); } 119 72 } 120 73 } … … 132 85 else 133 86 { 134 COUT(1) << "An error occurred in Identifier.cc:" << std::endl; 135 COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl; 87 COUT(1) << "An error occurred in Identifier.cc:" << std::endl; COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl; 136 88 COUT(1) << "Aborting..." << std::endl; 137 89 abort(); … … 175 127 { 176 128 return (this->parents_.find(identifier) != this->parents_.end()); 177 } 178 179 /** 180 @brief Returns true, if the assigned identifier is a direct child of the given identifier. 181 @param identifier The identifier to compare with 182 */ 183 bool Identifier::isDirectChildOf(const Identifier* identifier) const 184 { 185 return (this->directParents_.find(identifier) != this->directParents_.end()); 186 } 187 129 } /** @brief Returns true, if the assigned identifier is a direct child of the given identifier. @param identifier The identifier to compare with */ bool Identifier::isDirectChildOf(const Identifier* identifier) const { return (this->directParents_.find(identifier) != this->directParents_.end()); } 188 130 /** 189 131 @brief Returns true, if the assigned identifier is a parent of the given identifier. … … 193 135 { 194 136 return (this->children_->find(identifier) != this->children_->end()); 195 } 196 197 /** 198 @brief Returns true, if the assigned identifier is a direct parent of the given identifier. 199 @param identifier The identifier to compare with 200 */ 201 bool Identifier::isDirectParentOf(const Identifier* identifier) const 202 { 203 return (this->directChildren_->find(identifier) != this->directChildren_->end()); 204 } 205 206 /** 207 @brief Returns the map that stores all Identifiers. 208 @return The map 209 */ 210 std::map<std::string, Identifier*>& Identifier::getIdentifierMapIntern() 211 { 212 static std::map<std::string, Identifier*> identifierMap; 213 return identifierMap; 214 } 215 216 /** 217 @brief Returns the map that stores all Identifiers. 218 @return The map 219 */ 220 std::map<std::string, Identifier*>& Identifier::getLowercaseIdentifierMapIntern() 221 { 222 static std::map<std::string, Identifier*> lowercaseIdentifierMap; 223 return lowercaseIdentifierMap; 224 } 225 226 /** 227 @brief Adds the ConfigValueContainer of a variable, given by the string of its name. 228 @param varname The name of the variablee 229 @param container The container 230 */ 231 void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container) 232 { 233 std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname); 234 if (it != this->configValues_.end()) 235 { 236 COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << "." << std::endl; 237 } 238 239 this->bHasConfigValues_ = true; 240 this->configValues_[varname] = container; 241 this->configValues_LC_[getLowercase(varname)] = container; 242 } 243 244 /** 245 @brief Returns the ConfigValueContainer of a variable, given by the string of its name. 246 @param varname The name of the variable 247 @return The ConfigValueContainer 248 */ 249 ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname) 250 { 251 std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); 252 if (it != configValues_.end()) 253 return ((*it).second); 254 else 255 return 0; 256 } 257 258 /** 259 @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase. 260 @param varname The name of the variable in lowercase 261 @return The ConfigValueContainer 262 */ 263 ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname) 264 { 265 std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname); 266 if (it != configValues_LC_.end()) 267 return ((*it).second); 268 else 269 return 0; 270 } 271 272 /** 273 @brief Adds a new console command of this class. 274 @param executor The executor of the command 275 @param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command 276 @return The executor of the command 277 */ 278 ConsoleCommand& Identifier::addConsoleCommand(ConsoleCommand* command, bool bCreateShortcut) 279 { 280 std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(command->getName()); 281 if (it != this->consoleCommands_.end()) 282 { 283 COUT(2) << "Warning: Overwriting console-command with name " << command->getName() << " in class " << this->getName() << "." << std::endl; 284 } 285 286 this->bHasConsoleCommands_ = true; 287 this->consoleCommands_[command->getName()] = command; 288 this->consoleCommands_LC_[getLowercase(command->getName())] = command; 289 290 if (bCreateShortcut) 291 CommandExecutor::addConsoleCommandShortcut(command); 292 293 return (*command); 294 } 295 296 /** 297 @brief Returns the executor of a console command with given name. 298 @brief name The name of the requested console command 299 @return The executor of the requested console command 300 */ 301 ConsoleCommand* Identifier::getConsoleCommand(const std::string& name) const 302 { 303 std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(name); 304 if (it != this->consoleCommands_.end()) 305 return (*it).second; 306 else 307 return 0; 308 } 309 310 /** 311 @brief Returns the executor of a console command with given name in lowercase. 312 @brief name The name of the requested console command in lowercae 313 @return The executor of the requested console command 314 */ 315 ConsoleCommand* Identifier::getLowercaseConsoleCommand(const std::string& name) const 316 { 317 std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_LC_.find(name); 318 if (it != this->consoleCommands_LC_.end()) 319 return (*it).second; 320 else 321 return 0; 322 } 323 324 /** 325 @brief Lists the names of all Identifiers in a std::set<const Identifier*>. 326 @param out The outstream 327 @param list The list (or set) of Identifiers 328 @return The outstream 329 */ 330 std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list) 331 { 332 for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) 333 out << (*it)->getName() << " "; 334 335 return out; 336 } 337 } 137 } /** @brief Returns true, if the assigned identifier is a direct parent of the given identifier. @param identifier The identifier to compare with */ bool Identifier::isDirectParentOf(const Identifier* identifier) const { return (this->directChildren_->find(identifier) != this->directChildren_->end()); } /** @brief Returns the map that stores all Identifiers. @return The map */ std::map<std::string, Identifier*>& Identifier::getIdentifierMapIntern() { static std::map<std::string, Identifier*> identifierMap; return identifierMap; } /** @brief Returns the map that stores all Identifiers. @return The map */ std::map<std::string, Identifier*>& Identifier::getLowercaseIdentifierMapIntern() { static std::map<std::string, Identifier*> lowercaseIdentifierMap; return lowercaseIdentifierMap; } /** @brief Adds the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variablee @param container The container */ void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container) { std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname); if (it != this->configValues_.end()) { COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << "." << std::endl; } this->bHasConfigValues_ = true; this->configValues_[varname] = container; this->configValues_LC_[getLowercase(varname)] = container; } /** @brief Returns the ConfigValueContainer of a variable, given by the string of its name. @param varname The name of the variable @return The ConfigValueContainer */ ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname) { std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname); if (it != configValues_.end()) return ((*it).second); else return 0; } /** @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase. @param varname The name of the variable in lowercase @return The ConfigValueContainer */ ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname) { std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname); if (it != configValues_LC_.end()) return ((*it).second); else return 0; } /** @brief Adds a new console command of this class. @param executor The executor of the command @param bCreateShortcut If this is true a shortcut gets created so you don't have to add the classname to access this command @return The executor of the command */ ConsoleCommand& Identifier::addConsoleCommand(ConsoleCommand* command, bool bCreateShortcut) { std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(command->getName()); if (it != this->consoleCommands_.end()) { COUT(2) << "Warning: Overwriting console-command with name " << command->getName() << " in class " << this->getName() << "." << std::endl; } this->bHasConsoleCommands_ = true; this->consoleCommands_[command->getName()] = command; this->consoleCommands_LC_[getLowercase(command->getName())] = command; if (bCreateShortcut) CommandExecutor::addConsoleCommandShortcut(command); return (*command); } /** @brief Returns the executor of a console command with given name. @brief name The name of the requested console command @return The executor of the requested console command */ ConsoleCommand* Identifier::getConsoleCommand(const std::string& name) const { std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_.find(name); if (it != this->consoleCommands_.end()) return (*it).second; else return 0; } /** @brief Returns the executor of a console command with given name in lowercase. @brief name The name of the requested console command in lowercae @return The executor of the requested console command */ ConsoleCommand* Identifier::getLowercaseConsoleCommand(const std::string& name) const { std::map<std::string, ConsoleCommand*>::const_iterator it = this->consoleCommands_LC_.find(name); if (it != this->consoleCommands_LC_.end()) return (*it).second; else return 0; } /** @brief Lists the names of all Identifiers in a std::set<const Identifier*>. @param out The outstream @param list The list (or set) of Identifiers @return The outstream */ std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list) { for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it) out << (*it)->getName() << " "; return out; }}
Note: See TracChangeset
for help on using the changeset viewer.