Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/Identifier.cc @ 8332

Last change on this file since 8332 was 8267, checked in by rgrieder, 14 years ago

Fixed typos.

  • Property svn:eol-style set to native
File size: 18.6 KB
RevLine 
[790]1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
[1505]3 *                    > www.orxonox.net <
[790]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
[871]29/**
[2171]30    @file
[790]31    @brief Implementation of the Identifier class.
32*/
33
[1505]34#include "Identifier.h"
35
36#include <ostream>
37
[3280]38#include "util/StringUtils.h"
[1747]39#include "ConfigValueContainer.h"
[5929]40#include "ClassFactory.h"
[5781]41#include "XMLPort.h"
[790]42
43namespace orxonox
44{
45    // ###############################
46    // ###       Identifier        ###
47    // ###############################
[3325]48    int Identifier::hierarchyCreatingCounter_s = 0;
49    unsigned int Identifier::classIDCounter_s = 0;
[790]50
51    /**
52        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
53    */
54    Identifier::Identifier()
[3325]55        : classID_(classIDCounter_s++)
[790]56    {
[1747]57        this->objects_ = new ObjectListBase(this);
58
[790]59        this->bCreatedOneObject_ = false;
[1747]60        this->bSetName_ = false;
[1505]61        this->factory_ = 0;
[5929]62        this->bLoadable_ = false;
[1505]63
64        this->bHasConfigValues_ = false;
[790]65
[5781]66        // Default network ID is the class ID
67        this->networkID_ = this->classID_;
[790]68    }
69
70    /**
[871]71        @brief Destructor: Deletes the list containing the children.
[790]72    */
73    Identifier::~Identifier()
74    {
[1747]75        delete this->objects_;
76
77        if (this->factory_)
78            delete this->factory_;
79
80        for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it)
81            delete (it->second);
[5781]82        for (std::map<std::string, XMLPortParamContainer*>::iterator it = this->xmlportParamContainers_.begin(); it != this->xmlportParamContainers_.end(); ++it)
83            delete (it->second);
84        for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
85            delete (it->second);
[790]86    }
87
88    /**
[2662]89        @brief Returns the identifier map with the names as received by typeid(). This is only used internally.
90    */
91    std::map<std::string, Identifier*>& Identifier::getTypeIDIdentifierMap()
92    {
93        static std::map<std::string, Identifier*> identifiers;    //!< The map to store all Identifiers.
94        return identifiers;
95    }
96
97    /**
[1543]98        @brief Returns an identifier by name and adds it if not available
99        @param name The name of the identifier as typeid().name() suggests
[8267]100        @param proposal A pointer to a newly created identifier for the case of non existence in the map
[1543]101        @return The identifier (unique instance)
102    */
[1747]103    Identifier* Identifier::getIdentifierSingleton(const std::string& name, Identifier* proposal)
[1543]104    {
[2662]105        std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);
[1747]106
[2662]107        if (it != getTypeIDIdentifierMap().end())
[1543]108        {
[1747]109            // There is already an entry: return it and delete the proposal
110            delete proposal;
[5929]111            return it->second;
[1543]112        }
113        else
114        {
[1747]115            // There is no entry: put the proposal into the map and return it
[2662]116            getTypeIDIdentifierMap()[name] = proposal;
[1747]117            return proposal;
[1543]118        }
119    }
120
121    /**
[1856]122        @brief Registers a class, which means that the name and the parents get stored.
123        @param parents A list, containing the Identifiers of all parents of the class
124        @param bRootClass True if the class is either an Interface or the BaseObject itself
125    */
126    void Identifier::initializeClassHierarchy(std::set<const Identifier*>* parents, bool bRootClass)
127    {
128        // Check if at least one object of the given type was created
129        if (!this->bCreatedOneObject_ && Identifier::isCreatingHierarchy())
130        {
[5695]131            // If no: We have to store the information and initialize the Identifier
[1856]132            COUT(4) << "*** ClassIdentifier: Register Class in " << this->getName() << "-Singleton -> Initialize Singleton." << std::endl;
133            if (bRootClass)
134                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.
135            else
136                this->initialize(parents);
137        }
138    }
139
140    /**
[871]141        @brief Initializes the Identifier with a list containing all parents of the class the Identifier belongs to.
142        @param parents A list containing all parents
[790]143    */
[1052]144    void Identifier::initialize(std::set<const Identifier*>* parents)
[790]145    {
[871]146        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
[790]147        this->bCreatedOneObject_ = true;
148
149        if (parents)
[1505]150        {
151            this->parents_ = (*parents);
152            this->directParents_ = (*parents);
153
[871]154            // Iterate through all parents
[1052]155            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
[1505]156            {
157                // Tell the parent we're one of it's children
[5929]158                (*it)->children_.insert((*it)->children_.end(), this);
[1505]159
160                // Erase all parents of our parent from our direct-parent-list
161                for (std::set<const Identifier*>::const_iterator it1 = (*it)->getParents().begin(); it1 != (*it)->getParents().end(); ++it1)
162                {
163                    // Search for the parent's parent in our direct-parent-list
164                    for (std::set<const Identifier*>::iterator it2 = this->directParents_.begin(); it2 != this->directParents_.end(); ++it2)
165                    {
166                        if ((*it1) == (*it2))
167                        {
168                            // We've found a non-direct parent in our list: Erase it
169                            this->directParents_.erase(it2);
170                            break;
171                        }
172                    }
173                }
[790]174            }
[1505]175
176            // Now iterate through all direct parents
177            for (std::set<const Identifier*>::iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
178            {
179                // Tell the parent we're one of it's direct children
[5929]180                (*it)->directChildren_.insert((*it)->directChildren_.end(), this);
[5781]181
182                // Create the super-function dependencies
183                (*it)->createSuperFunctionCaller();
[1505]184            }
[790]185        }
186    }
187
188    /**
[5929]189        @brief Creates the class-hierarchy by creating and destroying one object of each type.
190    */
191    void Identifier::createClassHierarchy()
192    {
193        COUT(3) << "*** Identifier: Create class-hierarchy" << std::endl;
194        Identifier::startCreatingHierarchy();
195        for (std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMap().begin(); it != Identifier::getStringIdentifierMap().end(); ++it)
196        {
197            // To create the new branch of the class-hierarchy, we create a new object and delete it afterwards.
198            if (it->second->hasFactory())
199            {
200                BaseObject* temp = it->second->fabricate(0);
201                temp->destroy();
202            }
203        }
204        Identifier::stopCreatingHierarchy();
205        COUT(3) << "*** Identifier: Finished class-hierarchy creation" << std::endl;
206    }
207
208    /**
[1747]209        @brief Destroys all Identifiers. Called when exiting the program.
210    */
211    void Identifier::destroyAllIdentifiers()
212    {
[2662]213        for (std::map<std::string, Identifier*>::iterator it = Identifier::getTypeIDIdentifierMap().begin(); it != Identifier::getTypeIDIdentifierMap().end(); ++it)
[1747]214            delete (it->second);
215    }
216
217    /**
218        @brief Sets the name of the class.
219    */
220    void Identifier::setName(const std::string& name)
221    {
222        if (!this->bSetName_)
223        {
224            this->name_ = name;
225            this->bSetName_ = true;
[5929]226            Identifier::getStringIdentifierMapIntern()[name] = this;
227            Identifier::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this;
228            Identifier::getIDIdentifierMapIntern()[this->networkID_] = this;
[1747]229        }
230    }
231
232    /**
[790]233        @brief Creates an object of the type the Identifier belongs to.
234        @return The new object
235    */
[2087]236    BaseObject* Identifier::fabricate(BaseObject* creator)
[790]237    {
238        if (this->factory_)
239        {
[2087]240            return this->factory_->fabricate(creator); // We have to return a BaseObject, because we don't know the exact type.
[790]241        }
242        else
243        {
[1505]244            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
[871]245            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
[790]246            COUT(1) << "Aborting..." << std::endl;
247            abort();
[5929]248            return 0;
[790]249        }
250    }
251
252    /**
[5929]253        @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map.
[5781]254    */
255    void Identifier::setNetworkID(uint32_t id)
256    {
[5929]257//        Identifier::getIDIdentifierMapIntern().erase(this->networkID_);
258        Identifier::getIDIdentifierMapIntern()[id] = this;
[5781]259        this->networkID_ = id;
260    }
261
262    /**
[871]263        @brief Returns true, if the Identifier is at least of the given type.
[790]264        @param identifier The identifier to compare with
265    */
266    bool Identifier::isA(const Identifier* identifier) const
267    {
[1059]268        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
[790]269    }
270
271    /**
[871]272        @brief Returns true, if the Identifier is exactly of the given type.
[790]273        @param identifier The identifier to compare with
274    */
[871]275    bool Identifier::isExactlyA(const Identifier* identifier) const
[790]276    {
277        return (identifier == this);
278    }
279
280    /**
[871]281        @brief Returns true, if the assigned identifier is a child of the given identifier.
[790]282        @param identifier The identifier to compare with
283    */
284    bool Identifier::isChildOf(const Identifier* identifier) const
285    {
[1060]286        return (this->parents_.find(identifier) != this->parents_.end());
[1505]287    }
[790]288
289    /**
[1505]290        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
291        @param identifier The identifier to compare with
292    */
293    bool Identifier::isDirectChildOf(const Identifier* identifier) const
294    {
295        return (this->directParents_.find(identifier) != this->directParents_.end());
296    }
297
298    /**
[871]299        @brief Returns true, if the assigned identifier is a parent of the given identifier.
[790]300        @param identifier The identifier to compare with
301    */
302    bool Identifier::isParentOf(const Identifier* identifier) const
303    {
[5929]304        return (this->children_.find(identifier) != this->children_.end());
[1505]305    }
306
307    /**
308        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
309        @param identifier The identifier to compare with
310    */
311    bool Identifier::isDirectParentOf(const Identifier* identifier) const
312    {
[5929]313        return (this->directChildren_.find(identifier) != this->directChildren_.end());
[1505]314    }
315
316    /**
[5929]317        @brief Returns the map that stores all Identifiers with their names.
[1505]318        @return The map
319    */
[5929]320    std::map<std::string, Identifier*>& Identifier::getStringIdentifierMapIntern()
[1505]321    {
322        static std::map<std::string, Identifier*> identifierMap;
323        return identifierMap;
324    }
325
326    /**
[5929]327        @brief Returns the map that stores all Identifiers with their names in lowercase.
[1505]328        @return The map
329    */
[5929]330    std::map<std::string, Identifier*>& Identifier::getLowercaseStringIdentifierMapIntern()
[1505]331    {
332        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
333        return lowercaseIdentifierMap;
334    }
335
336    /**
[5929]337        @brief Returns the map that stores all Identifiers with their network IDs.
338        @return The map
339    */
340    std::map<uint32_t, Identifier*>& Identifier::getIDIdentifierMapIntern()
341    {
342        static std::map<uint32_t, Identifier*> identifierMap;
343        return identifierMap;
344    }
345
346    /**
347        @brief Returns the Identifier with a given name.
348        @param name The name of the wanted Identifier
349        @return The Identifier
350    */
351    Identifier* Identifier::getIdentifierByString(const std::string& name)
352    {
353        std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapIntern().find(name);
354        if (it != Identifier::getStringIdentifierMapIntern().end())
355            return it->second;
356        else
357            return 0;
358    }
359
360    /**
361        @brief Returns the Identifier with a given name in lowercase.
362        @param name The name of the wanted Identifier
363        @return The Identifier
364    */
365    Identifier* Identifier::getIdentifierByLowercaseString(const std::string& name)
366    {
367        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseStringIdentifierMapIntern().find(name);
368        if (it != Identifier::getLowercaseStringIdentifierMapIntern().end())
369            return it->second;
370        else
371            return 0;
372    }
373
374    /**
375        @brief Returns the Identifier with a given network ID.
376        @param id The network ID of the wanted Identifier
377        @return The Identifier
378    */
379    Identifier* Identifier::getIdentifierByID(const uint32_t id)
380    {
381        std::map<uint32_t, Identifier*>::const_iterator it = Identifier::getIDIdentifierMapIntern().find(id);
382        if (it != Identifier::getIDIdentifierMapIntern().end())
383            return it->second;
384        else
385            return 0;
386    }
387
388    /**
389        @brief Cleans the NetworkID map (needed on clients for correct initialization)
390    */
391    void Identifier::clearNetworkIDs()
392    {
393        Identifier::getIDIdentifierMapIntern().clear();
394    }
395
396    /**
[1505]397        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
398        @param varname The name of the variablee
399        @param container The container
400    */
401    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
402    {
403        std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname);
404        if (it != this->configValues_.end())
405        {
[6417]406            COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << std::endl;
[1747]407            delete (it->second);
[1505]408        }
409
410        this->bHasConfigValues_ = true;
411        this->configValues_[varname] = container;
412    }
413
414    /**
415        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
416        @param varname The name of the variable
417        @return The ConfigValueContainer
418    */
419    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
420    {
421        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
422        if (it != configValues_.end())
[5929]423            return it->second;
[1505]424        else
425            return 0;
426    }
427
428    /**
[5781]429        @brief Returns a XMLPortParamContainer that loads a parameter of this class.
430        @param paramname The name of the parameter
431        @return The container
432    */
433    XMLPortParamContainer* Identifier::getXMLPortParamContainer(const std::string& paramname)
434    {
435        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
436        if (it != this->xmlportParamContainers_.end())
[5929]437            return it->second;
[5781]438        else
439            return 0;
440    }
441
442    /**
443        @brief Adds a new XMLPortParamContainer that loads a parameter of this class.
444        @param paramname The name of the parameter
445        @param container The container
446    */
447    void Identifier::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
448    {
449        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
450        if (it != this->xmlportParamContainers_.end())
451        {
[6417]452            COUT(2) << "Warning: Overwriting XMLPortParamContainer in class " << this->getName() << '.' << std::endl;
[5781]453            delete (it->second);
454        }
455
456        this->xmlportParamContainers_[paramname] = container;
457    }
458
459    /**
460        @brief Returns a XMLPortObjectContainer that attaches an object to this class.
461        @param sectionname The name of the section that contains the attachable objects
462        @return The container
463    */
464    XMLPortObjectContainer* Identifier::getXMLPortObjectContainer(const std::string& sectionname)
465    {
466        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
467        if (it != this->xmlportObjectContainers_.end())
[5929]468            return it->second;
[5781]469        else
470            return 0;
471    }
472
473    /**
474        @brief Adds a new XMLPortObjectContainer that attaches an object to this class.
475        @param sectionname The name of the section that contains the attachable objects
476        @param container The container
477    */
478    void Identifier::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
479    {
480        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
481        if (it != this->xmlportObjectContainers_.end())
482        {
[6417]483            COUT(2) << "Warning: Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << std::endl;
[5781]484            delete (it->second);
485        }
486
487        this->xmlportObjectContainers_[sectionname] = container;
488    }
489
490    /**
[1505]491        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
492        @param out The outstream
493        @param list The list (or set) of Identifiers
494        @return The outstream
495    */
496    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
497    {
498        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
[5929]499        {
500            if (it != list.begin())
[6417]501                out << ' ';
[5929]502            out << (*it)->getName();
503        }
[1505]504
505        return out;
506    }
[790]507}
Note: See TracBrowser for help on using the repository browser.