Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/class/IdentifierManager.cc @ 10481

Last change on this file since 10481 was 10481, checked in by landauf, 9 years ago

StaticallyInitializedIdentifier is now responsible to register and unregister the assigned identifier.

  • Property svn:eol-style set to native
File size: 11.3 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/Core.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        this->recordTraceForIdentifier_ = NULL;
57    }
58
59    /**
60     * Registers the identifier in all maps of the IdentifierManager.
61     */
62    void IdentifierManager::addIdentifier(Identifier* identifier)
63    {
64        orxout(verbose, context::identifier) << "Adding identifier for " << identifier->getName() << " / " << identifier->getTypeInfo().name() << endl;
65
66        this->identifiers_.insert(identifier);
67        this->identifierByString_[identifier->getName()] = identifier;
68        this->identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
69        this->identifierByNetworkId_[identifier->getNetworkID()] = identifier;
70    }
71
72    /**
73     * Unregisters the identifier from all maps of the IdentifierManager.
74     */
75    void IdentifierManager::removeIdentifier(Identifier* identifier)
76    {
77        this->identifiers_.erase(identifier);
78        this->identifierByString_.erase(identifier->getName());
79        this->identifierByLowercaseString_.erase(getLowercase(identifier->getName()));
80        this->identifierByNetworkId_.erase(identifier->getNetworkID());
81    }
82
83    /**
84        @brief Creates the class-hierarchy by creating and destroying one object of each type.
85    */
86    void IdentifierManager::createClassHierarchy()
87    {
88        orxout(internal_status) << "Create class-hierarchy" << endl;
89        this->startCreatingHierarchy();
90
91        std::set<Identifier*> initializedIdentifiers;
92
93        // ensure root context exists before starting to create objects. if the root context is dynamically created while creating the class hierarchy, we
94        // would mistakenly assume the class of the currently created object inherits from Context
95        Context::getRootContext();
96
97        // iterate over all identifiers, create one instance of each class and initialize the identifiers
98        {
99            Context temporaryContext(NULL);
100            for (std::set<Identifier*>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
101            {
102                Identifier* identifier = (*it);
103
104                orxout(verbose, context::identifier) << "Initialize ClassIdentifier<" << identifier->getName() << ">-Singleton." << endl;
105                // To initialize the identifier, we create a new object and delete it afterwards.
106                if (identifier->hasFactory())
107                {
108                    this->identifierTraceOfNewObject_.clear();
109                    this->recordTraceForIdentifier_ = identifier;
110
111                    Identifiable* temp = identifier->fabricate(&temporaryContext);
112
113                    this->recordTraceForIdentifier_ = NULL;
114
115                    if (temp->getIdentifier() != identifier)
116                        orxout(internal_error) << "Newly created object of type " << identifier->getName() << " has unexpected identifier. Did you forget to use RegisterObject(classname)?" << endl;
117
118                    identifier->initializeParents(this->identifierTraceOfNewObject_[temp]);
119
120                    delete temp;
121                }
122
123                initializedIdentifiers.insert(identifier);
124            }
125
126            size_t numberOfObjects = temporaryContext.getObjectList<Listable>()->size();
127            if (numberOfObjects > 0)
128                orxout(internal_warning) << "There are still " << numberOfObjects << " listables left after creating the class hierarchy" << endl;
129        }
130
131        // finish the initialization of all identifiers
132        for (std::set<Identifier*>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
133        {
134            Identifier* identifier = (*it);
135
136            if (initializedIdentifiers.find(identifier) != initializedIdentifiers.end())
137                identifier->finishInitialization();
138            else
139                orxout(internal_error) << "Identifier was registered late and is not initialized: " << identifier->getName() << " / " << identifier->getTypeInfo().name() << endl;
140        }
141
142        // only check class hierarchy in dev mode because it's an expensive operation and it requires a developer to fix detected problems anyway.
143        if (!Core::exists() || Core::getInstance().getConfig()->inDevMode())
144            this->verifyClassHierarchy();
145
146        this->stopCreatingHierarchy();
147        orxout(internal_status) << "Finished class-hierarchy creation" << endl;
148    }
149
150    /**
151     * Verifies if the class hierarchy is consistent with the RTTI.
152     */
153    void IdentifierManager::verifyClassHierarchy()
154    {
155        Context temporaryContext(NULL);
156        for (std::set<Identifier*>::const_iterator it1 = this->identifiers_.begin(); it1 != this->identifiers_.end(); ++it1)
157        {
158            if (!(*it1)->hasFactory())
159                continue;
160
161            Identifiable* temp = (*it1)->fabricate(&temporaryContext);
162
163            for (std::set<Identifier*>::const_iterator it2 = this->identifiers_.begin(); it2 != this->identifiers_.end(); ++it2)
164            {
165                bool isA_AccordingToRtti = (*it2)->canDynamicCastObjectToIdentifierClass(temp);
166                bool isA_AccordingToClassHierarchy = temp->isA((*it2));
167
168                if (isA_AccordingToRtti != isA_AccordingToClassHierarchy)
169                {
170                    orxout(internal_error) << "Class hierarchy does not match RTTI: Class hierarchy claims that " << (*it1)->getName() <<
171                        (isA_AccordingToClassHierarchy ? " is a " : " is not a ") << (*it2)->getName() << " but RTTI says the opposite." << endl;
172                }
173            }
174
175            delete temp;
176        }
177        orxout(internal_info) << "Class hierarchy matches RTTI" << endl;
178
179        size_t numberOfObjects = temporaryContext.getObjectList<Listable>()->size();
180        if (numberOfObjects > 0)
181            orxout(internal_warning) << "There are still " << numberOfObjects << " listables left after creating the class hierarchy" << endl;
182    }
183
184    /**
185     * @brief Resets all Identifiers.
186     */
187    void IdentifierManager::destroyClassHierarchy()
188    {
189        orxout(internal_status) << "Destroy class-hierarchy" << endl;
190        for (std::set<Identifier*>::const_iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
191            (*it)->reset();
192    }
193
194    /**
195     * @brief Notifies the IdentifierManager about a newly created object while creating the class hierarchy.
196     */
197    void IdentifierManager::createdObject(Identifiable* identifiable)
198    {
199        if (this->isCreatingHierarchy())
200        {
201            if (this->recordTraceForIdentifier_)
202            {
203                std::list<const Identifier*>& traceForObject = this->identifierTraceOfNewObject_[identifiable];
204                if (std::find(traceForObject.begin(), traceForObject.end(), identifiable->getIdentifier()) != traceForObject.end())
205                {
206                    orxout(internal_warning) << this->recordTraceForIdentifier_->getName() << " inherits two times from " <<
207                        identifiable->getIdentifier()->getName() << ". Did you forget to use virtual inheritance?" << endl;
208                }
209                traceForObject.push_back(identifiable->getIdentifier());
210            }
211        }
212        else
213            orxout(internal_warning) << "createdObject() called outside of class hierarchy creation" << endl;
214    }
215
216    /**
217        @brief Returns the Identifier with a given name.
218        @param name The name of the wanted Identifier
219        @return The Identifier
220    */
221    Identifier* IdentifierManager::getIdentifierByString(const std::string& name)
222    {
223        std::map<std::string, Identifier*>::const_iterator it = this->identifierByString_.find(name);
224        if (it != this->identifierByString_.end())
225            return it->second;
226        else
227            return 0;
228    }
229
230    /**
231        @brief Returns the Identifier with a given name in lowercase.
232        @param name The name of the wanted Identifier
233        @return The Identifier
234    */
235    Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name)
236    {
237        std::map<std::string, Identifier*>::const_iterator it = this->identifierByLowercaseString_.find(name);
238        if (it != this->identifierByLowercaseString_.end())
239            return it->second;
240        else
241            return 0;
242    }
243
244    /**
245        @brief Returns the Identifier with a given network ID.
246        @param id The network ID of the wanted Identifier
247        @return The Identifier
248    */
249    Identifier* IdentifierManager::getIdentifierByID(const uint32_t id)
250    {
251        std::map<uint32_t, Identifier*>::const_iterator it = this->identifierByNetworkId_.find(id);
252        if (it != this->identifierByNetworkId_.end())
253            return it->second;
254        else
255            return 0;
256    }
257
258    /**
259        @brief Returns the Identifier with a given typeid-name.
260        @param name The typeid-name of the wanted Identifier
261        @return The Identifier
262    */
263    Identifier* IdentifierManager::getIdentifierByTypeInfo(const std::type_info& typeInfo)
264    {
265        // TODO: use std::type_index and a map to find identifiers by type_info (only with c++11)
266        for (std::set<Identifier*>::iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
267            if ((*it)->getTypeInfo() == typeInfo)
268                return (*it);
269        return 0;
270    }
271
272    /**
273        @brief Cleans the NetworkID map (needed on clients for correct initialization)
274    */
275    void IdentifierManager::clearNetworkIDs()
276    {
277        this->identifierByNetworkId_.clear();
278    }
279
280    /**
281        @brief Destroys all Identifiers. Called when exiting the program.
282    */
283    void IdentifierManager::destroyAllIdentifiers()
284    {
285        for (std::set<Identifier*>::iterator it = this->identifiers_.begin(); it != this->identifiers_.end(); ++it)
286            delete (*it);
287
288        this->identifiers_.clear();
289        this->identifierByString_.clear();
290        this->identifierByLowercaseString_.clear();
291        this->identifierByNetworkId_.clear();
292    }
293}
Note: See TracBrowser for help on using the repository browser.