Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

small refactoring

  • Property svn:eol-style set to native
File size: 5.8 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/config/ConfigValueContainer.h"
40#include "core/XMLPort.h"
41#include "core/object/ClassFactory.h"
42
43namespace orxonox
44{
45    /* static */ IdentifierManager& IdentifierManager::getInstance()
46    {
47        static IdentifierManager instance;
48        return instance;
49    }
50
51    IdentifierManager::IdentifierManager()
52    {
53        this->hierarchyCreatingCounter_s = 0;
54        this->classIDCounter_s = 0;
55    }
56
57    /**
58        @brief Returns an identifier by name and adds it if not available
59        @param proposal A pointer to a newly created identifier for the case of non existence in the map
60        @return The identifier (unique instance)
61    */
62    Identifier* IdentifierManager::getIdentifierSingleton(Identifier* proposal)
63    {
64        const std::string& typeidName = proposal->getTypeidName();
65        std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.find(typeidName);
66
67        if (it != this->identifierByTypeidName_.end())
68        {
69            // There is already an entry: return it
70            return it->second;
71        }
72        else
73        {
74            // There is no entry: put the proposal into the map and return it
75            this->identifierByTypeidName_[typeidName] = proposal;
76            return proposal;
77        }
78    }
79
80    /**
81     * Registers the identifier in all maps of the IdentifierManager.
82     */
83    void IdentifierManager::registerIdentifier(Identifier* identifier)
84    {
85        IdentifierManager::getInstance().identifierByString_[identifier->getName()] = identifier;
86        IdentifierManager::getInstance().identifierByLowercaseString_[getLowercase(identifier->getName())] = identifier;
87        IdentifierManager::getInstance().identifierByNetworkId_[identifier->getNetworkID()] = identifier;
88    }
89
90    /**
91        @brief Creates the class-hierarchy by creating and destroying one object of each type.
92    */
93    void IdentifierManager::createClassHierarchy()
94    {
95        orxout(internal_status) << "Create class-hierarchy" << endl;
96        this->startCreatingHierarchy();
97        for (std::map<std::string, Identifier*>::const_iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
98        {
99            // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
100            if (it->second->hasFactory())
101            {
102                Identifiable* temp = it->second->fabricate(0);
103                delete temp;
104            }
105        }
106        this->stopCreatingHierarchy();
107        orxout(internal_status) << "Finished class-hierarchy creation" << endl;
108    }
109
110    /**
111        @brief Destroys all Identifiers. Called when exiting the program.
112    */
113    void IdentifierManager::destroyAllIdentifiers()
114    {
115        for (std::map<std::string, Identifier*>::iterator it = this->identifierByTypeidName_.begin(); it != this->identifierByTypeidName_.end(); ++it)
116            delete (it->second);
117
118        this->identifierByTypeidName_.clear();
119        this->identifierByString_.clear();
120        this->identifierByLowercaseString_.clear();
121        this->identifierByNetworkId_.clear();
122    }
123
124    /**
125        @brief Returns the Identifier with a given name.
126        @param name The name of the wanted Identifier
127        @return The Identifier
128    */
129    Identifier* IdentifierManager::getIdentifierByString(const std::string& name)
130    {
131        std::map<std::string, Identifier*>::const_iterator it = this->identifierByString_.find(name);
132        if (it != this->identifierByString_.end())
133            return it->second;
134        else
135            return 0;
136    }
137
138    /**
139        @brief Returns the Identifier with a given name in lowercase.
140        @param name The name of the wanted Identifier
141        @return The Identifier
142    */
143    Identifier* IdentifierManager::getIdentifierByLowercaseString(const std::string& name)
144    {
145        std::map<std::string, Identifier*>::const_iterator it = this->identifierByLowercaseString_.find(name);
146        if (it != this->identifierByLowercaseString_.end())
147            return it->second;
148        else
149            return 0;
150    }
151
152    /**
153        @brief Returns the Identifier with a given network ID.
154        @param id The network ID of the wanted Identifier
155        @return The Identifier
156    */
157    Identifier* IdentifierManager::getIdentifierByID(const uint32_t id)
158    {
159        std::map<uint32_t, Identifier*>::const_iterator it = this->identifierByNetworkId_.find(id);
160        if (it != this->identifierByNetworkId_.end())
161            return it->second;
162        else
163            return 0;
164    }
165
166    /**
167        @brief Cleans the NetworkID map (needed on clients for correct initialization)
168    */
169    void IdentifierManager::clearNetworkIDs()
170    {
171        this->identifierByNetworkId_.clear();
172    }
173}
Note: See TracBrowser for help on using the repository browser.