Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/doc/src/libraries/core/Identifier.cc @ 7373

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

enhanced documentation of some core classes and added examples

  • Property svn:eol-style set to native
File size: 18.6 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 "ClassFactory.h"
41#include "XMLPort.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        this->bLoadable_ = false;
63
64        this->bHasConfigValues_ = false;
65
66        // Default network ID is the class ID
67        this->networkID_ = this->classID_;
68    }
69
70    /**
71        @brief Destructor: Deletes the list containing the children.
72    */
73    Identifier::~Identifier()
74    {
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);
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);
86    }
87
88    /**
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    /**
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    */
103    Identifier* Identifier::getIdentifierSingleton(const std::string& name, Identifier* proposal)
104    {
105        std::map<std::string, Identifier*>::const_iterator it = getTypeIDIdentifierMap().find(name);
106
107        if (it != getTypeIDIdentifierMap().end())
108        {
109            // There is already an entry: return it and delete the proposal
110            delete proposal;
111            return it->second;
112        }
113        else
114        {
115            // There is no entry: put the proposal into the map and return it
116            getTypeIDIdentifierMap()[name] = proposal;
117            return proposal;
118        }
119    }
120
121    /**
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        {
131            // If no: We have to store the information and initialize the Identifier
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    /**
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
143    */
144    void Identifier::initialize(std::set<const Identifier*>* parents)
145    {
146        COUT(4) << "*** Identifier: Initialize " << this->name_ << "-Singleton." << std::endl;
147        this->bCreatedOneObject_ = true;
148
149        if (parents)
150        {
151            this->parents_ = (*parents);
152            this->directParents_ = (*parents);
153
154            // Iterate through all parents
155            for (std::set<const Identifier*>::iterator it = parents->begin(); it != parents->end(); ++it)
156            {
157                // Tell the parent we're one of it's children
158                (*it)->children_.insert((*it)->children_.end(), this);
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                }
174            }
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
180                (*it)->directChildren_.insert((*it)->directChildren_.end(), this);
181
182                // Create the super-function dependencies
183                (*it)->createSuperFunctionCaller();
184            }
185        }
186    }
187
188    /**
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    /**
209        @brief Destroys all Identifiers. Called when exiting the program.
210    */
211    void Identifier::destroyAllIdentifiers()
212    {
213        for (std::map<std::string, Identifier*>::iterator it = Identifier::getTypeIDIdentifierMap().begin(); it != Identifier::getTypeIDIdentifierMap().end(); ++it)
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;
226            Identifier::getStringIdentifierMapIntern()[name] = this;
227            Identifier::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this;
228            Identifier::getIDIdentifierMapIntern()[this->networkID_] = this;
229        }
230    }
231
232    /**
233        @brief Creates an object of the type the Identifier belongs to.
234        @return The new object
235    */
236    BaseObject* Identifier::fabricate(BaseObject* creator)
237    {
238        if (this->factory_)
239        {
240            return this->factory_->fabricate(creator); // We have to return a BaseObject, because we don't know the exact type.
241        }
242        else
243        {
244            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
245            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
246            COUT(1) << "Aborting..." << std::endl;
247            abort();
248            return 0;
249        }
250    }
251
252    /**
253        @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map.
254    */
255    void Identifier::setNetworkID(uint32_t id)
256    {
257//        Identifier::getIDIdentifierMapIntern().erase(this->networkID_);
258        Identifier::getIDIdentifierMapIntern()[id] = this;
259        this->networkID_ = id;
260    }
261
262    /**
263        @brief Returns true, if the Identifier is at least of the given type.
264        @param identifier The identifier to compare with
265    */
266    bool Identifier::isA(const Identifier* identifier) const
267    {
268        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
269    }
270
271    /**
272        @brief Returns true, if the Identifier is exactly of the given type.
273        @param identifier The identifier to compare with
274    */
275    bool Identifier::isExactlyA(const Identifier* identifier) const
276    {
277        return (identifier == this);
278    }
279
280    /**
281        @brief Returns true, if the assigned identifier is a child of the given identifier.
282        @param identifier The identifier to compare with
283    */
284    bool Identifier::isChildOf(const Identifier* identifier) const
285    {
286        return (this->parents_.find(identifier) != this->parents_.end());
287    }
288
289    /**
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    /**
299        @brief Returns true, if the assigned identifier is a parent of the given identifier.
300        @param identifier The identifier to compare with
301    */
302    bool Identifier::isParentOf(const Identifier* identifier) const
303    {
304        return (this->children_.find(identifier) != this->children_.end());
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    {
313        return (this->directChildren_.find(identifier) != this->directChildren_.end());
314    }
315
316    /**
317        @brief Returns the map that stores all Identifiers with their names.
318        @return The map
319    */
320    std::map<std::string, Identifier*>& Identifier::getStringIdentifierMapIntern()
321    {
322        static std::map<std::string, Identifier*> identifierMap;
323        return identifierMap;
324    }
325
326    /**
327        @brief Returns the map that stores all Identifiers with their names in lowercase.
328        @return The map
329    */
330    std::map<std::string, Identifier*>& Identifier::getLowercaseStringIdentifierMapIntern()
331    {
332        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
333        return lowercaseIdentifierMap;
334    }
335
336    /**
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    /**
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        {
406            COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << std::endl;
407            delete (it->second);
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())
423            return it->second;
424        else
425            return 0;
426    }
427
428    /**
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())
437            return it->second;
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        {
452            COUT(2) << "Warning: Overwriting XMLPortParamContainer in class " << this->getName() << '.' << std::endl;
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())
468            return it->second;
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        {
483            COUT(2) << "Warning: Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << std::endl;
484            delete (it->second);
485        }
486
487        this->xmlportObjectContainers_[sectionname] = container;
488    }
489
490    /**
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)
499        {
500            if (it != list.begin())
501                out << ' ';
502            out << (*it)->getName();
503        }
504
505        return out;
506    }
507}
Note: See TracBrowser for help on using the repository browser.