Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/forks/sandbox/src/libraries/core/Identifier.cc @ 8740

Last change on this file since 8740 was 6038, checked in by rgrieder, 15 years ago

Synchronised sandbox with current code trunk. There should be a few bug fixes.

  • Property svn:eol-style set to native
File size: 15.0 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 "Identifier.h"
35
36#include <ostream>
37
38#include "util/StringUtils.h"
39#include "BaseObject.h"
40#include "ConfigValueContainer.h"
41#include "ClassFactory.h"
42
43namespace orxonox
44{
45    // ###############################
46    // ###       Identifier        ###
47    // ###############################
48    int Identifier::hierarchyCreatingCounter_s = 0;
49    unsigned int Identifier::classIDCounter_s = 0;
50
51    /**
52        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
53    */
54    Identifier::Identifier()
55        : classID_(classIDCounter_s++)
56    {
57        this->objects_ = new ObjectListBase(this);
58
59        this->bCreatedOneObject_ = false;
60        this->bSetName_ = false;
61        this->factory_ = 0;
62
63        this->bHasConfigValues_ = false;
64
65    }
66
67    /**
68        @brief Destructor: Deletes the list containing the children.
69    */
70    Identifier::~Identifier()
71    {
72        delete this->objects_;
73
74        if (this->factory_)
75            delete this->factory_;
76
77        for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it)
78            delete (it->second);
79    }
80
81    /**
82        @brief Returns the identifier map with the names as received by typeid(). This is only used internally.
83    */
84    std::map<std::string, Identifier*>& Identifier::getTypeIDIdentifierMap()
85    {
86        static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
87        return identifiers;
88    }
89
90    /**
91        @brief Returns an identifier by name and adds it if not available
92        @param name The name of the identifier as typeid().name() suggests
93        @param proposal A pointer to a newly created identifier for the case of non existance in the map
94        @return The identifier (unique instance)
95    */
96    Identifier* Identifier::getIdentifierSingleton(const std::string& name, Identifier* proposal)
97    {
98        std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);
99
100        if (it != getTypeIDIdentifierMap().end())
101        {
102            // There is already an entry: return it and delete the proposal
103            delete proposal;
104            return it->second;
105        }
106        else
107        {
108            // There is no entry: put the proposal into the map and return it
109            getTypeIDIdentifierMap()[name] = proposal;
110            return proposal;
111        }
112    }
113
114    /**
115        @brief Registers a class, which means that the name and the parents get stored.
116        @param parents A list, containing the Identifiers of all parents of the class
117        @param bRootClass True if the class is either an Interface or the BaseObject itself
118    */
119    void Identifier::initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass)
120    {
121        // Check if at least one object of the given type was created
122        if (!this->bCreatedOneObject_ && Identifier::isCreatingHierarchy())
123        {
124            // If no: We have to store the information and initialize the Identifier
125            COUT(4) << "*** ClassIdentifier: Register Class in " << this->getName() << "-Singleton -> Initialize Singleton." << std::endl;
126            if (bRootClass)
127                this->initialize(0); // If a class is derived from two interfaces, the second interface might think it's derived from the first because of the order of constructor-calls. Thats why we set parents to zero in that case.
128            else
129                this->initialize(parents);
130        }
131    }
132
133    /**
134        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
135        @param parents A list containing all parents
136    */
137    void Identifier::initialize(std::set<const Identifier*>* parents)
138    {
139        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
140        this->bCreatedOneObject_ = true;
141
142        if (parents)
143        {
144            this->parents_ = (*parents);
145            this->directParents_ = (*parents);
146
147            // Iterate through all parents
148            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
149            {
150                // Tell the parent we're one of it's children
151                (*it)->children_.insert((*it)->children_.end(), this);
152
153                // Erase all parents of our parent from our direct-parent-list
154                for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
155                {
156                    // Search for the parent's parent in our direct-parent-list
157                    for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
158                    {
159                        if ((*it1) == (*it2))
160                        {
161                            // We've found a non-direct parent in our list: Erase it
162                            this->directParents_.erase(it2);
163                            break;
164                        }
165                    }
166                }
167            }
168
169            // Now iterate through all direct parents
170            for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
171            {
172                // Tell the parent we're one of it's direct children
173                (*it)->directChildren_.insert((*it)->directChildren_.end(), this);
174            }
175        }
176    }
177
178    /**
179        @brief Creates the class-hierarchy by creating and destroying one object of each type.
180    */
181    void Identifier::createClassHierarchy()
182    {
183        COUT(3) << "*** Identifier: Create class-hierarchy" << std::endl;
184        Identifier::startCreatingHierarchy();
185        for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMap().begin(); it != Identifier::getStringIdentifierMap().end(); ++it)
186        {
187            // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
188            if (it->second->hasFactory())
189            {
190                BaseObject* temp = it->second->fabricate(0);
191                delete temp;
192            }
193        }
194        Identifier::stopCreatingHierarchy();
195        COUT(3) << "*** Identifier: Finished class-hierarchy creation" << std::endl;
196    }
197
198    /**
199        @brief Destroys all Identifiers. Called when exiting the program.
200    */
201    void Identifier::destroyAllIdentifiers()
202    {
203        for (std::map<std::string, Identifier*>::iterator it = Identifier::getTypeIDIdentifierMap().begin(); it != Identifier::getTypeIDIdentifierMap().end(); ++it)
204            delete (it->second);
205    }
206
207    /**
208        @brief Sets the name of the class.
209        @param name The name
210    */
211    void Identifier::setName(const std::string& name)
212    {
213        if (!this->bSetName_)
214        {
215            this->name_ = name;
216            this->bSetName_ = true;
217            Identifier::getStringIdentifierMapIntern()[name] = this;
218            Identifier::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this;
219        }
220    }
221
222    /**
223        @brief Creates an object of the type the Identifier belongs to.
224        @return The new object
225    */
226    BaseObject* Identifier::fabricate(BaseObject* creator)
227    {
228        if (this->factory_)
229        {
230            return this->factory_->fabricate(creator); // We have to return a BaseObject, because we don't know the exact type.
231        }
232        else
233        {
234            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
235            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
236            COUT(1) << "Aborting..." << std::endl;
237            abort();
238            return 0;
239        }
240    }
241
242    /**
243        @brief Returns true, if the Identifier is at least of the given type.
244        @param identifier The identifier to compare with
245    */
246    bool Identifier::isA(const Identifier* identifier) const
247    {
248        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
249    }
250
251    /**
252        @brief Returns true, if the Identifier is exactly of the given type.
253        @param identifier The identifier to compare with
254    */
255    bool Identifier::isExactlyA(const Identifier* identifier) const
256    {
257        return (identifier == this);
258    }
259
260    /**
261        @brief Returns true, if the assigned identifier is a child of the given identifier.
262        @param identifier The identifier to compare with
263    */
264    bool Identifier::isChildOf(const Identifier* identifier) const
265    {
266        return (this->parents_.find(identifier) != this->parents_.end());
267    }
268
269    /**
270        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
271        @param identifier The identifier to compare with
272    */
273    bool Identifier::isDirectChildOf(const Identifier* identifier) const
274    {
275        return (this->directParents_.find(identifier) != this->directParents_.end());
276    }
277
278    /**
279        @brief Returns true, if the assigned identifier is a parent of the given identifier.
280        @param identifier The identifier to compare with
281    */
282    bool Identifier::isParentOf(const Identifier* identifier) const
283    {
284        return (this->children_.find(identifier) != this->children_.end());
285    }
286
287    /**
288        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
289        @param identifier The identifier to compare with
290    */
291    bool Identifier::isDirectParentOf(const Identifier* identifier) const
292    {
293        return (this->directChildren_.find(identifier) != this->directChildren_.end());
294    }
295
296    /**
297        @brief Returns the map that stores all Identifiers with their names.
298        @return The map
299    */
300    std::map<std::string, Identifier*>& Identifier::getStringIdentifierMapIntern()
301    {
302        static std::map<std::string, Identifier*> identifierMap;
303        return identifierMap;
304    }
305
306    /**
307        @brief Returns the map that stores all Identifiers with their names in lowercase.
308        @return The map
309    */
310    std::map<std::string, Identifier*>& Identifier::getLowercaseStringIdentifierMapIntern()
311    {
312        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
313        return lowercaseIdentifierMap;
314    }
315
316    /**
317        @brief Returns the map that stores all Identifiers with their network IDs.
318        @return The map
319    */
320    std::map<uint32_t, Identifier*>& Identifier::getIDIdentifierMapIntern()
321    {
322        static std::map<uint32_t, Identifier*> identifierMap;
323        return identifierMap;
324    }
325
326    /**
327        @brief Returns the Identifier with a given name.
328        @param name The name of the wanted Identifier
329        @return The Identifier
330    */
331    Identifier* Identifier::getIdentifierByString(const std::string& name)
332    {
333        std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapIntern().find(name);
334        if (it != Identifier::getStringIdentifierMapIntern().end())
335            return it->second;
336        else
337            return 0;
338    }
339
340    /**
341        @brief Returns the Identifier with a given name in lowercase.
342        @param name The name of the wanted Identifier
343        @return The Identifier
344    */
345    Identifier* Identifier::getIdentifierByLowercaseString(const std::string& name)
346    {
347        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseStringIdentifierMapIntern().find(name);
348        if (it != Identifier::getLowercaseStringIdentifierMapIntern().end())
349            return it->second;
350        else
351            return 0;
352    }
353
354    /**
355        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
356        @param varname The name of the variablee
357        @param container The container
358    */
359    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
360    {
361        std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname);
362        if (it != this->configValues_.end())
363        {
364            COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << "." << std::endl;
365            delete (it->second);
366        }
367
368        this->bHasConfigValues_ = true;
369        this->configValues_[varname] = container;
370        this->configValues_LC_[getLowercase(varname)] = container;
371    }
372
373    /**
374        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
375        @param varname The name of the variable
376        @return The ConfigValueContainer
377    */
378    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
379    {
380        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
381        if (it != configValues_.end())
382            return it->second;
383        else
384            return 0;
385    }
386
387    /**
388        @brief Returns the ConfigValueContainer of a variable, given by the string of its name in lowercase.
389        @param varname The name of the variable in lowercase
390        @return The ConfigValueContainer
391    */
392    ConfigValueContainer* Identifier::getLowercaseConfigValueContainer(const std::string& varname)
393    {
394        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_LC_.find(varname);
395        if (it != configValues_LC_.end())
396            return it->second;
397        else
398            return 0;
399    }
400
401    /**
402        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
403        @param out The outstream
404        @param list The list (or set) of Identifiers
405        @return The outstream
406    */
407    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
408    {
409        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
410        {
411            if (it != list.begin())
412                out << " ";
413            out << (*it)->getName();
414        }
415
416        return out;
417    }
418}
Note: See TracBrowser for help on using the repository browser.