Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

avoid dependency on IdentifierManager for as long as possible

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