Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7284 was 7284, checked in by landauf, 14 years ago

merged consolecommands3 branch back to trunk.

note: the console command interface has changed completely, but the documentation is not yet up to date. just copy an existing command and change it to your needs, it's pretty self-explanatory. also the include files related to console commands are now located in core/command/. in the game it should work exactly like before, except for some changes in the auto-completion.

  • 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
100        @param proposal A pointer to a newly created identifier for the case of non existance in the map
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        @param name The name
220    */
221    void Identifier::setName(const std::string& name)
222    {
223        if (!this->bSetName_)
224        {
225            this->name_ = name;
226            this->bSetName_ = true;
[5929]227            Identifier::getStringIdentifierMapIntern()[name] = this;
228            Identifier::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this;
229            Identifier::getIDIdentifierMapIntern()[this->networkID_] = this;
[1747]230        }
231    }
232
233    /**
[790]234        @brief Creates an object of the type the Identifier belongs to.
235        @return The new object
236    */
[2087]237    BaseObject* Identifier::fabricate(BaseObject* creator)
[790]238    {
239        if (this->factory_)
240        {
[2087]241            return this->factory_->fabricate(creator); // We have to return a BaseObject, because we don't know the exact type.
[790]242        }
243        else
244        {
[1505]245            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
[871]246            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
[790]247            COUT(1) << "Aborting..." << std::endl;
248            abort();
[5929]249            return 0;
[790]250        }
251    }
252
253    /**
[5929]254        @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map.
[5781]255        @param id The new network ID
256    */
257    void Identifier::setNetworkID(uint32_t id)
258    {
[5929]259//        Identifier::getIDIdentifierMapIntern().erase(this->networkID_);
260        Identifier::getIDIdentifierMapIntern()[id] = this;
[5781]261        this->networkID_ = id;
262    }
263
264    /**
[871]265        @brief Returns true, if the Identifier is at least of the given type.
[790]266        @param identifier The identifier to compare with
267    */
268    bool Identifier::isA(const Identifier* identifier) const
269    {
[1059]270        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
[790]271    }
272
273    /**
[871]274        @brief Returns true, if the Identifier is exactly of the given type.
[790]275        @param identifier The identifier to compare with
276    */
[871]277    bool Identifier::isExactlyA(const Identifier* identifier) const
[790]278    {
279        return (identifier == this);
280    }
281
282    /**
[871]283        @brief Returns true, if the assigned identifier is a child of the given identifier.
[790]284        @param identifier The identifier to compare with
285    */
286    bool Identifier::isChildOf(const Identifier* identifier) const
287    {
[1060]288        return (this->parents_.find(identifier) != this->parents_.end());
[1505]289    }
[790]290
291    /**
[1505]292        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
293        @param identifier The identifier to compare with
294    */
295    bool Identifier::isDirectChildOf(const Identifier* identifier) const
296    {
297        return (this->directParents_.find(identifier) != this->directParents_.end());
298    }
299
300    /**
[871]301        @brief Returns true, if the assigned identifier is a parent of the given identifier.
[790]302        @param identifier The identifier to compare with
303    */
304    bool Identifier::isParentOf(const Identifier* identifier) const
305    {
[5929]306        return (this->children_.find(identifier) != this->children_.end());
[1505]307    }
308
309    /**
310        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
311        @param identifier The identifier to compare with
312    */
313    bool Identifier::isDirectParentOf(const Identifier* identifier) const
314    {
[5929]315        return (this->directChildren_.find(identifier) != this->directChildren_.end());
[1505]316    }
317
318    /**
[5929]319        @brief Returns the map that stores all Identifiers with their names.
[1505]320        @return The map
321    */
[5929]322    std::map<std::string, Identifier*>& Identifier::getStringIdentifierMapIntern()
[1505]323    {
324        static std::map<std::string, Identifier*> identifierMap;
325        return identifierMap;
326    }
327
328    /**
[5929]329        @brief Returns the map that stores all Identifiers with their names in lowercase.
[1505]330        @return The map
331    */
[5929]332    std::map<std::string, Identifier*>& Identifier::getLowercaseStringIdentifierMapIntern()
[1505]333    {
334        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
335        return lowercaseIdentifierMap;
336    }
337
338    /**
[5929]339        @brief Returns the map that stores all Identifiers with their network IDs.
340        @return The map
341    */
342    std::map<uint32_t, Identifier*>& Identifier::getIDIdentifierMapIntern()
343    {
344        static std::map<uint32_t, Identifier*> identifierMap;
345        return identifierMap;
346    }
347
348    /**
349        @brief Returns the Identifier with a given name.
350        @param name The name of the wanted Identifier
351        @return The Identifier
352    */
353    Identifier* Identifier::getIdentifierByString(const std::string& name)
354    {
355        std::map<std::string, Identifier*>::const_iterator it = Identifier::getStringIdentifierMapIntern().find(name);
356        if (it != Identifier::getStringIdentifierMapIntern().end())
357            return it->second;
358        else
359            return 0;
360    }
361
362    /**
363        @brief Returns the Identifier with a given name in lowercase.
364        @param name The name of the wanted Identifier
365        @return The Identifier
366    */
367    Identifier* Identifier::getIdentifierByLowercaseString(const std::string& name)
368    {
369        std::map<std::string, Identifier*>::const_iterator it = Identifier::getLowercaseStringIdentifierMapIntern().find(name);
370        if (it != Identifier::getLowercaseStringIdentifierMapIntern().end())
371            return it->second;
372        else
373            return 0;
374    }
375
376    /**
377        @brief Returns the Identifier with a given network ID.
378        @param id The network ID of the wanted Identifier
379        @return The Identifier
380    */
381    Identifier* Identifier::getIdentifierByID(const uint32_t id)
382    {
383        std::map<uint32_t, Identifier*>::const_iterator it = Identifier::getIDIdentifierMapIntern().find(id);
384        if (it != Identifier::getIDIdentifierMapIntern().end())
385            return it->second;
386        else
387            return 0;
388    }
389
390    /**
391        @brief Cleans the NetworkID map (needed on clients for correct initialization)
392    */
393    void Identifier::clearNetworkIDs()
394    {
395        Identifier::getIDIdentifierMapIntern().clear();
396    }
397
398    /**
[1505]399        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
400        @param varname The name of the variablee
401        @param container The container
402    */
403    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
404    {
405        std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname);
406        if (it != this->configValues_.end())
407        {
[6417]408            COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << std::endl;
[1747]409            delete (it->second);
[1505]410        }
411
412        this->bHasConfigValues_ = true;
413        this->configValues_[varname] = container;
414    }
415
416    /**
417        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
418        @param varname The name of the variable
419        @return The ConfigValueContainer
420    */
421    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
422    {
423        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
424        if (it != configValues_.end())
[5929]425            return it->second;
[1505]426        else
427            return 0;
428    }
429
430    /**
[5781]431        @brief Returns a XMLPortParamContainer that loads a parameter of this class.
432        @param paramname The name of the parameter
433        @return The container
434    */
435    XMLPortParamContainer* Identifier::getXMLPortParamContainer(const std::string& paramname)
436    {
437        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
438        if (it != this->xmlportParamContainers_.end())
[5929]439            return it->second;
[5781]440        else
441            return 0;
442    }
443
444    /**
445        @brief Adds a new XMLPortParamContainer that loads a parameter of this class.
446        @param paramname The name of the parameter
447        @param container The container
448    */
449    void Identifier::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
450    {
451        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
452        if (it != this->xmlportParamContainers_.end())
453        {
[6417]454            COUT(2) << "Warning: Overwriting XMLPortParamContainer in class " << this->getName() << '.' << std::endl;
[5781]455            delete (it->second);
456        }
457
458        this->xmlportParamContainers_[paramname] = container;
459    }
460
461    /**
462        @brief Returns a XMLPortObjectContainer that attaches an object to this class.
463        @param sectionname The name of the section that contains the attachable objects
464        @return The container
465    */
466    XMLPortObjectContainer* Identifier::getXMLPortObjectContainer(const std::string& sectionname)
467    {
468        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
469        if (it != this->xmlportObjectContainers_.end())
[5929]470            return it->second;
[5781]471        else
472            return 0;
473    }
474
475    /**
476        @brief Adds a new XMLPortObjectContainer that attaches an object to this class.
477        @param sectionname The name of the section that contains the attachable objects
478        @param container The container
479    */
480    void Identifier::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
481    {
482        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
483        if (it != this->xmlportObjectContainers_.end())
484        {
[6417]485            COUT(2) << "Warning: Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << std::endl;
[5781]486            delete (it->second);
487        }
488
489        this->xmlportObjectContainers_[sectionname] = container;
490    }
491
492    /**
[1505]493        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
494        @param out The outstream
495        @param list The list (or set) of Identifiers
496        @return The outstream
497    */
498    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
499    {
500        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
[5929]501        {
502            if (it != list.begin())
[6417]503                out << ' ';
[5929]504            out << (*it)->getName();
505        }
[1505]506
507        return out;
508    }
[790]509}
Note: See TracBrowser for help on using the repository browser.