[365] | 1 | /*! |
---|
| 2 | @file Identifier.cc |
---|
| 3 | @brief Implementation of the Identifier class. |
---|
| 4 | */ |
---|
| 5 | |
---|
[197] | 6 | #include "Identifier.h" |
---|
| 7 | |
---|
| 8 | namespace orxonox |
---|
| 9 | { |
---|
| 10 | // ############################### |
---|
| 11 | // ### Identifier ### |
---|
| 12 | // ############################### |
---|
[365] | 13 | int Identifier::hierarchyCreatingCounter_s = 0; // Set the static member variable hierarchyCreatingCounter_s to zero |
---|
| 14 | unsigned int Identifier::classIDcounter_s = 0; // Set the static member variable classIDcounter_s to zero |
---|
[219] | 15 | |
---|
[365] | 16 | /** |
---|
| 17 | @brief Constructor: No factory, no object created, new ObjectList and a unique networkID. |
---|
| 18 | */ |
---|
[197] | 19 | Identifier::Identifier() |
---|
| 20 | { |
---|
| 21 | this->bCreatedOneObject_ = false; |
---|
[244] | 22 | this->factory_ = 0; |
---|
[239] | 23 | |
---|
[243] | 24 | this->children_ = new IdentifierList; |
---|
[362] | 25 | this->classID_ = Identifier::classIDcounter_s++; |
---|
[197] | 26 | } |
---|
| 27 | |
---|
[365] | 28 | /** |
---|
| 29 | @brief Destructor: Deletes the name and the IdentifierList containing the children. |
---|
| 30 | */ |
---|
[197] | 31 | Identifier::~Identifier() |
---|
| 32 | { |
---|
| 33 | delete &this->name_; |
---|
[243] | 34 | delete this->children_; |
---|
[197] | 35 | } |
---|
| 36 | |
---|
[365] | 37 | /** |
---|
| 38 | @brief Initializes the Identifier with an IdentifierList containing all parents of the class the Identifier belongs to. |
---|
| 39 | @param parents The IdentifierList containing all parents |
---|
| 40 | */ |
---|
[239] | 41 | void Identifier::initialize(const IdentifierList* parents) |
---|
[197] | 42 | { |
---|
[231] | 43 | #if HIERARCHY_VERBOSE |
---|
[197] | 44 | std::cout << "*** Initialize " << this->name_ << "-Singleton.\n"; |
---|
[231] | 45 | #endif |
---|
[244] | 46 | this->bCreatedOneObject_ = true; |
---|
| 47 | |
---|
[197] | 48 | if (parents) |
---|
| 49 | { |
---|
[243] | 50 | IdentifierListElement* temp1 = parents->first_; |
---|
[197] | 51 | while (temp1) |
---|
| 52 | { |
---|
[243] | 53 | this->parents_.add(temp1->identifier_); |
---|
[365] | 54 | temp1->identifier_->getChildren().add(this); // We're a child of our parents |
---|
[197] | 55 | |
---|
| 56 | temp1 = temp1->next_; |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
[365] | 61 | /** |
---|
| 62 | @brief Creates an object of the type the Identifier belongs to. |
---|
| 63 | @return The new object |
---|
| 64 | */ |
---|
[244] | 65 | BaseObject* Identifier::fabricate() |
---|
| 66 | { |
---|
| 67 | if (this->factory_) |
---|
| 68 | { |
---|
[365] | 69 | return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type. |
---|
[244] | 70 | } |
---|
| 71 | else |
---|
| 72 | { |
---|
[365] | 73 | // Abstract classes don't have a factory and therefore can't create new objects |
---|
[244] | 74 | std::cout << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract.\n"; |
---|
| 75 | std::cout << "Aborting..."; |
---|
| 76 | abort(); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
[365] | 80 | /** |
---|
| 81 | @brief Sets the networkID to a new value and changes the entry in the Factory. |
---|
| 82 | @param id The new networkID |
---|
| 83 | */ |
---|
[362] | 84 | void Identifier::setNetworkID(unsigned int id) |
---|
| 85 | { |
---|
| 86 | Factory::changeNetworkID(this, this->classID_, id); |
---|
| 87 | this->classID_ = id; |
---|
| 88 | } |
---|
| 89 | |
---|
[365] | 90 | /** |
---|
| 91 | @returns true, if the Identifier is at least of the given type. |
---|
| 92 | @param identifier The identifier to compare with |
---|
| 93 | */ |
---|
[239] | 94 | bool Identifier::isA(const Identifier* identifier) const |
---|
[197] | 95 | { |
---|
[243] | 96 | return (identifier == this || this->parents_.isInList(identifier)); |
---|
[197] | 97 | } |
---|
| 98 | |
---|
[365] | 99 | /** |
---|
| 100 | @returns true, if the Identifier is exactly of the given type. |
---|
| 101 | @param identifier The identifier to compare with |
---|
| 102 | */ |
---|
[239] | 103 | bool Identifier::isDirectlyA(const Identifier* identifier) const |
---|
[197] | 104 | { |
---|
| 105 | return (identifier == this); |
---|
| 106 | } |
---|
| 107 | |
---|
[365] | 108 | /** |
---|
| 109 | @returns true, if the assigned identifier is a child of the given identifier. |
---|
| 110 | @param identifier The identifier to compare with |
---|
| 111 | */ |
---|
[239] | 112 | bool Identifier::isChildOf(const Identifier* identifier) const |
---|
[197] | 113 | { |
---|
[243] | 114 | return this->parents_.isInList(identifier); |
---|
[197] | 115 | } |
---|
| 116 | |
---|
[365] | 117 | /** |
---|
| 118 | @returns true, if the assigned identifier is a parent of the given identifier. |
---|
| 119 | @param identifier The identifier to compare with |
---|
| 120 | */ |
---|
[239] | 121 | bool Identifier::isParentOf(const Identifier* identifier) const |
---|
[197] | 122 | { |
---|
[243] | 123 | return this->children_->isInList(identifier); |
---|
[197] | 124 | } |
---|
| 125 | } |
---|