Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core6/src/libraries/core/class/IdentifierManager.cc @ 9646

Last change on this file since 9646 was 9646, checked in by landauf, 11 years ago

implemented new concept to initialize the class hierarchy: each identifier is initialized individually by creating an object of the corresponding class. identifiers of abstract classes must be told explicitly about their direct parents. this allows to build the class tree correctly also for complicated setups like multiple inheritance of abstract classes.

  • Property svn:eol-style set to native
File size: 7.9 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29/**
30    @file
31    @brief Implementation of the Identifier class.
32*/
33
34#include "IdentifierManager.h"
35
36#include <ostream>
37
38#include "util/StringUtils.h"
39#include "core/CoreIncludes.h"
40#include "core/config/ConfigValueContainer.h"
41#include "core/XMLPort.h"
42#include "core/object/ClassFactory.h"
43
44namespace orxonox
45{
46    /* static */ IdentifierManager& IdentifierManager::getInstance()
47    {
48        static IdentifierManager instance;
49        return instance;
50    }
51
52    IdentifierManager::IdentifierManager()
53    {
54        this->hierarchyCreatingCounter_s = 0;
55        this->classIDCounter_s = 0;
56    }
57
58    /**
59        @brief Returns an identifier by name and adds it if not available
60        @param proposal A pointer to a newly created identifier for the case of non existence in the map
61        @return The identifier (unique instance)
62    */
63    Identifier* IdentifierManager::getGloballyUniqueIdentifier(Identifier* proposal)
64    {
65        const std::string& typeidName = proposal->getTypeidName();
66        std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
67
68        if (it != this->identifierByTypeidName_.end())
69        {
70            // There is already an entry: return it
71            return it->second;
72        }
73        else
74        {
75            // There is no entry: put the proposal into the map and return it
76            this->identifierByTypeidName_[typeidName] = proposal;
77            return proposal;
78        }
79    }
80
81    /**
82     * Registers the identifier in all maps of the IdentifierManager.
83     */
84    void IdentifierManager::addIdentifierToLookupMaps(Identifier* identifier)
85    {
86        const std::string& typeidName = identifier->getTypeidName();
87        if (this->identifierByTypeidName_.find(typeidName) != this->identifierByTypeidName_.end())
88        {
89            this->identifierByString_[identifier->getName()] = identifier;
90            this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
91            this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
92        }
93        else
94            orxout(internal_warning) << "Trying to add an identifier to lookup maps which is not known to IdentifierManager" << endl;
95    }
96
97    /**
98        @brief Creates the class-hierarchy by creating and destroying one object of each type.
99    */
100    void IdentifierManager::createClassHierarchy()
101    {
102        orxout(internal_status) << "Create class-hierarchy" << endl;
103        this->startCreatingHierarchy();
104
105        std::set<Identifier*> initializedIdentifiers;
106
107        // iterate over all identifiers, create one instance of each class and initialize the identifiers
108        {
109            Context temporaryContext(NULL);
110            for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
111            {
112                orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << it->second->getName() << ">-Singleton." << endl;
113                // To initialize the identifier, we create a new object and delete it afterwards.
114                if (it->second->hasFactory())
115                {
116                    this->identifiersOfNewObject_.clear();
117                    Identifiable* temp = it->second->fabricate(&temporaryContext);
118                    if (temp->getIdentifier() != it->second)
119                        orxout(internal_error) << "Newly created object has unexpected identifier" << endl;
120                    delete temp;
121
122                    it->second->initializeParents(this->identifiersOfNewObject_);
123                }
124                else
125                    it->second->initializeDirectParentsOfAbstractClass();
126
127                initializedIdentifiers.insert(it->second);
128            }
129        }
130
131        // finish the initialization of all identifiers
132        for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
133        {
134            if (initializedIdentifiers.find(it->second) != initializedIdentifiers.end())
135                it->second->finishInitialization();
136            else
137                orxout(internal_error) << "Identifier was registered late and is not initialized: " << it->second->getName() << " / " << it->second->getTypeidName() << endl;
138        }
139
140        this->stopCreatingHierarchy();
141        orxout(internal_status) << "Finished class-hierarchy creation" << endl;
142    }
143
144    /**
145        @brief Destroys all Identifiers. Called when exiting the program.
146    */
147    void IdentifierManager::destroyAllIdentifiers()
148    {
149        for (std::map<std::string, Identifier*>::iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
150            delete (it->second);
151
152        this->identifierByTypeidName_.clear();
153        this->identifierByString_.clear();
154        this->identifierByLowercaseString_.clear();
155        this->identifierByNetworkId_.clear();
156    }
157
158    /**
159     * @brief Notifies the IdentifierManager about a newly created object while creating the class hierarchy.
160     */
161    void IdentifierManager::createdObject(Identifiable* identifiable)
162    {
163        if (this->isCreatingHierarchy())
164            this->identifiersOfNewObject_.insert(identifiable->getIdentifier());
165        else
166            orxout(internal_warning) << "createdObject() called outside of class hierarchy creation" << endl;
167    }
168
169    /**
170        @brief Returns the Identifier with a given name.
171        @param name The name of the wanted Identifier
172        @return The Identifier
173    */
174    Identifier* IdentifierManager::getIdentifierByString(const std::string& name)
175    {
176        std::map<std::string, Identifier*>::const_iterator it = this->identifierByString_.find(name);
177        if (it != this->identifierByString_.end())
178            return it->second;
179        else
180            return 0;
181    }
182
183    /**
184        @brief Returns the Identifier with a given name in lowercase.
185        @param name The name of the wanted Identifier
186        @return The Identifier
187    */
188    Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name)
189    {
190        std::map<std::string, Identifier*>::const_iterator it = this->identifierByLowercaseString_.find(name);
191        if (it != this->identifierByLowercaseString_.end())
192            return it->second;
193        else
194            return 0;
195    }
196
197    /**
198        @brief Returns the Identifier with a given network ID.
199        @param id The network ID of the wanted Identifier
200        @return The Identifier
201    */
202    Identifier* IdentifierManager::getIdentifierByID(const uint32_t id)
203    {
204        std::map<uint32_t, Identifier*>::const_iterator it = this->identifierByNetworkId_.find(id);
205        if (it != this->identifierByNetworkId_.end())
206            return it->second;
207        else
208            return 0;
209    }
210
211    /**
212        @brief Cleans the NetworkID map (needed on clients for correct initialization)
213    */
214    void IdentifierManager::clearNetworkIDs()
215    {
216        this->identifierByNetworkId_.clear();
217    }
218}
Note: See TracBrowser for help on using the repository browser.