Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11071 was 11071, checked in by landauf, 8 years ago

merged branch cpp11_v3 back to trunk

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