Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

detail

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