1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < * |
---|
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 | #include "Identifier.h"#include <ostream> |
---|
33 | #include "Factory.h" |
---|
34 | #include "ConsoleCommand.h"#include "CommandExecutor.h" |
---|
35 | namespace orxonox |
---|
36 | { |
---|
37 | // ############################### |
---|
38 | // ### Identifier ### |
---|
39 | // ############################### |
---|
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) |
---|
41 | /** |
---|
42 | @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. |
---|
43 | */ |
---|
44 | Identifier::Identifier() |
---|
45 | { |
---|
46 | this->bCreatedOneObject_ = false; |
---|
47 | this->factory_ = 0; this->bHasConfigValues_ = false; this->bHasConsoleCommands_ = false; |
---|
48 | |
---|
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++; } |
---|
51 | |
---|
52 | /** |
---|
53 | @brief Destructor: Deletes the list containing the children. |
---|
54 | */ |
---|
55 | Identifier::~Identifier() |
---|
56 | { |
---|
57 | delete this->children_; delete this->directChildren_; } |
---|
58 | |
---|
59 | /** |
---|
60 | @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to. |
---|
61 | @param parents A list containing all parents |
---|
62 | */ |
---|
63 | void Identifier::initialize(std::set<const Identifier*>* parents) |
---|
64 | { |
---|
65 | COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl; |
---|
66 | this->bCreatedOneObject_ = true; |
---|
67 | |
---|
68 | if (parents) |
---|
69 | { this->parents_ = (*parents); this->directParents_ = (*parents); // Iterate through all parents |
---|
70 | for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it) |
---|
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); } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | @brief Creates an object of the type the Identifier belongs to. |
---|
77 | @return The new object |
---|
78 | */ |
---|
79 | BaseObject* Identifier::fabricate() |
---|
80 | { |
---|
81 | if (this->factory_) |
---|
82 | { |
---|
83 | return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type. |
---|
84 | } |
---|
85 | else |
---|
86 | { |
---|
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; |
---|
88 | COUT(1) << "Aborting..." << std::endl; |
---|
89 | abort(); |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | @brief Sets the network ID to a new value and changes the entry in the Factory. |
---|
96 | @param id The new network ID |
---|
97 | */ |
---|
98 | void Identifier::setNetworkID(unsigned int id) |
---|
99 | { |
---|
100 | Factory::changeNetworkID(this, this->classID_, id); |
---|
101 | this->classID_ = id; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | @brief Returns true, if the Identifier is at least of the given type. |
---|
106 | @param identifier The identifier to compare with |
---|
107 | */ |
---|
108 | bool Identifier::isA(const Identifier* identifier) const |
---|
109 | { |
---|
110 | return (identifier == this || (this->parents_.find(identifier) != this->parents_.end())); |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | @brief Returns true, if the Identifier is exactly of the given type. |
---|
115 | @param identifier The identifier to compare with |
---|
116 | */ |
---|
117 | bool Identifier::isExactlyA(const Identifier* identifier) const |
---|
118 | { |
---|
119 | return (identifier == this); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | @brief Returns true, if the assigned identifier is a child of the given identifier. |
---|
124 | @param identifier The identifier to compare with |
---|
125 | */ |
---|
126 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
127 | { |
---|
128 | return (this->parents_.find(identifier) != this->parents_.end()); |
---|
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()); } |
---|
130 | /** |
---|
131 | @brief Returns true, if the assigned identifier is a parent of the given identifier. |
---|
132 | @param identifier The identifier to compare with |
---|
133 | */ |
---|
134 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
135 | { |
---|
136 | return (this->children_->find(identifier) != this->children_->end()); |
---|
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; }} |
---|