Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/class/Identifier.cc @ 10380

Last change on this file since 10380 was 10379, checked in by landauf, 9 years ago

check if all classes are registered

  • Property svn:eol-style set to native
File size: 19.3 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"
[9667]39#include "core/CoreIncludes.h"
[9563]40#include "core/config/ConfigValueContainer.h"
41#include "core/XMLPort.h"
42#include "core/object/ClassFactory.h"
[790]43
44namespace orxonox
45{
46    // ###############################
47    // ###       Identifier        ###
48    // ###############################
49    /**
50        @brief Constructor: No factory, no object created, new ObjectList and a unique networkID.
51    */
52    Identifier::Identifier()
[9667]53        : classID_(IdentifierManager::getInstance().getUniqueClassId())
[790]54    {
[1505]55        this->factory_ = 0;
[9667]56        this->bInitialized_ = false;
[10379]57        this->bRegistered_ = false;
[5929]58        this->bLoadable_ = false;
[10374]59        this->bIsVirtualBase_ = false;
[1505]60
61        this->bHasConfigValues_ = false;
[790]62
[5781]63        // Default network ID is the class ID
64        this->networkID_ = this->classID_;
[790]65    }
66
67    /**
[871]68        @brief Destructor: Deletes the list containing the children.
[790]69    */
70    Identifier::~Identifier()
71    {
[1747]72        if (this->factory_)
73            delete this->factory_;
74
75        for (std::map<std::string, ConfigValueContainer*>::iterator it = this->configValues_.begin(); it != this->configValues_.end(); ++it)
76            delete (it->second);
[5781]77        for (std::map<std::string, XMLPortParamContainer*>::iterator it = this->xmlportParamContainers_.begin(); it != this->xmlportParamContainers_.end(); ++it)
78            delete (it->second);
79        for (std::map<std::string, XMLPortObjectContainer*>::iterator it = this->xmlportObjectContainers_.begin(); it != this->xmlportObjectContainers_.end(); ++it)
80            delete (it->second);
[790]81    }
82
83    /**
[9667]84        @brief Sets the name of the class.
[1856]85    */
[9667]86    void Identifier::setName(const std::string& name)
[1856]87    {
[9667]88        if (name != this->name_)
[1856]89        {
[9667]90            this->name_ = name;
91            IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
[1856]92        }
93    }
94
[9667]95    void Identifier::setFactory(Factory* factory)
[790]96    {
[9667]97        if (this->factory_)
98            delete this->factory_;
[790]99
[9667]100        this->factory_ = factory;
[790]101    }
102
[1747]103
104    /**
[790]105        @brief Creates an object of the type the Identifier belongs to.
106        @return The new object
107    */
[9667]108    Identifiable* Identifier::fabricate(Context* context)
[790]109    {
110        if (this->factory_)
111        {
[9667]112            return this->factory_->fabricate(context);
[790]113        }
114        else
115        {
[8858]116            orxout(user_error) << "An error occurred in Identifier.cc:" << endl;
117            orxout(user_error) << "Cannot fabricate an object of type '" << this->name_ << "'. Class has no factory." << endl;
118            orxout(user_error) << "Aborting..." << endl;
[790]119            abort();
[5929]120            return 0;
[790]121        }
122    }
123
124    /**
[5929]125        @brief Sets the network ID to a new value and changes the entry in the ID-Identifier-map.
[5781]126    */
127    void Identifier::setNetworkID(uint32_t id)
128    {
129        this->networkID_ = id;
[9667]130        IdentifierManager::getInstance().addIdentifierToLookupMaps(this);
[5781]131    }
132
133    /**
[9667]134     * @brief Used to define the direct parents of an Identifier of an abstract class.
135     */
136    Identifier& Identifier::inheritsFrom(Identifier* directParent)
137    {
138        if (this->parents_.empty())
[10372]139            this->directParents_.push_back(directParent);
[9667]140        else
141            orxout(internal_error) << "Trying to add " << directParent->getName() << " as a direct parent of " << this->getName() << " after the latter was already initialized" << endl;
142
143        return *this;
144    }
145
146    /**
147     * @brief Initializes the parents of this Identifier while creating the class hierarchy.
[10366]148     * @param initializationTrace All identifiers that were recorded while creating an instance of this class (including nested classes and this identifier itself)
[9667]149     */
[10372]150    void Identifier::initializeParents(const std::list<const Identifier*>& initializationTrace)
[9667]151    {
152        if (!IdentifierManager::getInstance().isCreatingHierarchy())
153        {
154            orxout(internal_warning) << "Identifier::initializeParents() created outside of class hierarchy creation" << endl;
155            return;
156        }
157
[10371]158        if (this->directParents_.empty())
[9667]159        {
[10372]160            for (std::list<const Identifier*>::const_iterator it = initializationTrace.begin(); it != initializationTrace.end(); ++it)
[10371]161                if (*it != this)
[10372]162                    this->parents_.push_back(*it);
[9667]163        }
[10371]164        else
165            orxout(internal_error) << "Trying to add parents to " << this->getName() << " after it was already initialized with manual calls to inheritsFrom<Class>()." << endl;
[9667]166    }
167
168    /**
169     * @brief Finishes the initialization of this Identifier after creating the class hierarchy by wiring the (direct) parent/child references correctly.
170     */
171    void Identifier::finishInitialization()
172    {
173        if (!IdentifierManager::getInstance().isCreatingHierarchy())
174        {
175            orxout(internal_warning) << "Identifier::finishInitialization() created outside of class hierarchy creation" << endl;
176            return;
177        }
178
179        if (this->isInitialized())
180            return;
181
[10371]182        if (!this->parents_.empty())
183        {
184            // parents defined -> this class was initialized by creating a sample instance and recording the trace of identifiers
185
186            // initialize all parents
[10372]187            for (std::list<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it)
[10371]188                const_cast<Identifier*>(*it)->finishInitialization(); // initialize parent
189
190            // parents of parents are no direct parents of this identifier
[9667]191            this->directParents_ = this->parents_;
[10372]192            for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
193                for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
194                    this->directParents_.remove(*it_parent_parent);
[10377]195
196            this->verifyIdentifierTrace();
[10371]197        }
198        else if (!this->directParents_.empty())
199        {
200            // no parents defined -> this class was manually initialized by calling inheritsFrom<Class>()
[9667]201
[10371]202            // initialize all direct parents
[10372]203            for (std::list<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
[10371]204                const_cast<Identifier*>(*it)->finishInitialization(); // initialize parent
205
206            // direct parents and their parents are also parents of this identifier (but only add them once)
[10372]207            for (std::list<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent)
[10375]208            {
[10372]209                for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
[10376]210                    this->addIfNotExists(this->parents_, *it_parent_parent);
211                this->addIfNotExists(this->parents_, *it_parent);
[10375]212            }
[10371]213        }
214        else if (!this->isExactlyA(Class(Identifiable)))
[9667]215        {
[10371]216            // only Identifiable is allowed to have no parents (even tough it's currently not abstract)
217            orxout(internal_error) << "Identifier " << this->getName() << " / " << this->getTypeidName() << " is marked as abstract but has no direct parents defined" << endl;
218            orxout(internal_error) << "  If this class is not abstract, use RegisterClass(ThisClass);" << endl;
219            orxout(internal_error) << "  If this class is abstract, use RegisterAbstractClass(ThisClass).inheritsFrom(Class(BaseClass));" << endl;
[9667]220        }
221
222        // tell all parents that this identifier is a child
[10372]223        for (std::list<const Identifier*>::const_iterator it = this->parents_.begin(); it != this->parents_.end(); ++it)
[9667]224            const_cast<Identifier*>(*it)->children_.insert(this);
225
226        // tell all direct parents that this identifier is a direct child
[10372]227        for (std::list<const Identifier*>::const_iterator it = this->directParents_.begin(); it != this->directParents_.end(); ++it)
[9667]228        {
229            const_cast<Identifier*>(*it)->directChildren_.insert(this);
230
231            // Create the super-function dependencies
232            (*it)->createSuperFunctionCaller();
233        }
234
235        this->bInitialized_ = true;
236    }
237
238    /**
[10377]239     * Verifies if the recorded trace of parent identifiers matches the expected trace according to the class hierarchy. If it doesn't match, the class
240     * hierarchy is likely wrong, e.g. due to wrong inheritsFrom<>() definitions in abstract classes.
241     */
242    void Identifier::verifyIdentifierTrace() const
243    {
244
245        std::list<const Identifier*> expectedIdentifierTrace;
246
247        // if any parent class is virtual, it will be instantiated first, so we need to add them first.
248        for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
249        {
250            if ((*it_parent)->isVirtualBase())
251            {
252                for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
253                    this->addIfNotExists(expectedIdentifierTrace, *it_parent_parent);
254                this->addIfNotExists(expectedIdentifierTrace, *it_parent);
255            }
256        }
257
258        // now all direct parents get created recursively. already added identifiers (e.g. virtual base classes) are not added again.
259        for (std::list<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent)
260        {
261            for (std::list<const Identifier*>::const_iterator it_parent_parent = const_cast<Identifier*>(*it_parent)->parents_.begin(); it_parent_parent != const_cast<Identifier*>(*it_parent)->parents_.end(); ++it_parent_parent)
262                this->addIfNotExists(expectedIdentifierTrace, *it_parent_parent);
263            this->addIfNotExists(expectedIdentifierTrace, *it_parent);
264        }
265
266        // check if the expected trace matches the actual trace (which was defined by creating a sample instance)
267        if (expectedIdentifierTrace != this->parents_)
268        {
269            orxout(internal_warning) << this->getName() << " has an unexpected initialization trace:" << endl;
270
271            orxout(internal_warning) << "  Actual trace (after creating a sample instance):" << endl << "    ";
272            for (std::list<const Identifier*>::const_iterator it_parent = this->parents_.begin(); it_parent != this->parents_.end(); ++it_parent)
273                orxout(internal_warning) << " " << (*it_parent)->getName();
274            orxout(internal_warning) << endl;
275
276            orxout(internal_warning) << "  Expected trace (according to class hierarchy definitions):" << endl << "    ";
277            for (std::list<const Identifier*>::const_iterator it_parent = expectedIdentifierTrace.begin(); it_parent != expectedIdentifierTrace.end(); ++it_parent)
278                orxout(internal_warning) << " " << (*it_parent)->getName();
279            orxout(internal_warning) << endl;
280
281            orxout(internal_warning) << "  Direct parents (according to class hierarchy definitions):" << endl << "    ";
282            for (std::list<const Identifier*>::const_iterator it_parent = this->directParents_.begin(); it_parent != this->directParents_.end(); ++it_parent)
283                orxout(internal_warning) << " " << (*it_parent)->getName();
284            orxout(internal_warning) << endl;
285        }
286    }
287
288    /**
[10376]289     * Adds @param identifierToAdd to @param list if this identifier is not already contained in the list.
290     */
291    void Identifier::addIfNotExists(std::list<const Identifier*>& list, const Identifier* identifierToAdd) const
292    {
293        if (std::find(list.begin(), list.end(), identifierToAdd) == list.end())
294            list.push_back(identifierToAdd);
295    }
296
297    /**
[871]298        @brief Returns true, if the Identifier is at least of the given type.
[790]299        @param identifier The identifier to compare with
300    */
301    bool Identifier::isA(const Identifier* identifier) const
302    {
[10371]303        return (identifier == this || (this->isChildOf(identifier)));
[790]304    }
305
306    /**
[871]307        @brief Returns true, if the Identifier is exactly of the given type.
[790]308        @param identifier The identifier to compare with
309    */
[871]310    bool Identifier::isExactlyA(const Identifier* identifier) const
[790]311    {
312        return (identifier == this);
313    }
314
315    /**
[871]316        @brief Returns true, if the assigned identifier is a child of the given identifier.
[790]317        @param identifier The identifier to compare with
318    */
319    bool Identifier::isChildOf(const Identifier* identifier) const
320    {
[10372]321        return (std::find(this->parents_.begin(), this->parents_.end(),  identifier) != this->parents_.end());
[1505]322    }
[790]323
324    /**
[1505]325        @brief Returns true, if the assigned identifier is a direct child of the given identifier.
326        @param identifier The identifier to compare with
327    */
328    bool Identifier::isDirectChildOf(const Identifier* identifier) const
329    {
[10372]330        return (std::find(this->directParents_.begin(), this->directParents_.end(), identifier) != this->directParents_.end());
[1505]331    }
332
333    /**
[871]334        @brief Returns true, if the assigned identifier is a parent of the given identifier.
[790]335        @param identifier The identifier to compare with
336    */
337    bool Identifier::isParentOf(const Identifier* identifier) const
338    {
[5929]339        return (this->children_.find(identifier) != this->children_.end());
[1505]340    }
341
342    /**
343        @brief Returns true, if the assigned identifier is a direct parent of the given identifier.
344        @param identifier The identifier to compare with
345    */
346    bool Identifier::isDirectParentOf(const Identifier* identifier) const
347    {
[5929]348        return (this->directChildren_.find(identifier) != this->directChildren_.end());
[1505]349    }
350
351    /**
352        @brief Adds the ConfigValueContainer of a variable, given by the string of its name.
353        @param varname The name of the variablee
354        @param container The container
355    */
356    void Identifier::addConfigValueContainer(const std::string& varname, ConfigValueContainer* container)
357    {
358        std::map<std::string, ConfigValueContainer*>::const_iterator it = this->configValues_.find(varname);
359        if (it != this->configValues_.end())
360        {
[8858]361            orxout(internal_warning) << "Overwriting config-value with name " << varname << " in class " << this->getName() << '.' << endl;
[1747]362            delete (it->second);
[1505]363        }
364
365        this->bHasConfigValues_ = true;
366        this->configValues_[varname] = container;
367    }
368
369    /**
370        @brief Returns the ConfigValueContainer of a variable, given by the string of its name.
371        @param varname The name of the variable
372        @return The ConfigValueContainer
373    */
374    ConfigValueContainer* Identifier::getConfigValueContainer(const std::string& varname)
375    {
376        std::map<std::string, ConfigValueContainer*>::const_iterator it = configValues_.find(varname);
377        if (it != configValues_.end())
[5929]378            return it->second;
[1505]379        else
380            return 0;
381    }
382
383    /**
[5781]384        @brief Returns a XMLPortParamContainer that loads a parameter of this class.
385        @param paramname The name of the parameter
386        @return The container
387    */
388    XMLPortParamContainer* Identifier::getXMLPortParamContainer(const std::string& paramname)
389    {
390        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
391        if (it != this->xmlportParamContainers_.end())
[5929]392            return it->second;
[5781]393        else
394            return 0;
395    }
396
397    /**
398        @brief Adds a new XMLPortParamContainer that loads a parameter of this class.
399        @param paramname The name of the parameter
400        @param container The container
401    */
402    void Identifier::addXMLPortParamContainer(const std::string& paramname, XMLPortParamContainer* container)
403    {
404        std::map<std::string, XMLPortParamContainer*>::const_iterator it = this->xmlportParamContainers_.find(paramname);
405        if (it != this->xmlportParamContainers_.end())
406        {
[8858]407            orxout(internal_warning) << "Overwriting XMLPortParamContainer in class " << this->getName() << '.' << endl;
[5781]408            delete (it->second);
409        }
410
411        this->xmlportParamContainers_[paramname] = container;
412    }
413
414    /**
415        @brief Returns a XMLPortObjectContainer that attaches an object to this class.
416        @param sectionname The name of the section that contains the attachable objects
417        @return The container
418    */
419    XMLPortObjectContainer* Identifier::getXMLPortObjectContainer(const std::string& sectionname)
420    {
421        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
422        if (it != this->xmlportObjectContainers_.end())
[5929]423            return it->second;
[5781]424        else
425            return 0;
426    }
427
428    /**
429        @brief Adds a new XMLPortObjectContainer that attaches an object to this class.
430        @param sectionname The name of the section that contains the attachable objects
431        @param container The container
432    */
433    void Identifier::addXMLPortObjectContainer(const std::string& sectionname, XMLPortObjectContainer* container)
434    {
435        std::map<std::string, XMLPortObjectContainer*>::const_iterator it = this->xmlportObjectContainers_.find(sectionname);
436        if (it != this->xmlportObjectContainers_.end())
437        {
[8858]438            orxout(internal_warning) << "Overwriting XMLPortObjectContainer in class " << this->getName() << '.' << endl;
[5781]439            delete (it->second);
440        }
441
442        this->xmlportObjectContainers_[sectionname] = container;
443    }
444
445    /**
[1505]446        @brief Lists the names of all Identifiers in a std::set<const Identifier*>.
447        @param out The outstream
448        @param list The list (or set) of Identifiers
449        @return The outstream
450    */
451    std::ostream& operator<<(std::ostream& out, const std::set<const Identifier*>& list)
452    {
453        for (std::set<const Identifier*>::const_iterator it = list.begin(); it != list.end(); ++it)
[5929]454        {
455            if (it != list.begin())
[6417]456                out << ' ';
[5929]457            out << (*it)->getName();
458        }
[1505]459
460        return out;
461    }
[790]462}
Note: See TracBrowser for help on using the repository browser.