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