| 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 IdentifierList; |
|---|
| 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 IdentifierList containing the children. |
|---|
| 60 | */ |
|---|
| 61 | Identifier::~Identifier() |
|---|
| 62 | { |
|---|
| 63 | delete this->children_; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | @brief Initializes the Identifier with an IdentifierList containing all parents of the class the Identifier belongs to. |
|---|
| 68 | @param parents The IdentifierList containing all parents |
|---|
| 69 | */ |
|---|
| 70 | void Identifier::initialize(const IdentifierList* parents) |
|---|
| 71 | { |
|---|
| 72 | COUT(4) << "*** Initialize " << this->name_ << "-Singleton." << std::endl; |
|---|
| 73 | this->bCreatedOneObject_ = true; |
|---|
| 74 | |
|---|
| 75 | if (parents) |
|---|
| 76 | { |
|---|
| 77 | IdentifierListElement* temp1 = parents->first_; |
|---|
| 78 | while (temp1) |
|---|
| 79 | { |
|---|
| 80 | this->parents_.add(temp1->identifier_); |
|---|
| 81 | temp1->identifier_->getChildren().add(this); // We're a child of our parents |
|---|
| 82 | |
|---|
| 83 | temp1 = temp1->next_; |
|---|
| 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) << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract." << std::endl; |
|---|
| 102 | COUT(1) << "Aborting..." << std::endl; |
|---|
| 103 | abort(); |
|---|
| 104 | return NULL; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | /** |
|---|
| 109 | @brief Sets the network ID to a new value and changes the entry in the Factory. |
|---|
| 110 | @param id The new network ID |
|---|
| 111 | */ |
|---|
| 112 | void Identifier::setNetworkID(unsigned int id) |
|---|
| 113 | { |
|---|
| 114 | Factory::changeNetworkID(this, this->classID_, id); |
|---|
| 115 | this->classID_ = id; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | /** |
|---|
| 119 | @returns a reference to the Identifier map, containing all Identifiers. |
|---|
| 120 | */ |
|---|
| 121 | std::map<std::string, Identifier*>& Identifier::getIdentifierMap() |
|---|
| 122 | { |
|---|
| 123 | static std::map<std::string, Identifier*> identifierMapStaticReference = std::map<std::string, Identifier*>(); |
|---|
| 124 | return identifierMapStaticReference; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | @returns true, if the Identifier is at least of the given type. |
|---|
| 129 | @param identifier The identifier to compare with |
|---|
| 130 | */ |
|---|
| 131 | bool Identifier::isA(const Identifier* identifier) const |
|---|
| 132 | { |
|---|
| 133 | return (identifier == this || this->parents_.isInList(identifier)); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | @returns true, if the Identifier is exactly of the given type. |
|---|
| 138 | @param identifier The identifier to compare with |
|---|
| 139 | */ |
|---|
| 140 | bool Identifier::isDirectlyA(const Identifier* identifier) const |
|---|
| 141 | { |
|---|
| 142 | return (identifier == this); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /** |
|---|
| 146 | @returns true, if the assigned identifier is a child of the given identifier. |
|---|
| 147 | @param identifier The identifier to compare with |
|---|
| 148 | */ |
|---|
| 149 | bool Identifier::isChildOf(const Identifier* identifier) const |
|---|
| 150 | { |
|---|
| 151 | return this->parents_.isInList(identifier); |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | @returns true, if the assigned identifier is a parent of the given identifier. |
|---|
| 156 | @param identifier The identifier to compare with |
|---|
| 157 | */ |
|---|
| 158 | bool Identifier::isParentOf(const Identifier* identifier) const |
|---|
| 159 | { |
|---|
| 160 | return this->children_->isInList(identifier); |
|---|
| 161 | } |
|---|
| 162 | } |
|---|