Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 7299 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
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        @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;
227            Identifier::getStringIdentifierMapIntern()[name] = this;
228            Identifier::getLowercaseStringIdentifierMapIntern()[getLowercase(name)] = this;
229            Identifier::getIDIdentifierMapIntern()[this->networkID_] = this;
230        }
231    }
232
233    /**
234        @brief Creates an object of the type the Identifier belongs to.
235        @return The new object
236    */
237    BaseObject* Identifier::fabricate(BaseObject* creator)
238    {
239        if (this->factory_)
240        {
241            return this->factory_->fabricate(creator); // We have to return a BaseObject, because we don't know the exact type.
242        }
243        else
244        {
245            COUT(1) << "An error occurred in Identifier.cc:" << std::endl;
246            COUT(1) << "Error: Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << std::endl;
247            COUT(1) << "Aborting..." << std::endl;
248            abort();
249            return 0;
250        }
251    }
252
253    /**
254        @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map.
255        @param id The new network ID
256    */
257    void Identifier::setNetworkID(uint32_t id)
258    {
259//        Identifier::getIDIdentifierMapIntern().erase(this->networkID_);
260        Identifier::getIDIdentifierMapIntern()[id] = this;
261        this->networkID_ = id;
262    }
263
264    /**
265        @brief Returns true, if the Identifier is at least of the given type.
266        @param identifier The identifier to compare with
267    */
268    bool Identifier::isA(const Identifier* identifier) const
269    {
270        return (identifier == this || (this->parents_.find(identifier) != this->parents_.end()));
271    }
272
273    /**
274        @brief Returns true, if the Identifier is exactly of the given type.
275        @param identifier The identifier to compare with
276    */
277    bool Identifier::isExactlyA(const Identifier* identifier) const
278    {
279        return (identifier == this);
280    }
281
282    /**
283        @brief Returns true, if the assigned identifier is a child of the given identifier.
284        @param identifier The identifier to compare with
285    */
286    bool Identifier::isChildOf(const Identifier* identifier) const
287    {
288        return (this->parents_.find(identifier) != this->parents_.end());
289    }
290
291    /**
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    /**
301        @brief Returns true, if the assigned identifier is a parent of the given identifier.
302        @param identifier The identifier to compare with
303    */
304    bool Identifier::isParentOf(const Identifier* identifier) const
305    {
306        return (this->children_.find(identifier) != this->children_.end());
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    {
315        return (this->directChildren_.find(identifier) != this->directChildren_.end());
316    }
317
318    /**
319        @brief Returns the map that stores all Identifiers with their names.
320        @return The map
321    */
322    std::map<std::string, Identifier*>& Identifier::getStringIdentifierMapIntern()
323    {
324        static std::map<std::string, Identifier*> identifierMap;
325        return identifierMap;
326    }
327
328    /**
329        @brief Returns the map that stores all Identifiers with their names in lowercase.
330        @return The map
331    */
332    std::map<std::string, Identifier*>& Identifier::getLowercaseStringIdentifierMapIntern()
333    {
334        static std::map<std::string, Identifier*> lowercaseIdentifierMap;
335        return lowercaseIdentifierMap;
336    }
337
338    /**
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    /**
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        {
408            COUT(2) << "Warning: Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << std::endl;
409            delete (it->second);
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())
425            return it->second;
426        else
427            return 0;
428    }
429
430    /**
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())
439            return it->second;
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        {
454            COUT(2) << "Warning: Overwriting XMLPortParamContainer in class " << this->getName() << '.' << std::endl;
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())
470            return it->second;
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        {
485            COUT(2) << "Warning: Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << std::endl;
486            delete (it->second);
487        }
488
489        this->xmlportObjectContainers_[sectionname] = container;
490    }
491
492    /**
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)
501        {
502            if (it != list.begin())
503                out << ' ';
504            out << (*it)->getName();
505        }
506
507        return out;
508    }
509}
Note: See TracBrowser for help on using the repository browser.