Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core5/src/libraries/core/Identifier.cc @ 5821

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

Fixed 3 memory leaks (one of them was a true even a true one ;))

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