Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

identifier removes itself from all related identifiers when being destroyed

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