Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 705 was 698, checked in by landauf, 17 years ago

hopefully replaced all static pre-main variables with more secure wrapper functions. and now i'll sleep.

File size: 5.1 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
35namespace 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    /**
43        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
44    */
45    Identifier::Identifier()
46    {
47        this->bCreatedOneObject_ = false;
48        this->factory_ = 0;
49
50        this->children_ = new IdentifierList;
51
52        // Use a static variable because the classID gets created before main() and that's why we should avoid static member variables
53        static unsigned int classIDcounter_s = 0;
54        this->classID_ = classIDcounter_s++;
55    }
56
57    /**
58        @brief Destructor: Deletes the IdentifierList containing the children.
59    */
60    Identifier::~Identifier()
61    {
62        delete this->children_;
63    }
64
65    /**
66        @brief Initializes the Identifier with an IdentifierList containing all parents of the class the Identifier belongs to.
67        @param parents The IdentifierList containing all parents
68    */
69    void Identifier::initialize(const IdentifierList* parents)
70    {
71        COUT(4) << "*** Initialize " << this->name_ << "-Singleton." << std::endl;
72        this->bCreatedOneObject_ = true;
73
74        if (parents)
75        {
76            IdentifierListElement* temp1 = parents->first_;
77            while (temp1)
78            {
79                this->parents_.add(temp1->identifier_);
80                temp1->identifier_->getChildren().add(this); // We're a child of our parents
81
82                temp1 = temp1->next_;
83            }
84        }
85    }
86
87    /**
88        @brief Creates an object of the type the Identifier belongs to.
89        @return The new object
90    */
91    BaseObject* Identifier::fabricate()
92    {
93        if (this->factory_)
94        {
95            return this->factory_->fabricate(); // We have to return a BaseObject, because we don't know the exact type.
96        }
97        else
98        {
99            // Abstract classes don't have a factory and therefore can't create new objects
100            COUT(1) << "Error: Cannot create an object of type '" << this->name_ << "'. Class is abstract." << std::endl;
101            COUT(1) << "Aborting..." << std::endl;
102            abort();
103            return NULL;
104        }
105    }
106
107    /**
108        @brief Sets the network ID to a new value and changes the entry in the Factory.
109        @param id The new network ID
110    */
111    void Identifier::setNetworkID(unsigned int id)
112    {
113        Factory::changeNetworkID(this, this->classID_, id);
114        this->classID_ = id;
115    }
116
117    /**
118        @returns a reference to the Identifier map, containing all Identifiers.
119    */
120    std::map<std::string, Identifier*>& Identifier::getIdentifierMap()
121    {
122        static std::map<std::string, Identifier*> identifierMapStaticReference = std::map<std::string, Identifier*>();
123        return identifierMapStaticReference;
124    }
125
126    /**
127        @returns true, if the Identifier is at least of the given type.
128        @param identifier The identifier to compare with
129    */
130    bool Identifier::isA(const Identifier* identifier) const
131    {
132        return (identifier == this || this->parents_.isInList(identifier));
133    }
134
135    /**
136        @returns true, if the Identifier is exactly of the given type.
137        @param identifier The identifier to compare with
138    */
139    bool Identifier::isDirectlyA(const Identifier* identifier) const
140    {
141        return (identifier == this);
142    }
143
144    /**
145        @returns true, if the assigned identifier is a child of the given identifier.
146        @param identifier The identifier to compare with
147    */
148    bool Identifier::isChildOf(const Identifier* identifier) const
149    {
150        return this->parents_.isInList(identifier);
151    }
152
153    /**
154        @returns true, if the assigned identifier is a parent of the given identifier.
155        @param identifier The identifier to compare with
156    */
157    bool Identifier::isParentOf(const Identifier* identifier) const
158    {
159        return this->children_->isInList(identifier);
160    }
161}
Note: See TracBrowser for help on using the repository browser.