Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10176 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

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