Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core/src/orxonox/core/Identifier.cc @ 811

Last change on this file since 811 was 811, checked in by landauf, 16 years ago
  • Changed the ClassManager/IdentifierDistributor: ClassIdentifiers are now saved with the name returned by typeid(class).name() because this is a simple way getting the name without creating an object (which might be impossible because of non-public constructors or abstract functions).
  • Changed some debug outputs
File size: 4.9 KB
Line 
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
36namespace 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) << "*** Identifier: 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) << "An error occurred in Identifier:" << std::endl;
102            COUT(1) << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract." << std::endl;
103            COUT(1) << "Aborting..." << std::endl;
104            abort();
105            return NULL;
106        }
107    }
108
109    /**
110        @brief Sets the network ID to a new value and changes the entry in the Factory.
111        @param id The new network ID
112    */
113    void Identifier::setNetworkID(unsigned int id)
114    {
115        Factory::changeNetworkID(this, this->classID_, id);
116        this->classID_ = id;
117    }
118
119    /**
120        @returns true, if the Identifier is at least of the given type.
121        @param identifier The identifier to compare with
122    */
123    bool Identifier::isA(const Identifier* identifier) const
124    {
125        return (identifier == this || this->parents_.isInList(identifier));
126    }
127
128    /**
129        @returns true, if the Identifier is exactly of the given type.
130        @param identifier The identifier to compare with
131    */
132    bool Identifier::isDirectlyA(const Identifier* identifier) const
133    {
134        return (identifier == this);
135    }
136
137    /**
138        @returns true, if the assigned identifier is a child of the given identifier.
139        @param identifier The identifier to compare with
140    */
141    bool Identifier::isChildOf(const Identifier* identifier) const
142    {
143        return this->parents_.isInList(identifier);
144    }
145
146    /**
147        @returns true, if the assigned identifier is a parent of the given identifier.
148        @param identifier The identifier to compare with
149    */
150    bool Identifier::isParentOf(const Identifier* identifier) const
151    {
152        return this->children_->isInList(identifier);
153    }
154}
Note: See TracBrowser for help on using the repository browser.